Linux Rsync服务相关总结
Rsync是一款快速而且非常灵活的文件拷贝工具。
Rsync可以实现本地目录的拷贝(类似CP命令),同时可以支持在两台不同的主机之间进行数据的拷贝(类似于SCP);Rsync也可以实现类似ls和rm命令的功能
- 类似CP命令拷贝本地文件到不同的目录下
[root@ns01 oldboy_dir]# ll /tmp
total 0
-rw-------. 1 root root 0 Mar 20 09:03 yum.log
[root@ns01 oldboy_dir]# rsync /etc/services /tmp
[root@ns01 oldboy_dir]# ll /tmp
total 628
-rw-r--r-- 1 root root 641020 Mar 21 00:02 services
-rw-------. 1 root root 0 Mar 20 09:03 yum.log
2.类似ls命令显示文件的相关信息
[root@ns01 oldboy_dir]# rsync /tmp/services
-rw-r--r-- 641020 2018/03/21 00:02:15 services
3.类似scp命令在不同的主机之间进行文件的拷贝
[root@ns01 oldboy_dir]# rsync -avz /etc/services 172.16.1.41:/data
root@172.16.1.41's password:
sending incremental file list
services
sent 127421 bytes received 31 bytes 23173.09 bytes/sec
total size is 641020 speedup is 5.03
4.类似rm命令清空一个目录
[root@ns01 /]# tree oldboy_dir/
oldboy_dir/
├── a
│ ├── 1.txt
│ ├── 2.txt
│ ├── 3.txt
│ └── 4.txt
├── b
│ ├── 1.txt
│ ├── 2.txt
│ ├── 3.txt
│ └── 4.txt
├── c
│ ├── 1.txt
│ ├── 2.txt
│ ├── 3.txt
│ └── 4.txt
└── d
├── 1.txt
├── 2.txt
├── 3.txt
└── 4.txt
4 directories, 16 files
[root@ns01 /]# mkdir /tmp/null -p
[root@ns01 /]# rsync -avz --delete /tmp/null/ /oldboy_dir/
sending incremental file list
./
deleting d/4.txt
deleting d/3.txt
deleting d/2.txt
deleting d/1.txt
deleting d/
deleting c/4.txt
deleting c/3.txt
deleting c/2.txt
deleting c/1.txt
deleting c/
deleting b/4.txt
deleting b/3.txt
deleting b/2.txt
deleting b/1.txt
deleting b/
deleting a/4.txt
deleting a/3.txt
deleting a/2.txt
deleting a/1.txt
deleting a/
sent 29 bytes received 15 bytes 88.00 bytes/sec
total size is 0 speedup is 0.00
[root@ns01 /]# tree oldboy_dir/
oldboy_dir/
0 directories, 0 files
Rsync通过delta-transfer算法,可以实现整体和增量的数据备份从而节约网络的带宽资源占用
Rsync具有以下三种模式:
1.本地工作模式
2.通过shell管道进行数据备份的模式
[root@ns01 /]# rsync -avz -e "ssh -p22" /etc/hosts 172.16.1.41:/data
root@172.16.1.41's password:
sending incremental file list
hosts
sent 193 bytes received 31 bytes 64.00 bytes/sec
total size is 352 speedup is 1.57
3.通过daemon守护进程进行工作
[root@backup ~]# /etc/init.d/rsyncd start
Rsync service is starting... [ OK ]
[root@backup ~]# ss -tunlp | grep rsync
tcp LISTEN 0 5 :::873 :::* users:(("rsync",27347,5))
tcp LISTEN 0 5 *:873 *:* users:(("rsync",27347,3))
Rsync服务端的配置流程:
- 检查服务器是否安装了Rsync软件,如果没有安装则安装Rsync软件
- rpm -qa | grep rsync
- yum -y install rsync
- 新建Rsync软件的配置文件(/etc/rsyncd.conf),并进行相关的配置
cat >> /etc/rsyncd.conf << EOF
#Rsync_configure_file
#create by XXX at 2018-X-X
#Rsync_________start
uid = rsync
gid = rsync
max connections = 200
timeout = 300
read only = false
list = false
use_chroot = no
ignore erres
hosts allow = 172.16.1.0/24
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
auth users = rsync_bakcup
secrets file = /etc/rsync.password
[backup]
comment = "This is backup directory for XXXX"
path = /backup
EOF
3.创建Rsync用来管理备份目录的用户及其用户组
useradd rsync -s /sbin/nologin -M
4.创建Rsync用来备份数据的目录,并进行相关的授权
mkdir -p /backup
chown -R rsync.rsync /backup
5.创建Rsync服务用来完成认证的密钥文件(/etc/rsync.password)
echo "XXXX:YYYYY" > /etc/rsync.password
6.使用Rsync命令进行守护进程的启动
rsync --daemon
Rsync客户端的配置流程:
- 检查服务器是否安装了Rsync软件,如果没有安装则安装Rsync软件
- rpm -qa | grep rsync
- yum -y install rsync
- 创建Rsync需要使用的密钥文件
- echo "YYYYY" > /etc/rsync.password
- 使用Rsync命令进行相关的免密文件拷贝
rsync -avz /etc/services rsync_backup@172.16.1.31::backup --password-file=/etc/rsync.password
Rsync服务帮助信息说明
1)详细rsync命令帮助信息:man rsync
2)详细rsyncd.conf配置文件:man rsyncd.conf
Rsync基于守护进程方式工作的拓展应用
- 守护进程的特殊启动方式(被超级守护进程xinetd管理)
- 查看系统是否安装了xinetd软件,如果没有安装,完成软件的安装
- rpm -qa | grep xinetd
- yum -y install xinetd
- 编辑/etc/xinetd.d/rsync文件
- disabled = no
- 使用/etc/init.d/xinetd启动rsync服务的启动
- /etc/init.d/xinetd start
- 查看系统是否安装了xinetd软件,如果没有安装,完成软件的安装
- rsync多模块配置
- 修改/etc/rsyncd.conf配置文件
- [backup]
- path = /backup
- comment = "This is backup directory for XXX"
- [nfsbackup]
- path = /nfsbackup
- comment = "This is backup directory for YYY"
- 系统增加备份文件目录
- mkdir -p /nfsbackup
- 重新启动rsync服务
- /etc/init.d/xinetd restart
- 修改/etc/rsyncd.conf配置文件
- 使用--exclude=将需要备份的文件进行过滤
- rsync -avz /oldboy_dir --exclude={a,c} --exclude=d/2.txt rsync_backup@172.16.1.41::nfsbackup --password-file=/etc/rsync_password
- 使用--exclude-from=文件全路径方式进行备份文件的过滤
- 首先创建需要排除的文件列表
- cat >> exclude.txt << EOF
- a
- c
- d/2.txt
- exclude.txt
- EOF
- 使用命令进行数据备份
- rsync -avz /oldboy_dir --exclude-from=/oldboy_dir/exclude.txt rsync_backup@172.16.1.41::nfsbackup --password-file=/etc/rsync_password
- 首先创建需要排除的文件列表
- 使用rsync文件进行备份目录的子目录的创建
-
rsync -avz /oldboy_dir/ --exclude {a,c} --exclude d/2.txt rsync_backup@172.16.1.41::nfsbackup/sa --password-file=/etc/rsync.password
无法使用Rsync进行多级子目录的创建
[root@ns01 oldboy_dir]# rsync -avz /oldboy_dir/ --exclude={a,c} --exclude=d/2.txt rsync_backup@172.16.1.41::nfsbackup/sa/01/02/03 --password-file=/etc/rsync.password
sending incremental file list
rsync: mkdir "/sa/01/02/03" (in nfsbackup) failed: No such file or directory (2)
rsync error: error in file IO (code 11) at main.c(576) [receiver=3.0.6]
rsync: read error: Connection reset by peer (104)
rsync error: error in rsync protocol data stream (code 12) at io.c(759) [sender=3.0.6]
-
- 使用--delete现象进行无差异化的同步
-
无差异同步数据概念:你有什么,我也有什么;你没有什么,我有的也要删除
[root@nfs01 oldboy_dir]# rsync -avz /oldboy_dir --delete rsync_backup@172.16.1.41::nfsbackup --password-file=/etc/rsync.password
sending incremental file list
deleting oldboy_dir/test.txt
sent 292 bytes received 13 bytes 610.00 bytes/sec
total size is 23 speedup is 0.08
[root@nfs01 oldboy_dir]# touch test.txt
[root@nfs01 oldboy_dir]# rsync -avz /oldboy_dir --delete rsync_backup@172.16.1.41::nfsbackup --password-file=/etc/rsync.password
sending incremental file list
oldboy_dir/
oldboy_dir/test.txt
sent 357 bytes received 35 bytes 784.00 bytes/sec
total size is 23 speedup is 0.06
说明:通过无差异同步,可以实现备份服务器端数据与存储服务端数据保持高度一致
面试:如何快速删除目录中大的数据
mkdir /null <-- 创建一个空目录
rsync -avz --delete /null/ /oldboy_dir/ <-- 利用空目录和有数据的目录进行无差异同步,实现快速清空目录数据信息
-
- 使用rsync的配置文件的列表功能(list = true)
list = true
rsync客户端,可以利用命令查看服务端模块列表信息
rsync rsync_backup@172.16.1.41::
list = false
rsync客户端,不可以利用命令查看服务端模块列表信息
rsync rsync_backup@172.16.1.41::
说明:出于安全考虑,建议将list设置为false
一键安装Rsync服务并自行编写Rsync启动脚本同时支持chkconfig管理
[root@db01 tmp]# cat OKIRsync.sh
#!/bin/bash
# One Key To Install Rsync Create By Duanzhili at 2018-03-21
#初始化系统函数库
. /etc/rc.d/init.d/functions
#检查Rsync软件是否安装
if [ `/bin/rpm -qa rsync | grep -w rsync | wc -l` -lt 1 ];then
/usr/bin/yum -y install rsync
fi
#创建Rsync的配置文件
##判断是否已经存在Rsync的配置文件,如果存在首先备份
if [ -f /etc/rsyncd.conf ];then
/bin/mv /etc/rsyncd.conf /etc/rsyncd.conf.bak
fi
##开始创建Rsync的配置文件
/bin/cat >> /etc/rsyncd.conf << EOF
#Rsync_________________configure file
#create by Duanzhili at 2018-03-21
#
#设置Rsync的用户ID和组ID
uid = rsync
gid = rsync
#设置安全相关参数
use_chroot = no
list = false
read only = false
ignore errors
hosts allow = 172.16.1.0/24
hosts deny = 0.0.0.0/32
#设置Rsync相关的文件位置
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
#设置系统的最大连接数和超时时间
max connections = 200
timeout = 300
#设置认证用户和密码文件
auth users = rsync_backup
secrets file = /etc/rsync.password
#设置Rsync的备份模块
[backup]
comment = "This is a backup directory for oldboy"
path = /backup
EOF
##创建Rsync相关的系统用户
/usr/sbin/useradd rsync -s /sbin/nologin -M
##创建Rsync认证文件并修改文件授权到600
echo "oldboy123" >> /etc/rsync.password
chmod 600 /etc/rsync.password
##创建备份目录并授权给rsync用户
/bin/mkdir /backup -p
/bin/chown rsync.rsync /backup
#创建Rsync启动脚本并添加为chkconfig管理
##判断服务器脚本目录是否存在
if [ ! -d /server/scripts ];then
/bin/mkdir -p /server/scripts
fi
##目录创建完成后开始创建Rsync的启动脚本Rsyncd
if [ -f /server/scripts/rsyncd ];then
/bin/mv /server/scripts/rsyncd /server/scripts/rsyncd.bak
fi
/bin/cat >> /server/scripts/rsyncd <<EOF
#!/bin/bash
#chkconfig:2345 55 25
#This is a script for start Rsync
#create by Duanzhili at 2018-03-21
#初始化系统函数库
. /etc/rc.d/init.d/functions
#启动脚本
PFile='/var/run/rsyncd.pid'
LFile='/var/run/rsync.lock'
#CMD='`ps -ef | grep -w rsync | wc -l`'
start() {
###判断rsync服务是否已经启动
if [ CMD -gt 1 ];then
action "The Rsync service is running..." /bin/false
exit 2
fi
###判断一下Rsync服务相关的文件是否存在,如果存在直接删除
if [ -f $PFile ] || [ -f $LFile ];then
/bin/rm -f $PFile > /dev/null 2&>1
/bin/rm -f $LFile > /dev/null 2&>1
fi
/usr/bin/rsync --daemon
action "Rsync Service is starting...." /bin/true
}
stop() {
###判断rsync服务是否已经关闭
if [ CMD -lt 2 ];then
action "The Rsync Service is stopped..." /bin/false
exit 4
fi
/bin/kill PID
/bin/rm -f $PFile > /dev/null 2&>1
/bin/rm -f $LFile > /dev/null 2&>1
action "The Rsync Service is stopping..." /bin/true
}
restart() {
stop
start
}
case "\$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
*)
echo "Usage \$0 {start|stop|restart}"
exit 2
esac
EOF
sed -i 's/PID/\`\/bin\/cat $PFile\`/g' /server/scripts/rsyncd
sed -i 's/CMD/\`ps \-ef \| grep \-w rsync \| wc \-l\`/g' /server/scripts/rsyncd
##将文件拷贝到/etc/init.d/目录下,并添加可执行权限
/bin/cp /server/scripts/rsyncd /etc/init.d/
/bin/chmod +x /etc/init.d/rsyncd
#启动服务,添加自启动
START="start"
/etc/init.d/rsyncd $START
/sbin/chkconfig rsyncd on
echo "==================================="
echo "您可以愉快的使用./etc/init.d {start|stop|restart}进行管理服务了"
echo "=================================="
action “One Key to Install Rsync Service is finished...” /bin/true
浙公网安备 33010602011771号