rsync
rsync
rsync的备份方式是增量备份,只传输本地与目标相比更改多的文档,从而减少了备份时间,也避免保留多次完全备份而占用巨量存储空间。
rsync支持在本地的目录之间执行同步,用法如下(源目录跟/符号表示被同步的是目录下的文档而不是整个目录):
rsync [选项...] 本地目录1 本地目录2
rsync [选项...] 本地目录1/ 本地目录2
rsync也支持在本地与远程主机之间执行同步,以SSH服务器为例,用法如下(上行同步时,认证的用户必须对目标目录有写入权限):
下行:rsync [选项...] user@host:源目录 本地目录
上行:rsync [选项...] 本地目录 user@host:目标目录
rsync常用的命令选项:
-a:归档模式,相当于递归、保留权限等多个选项的组合
-v:显示同步过程详细信息
-z:传输过程中启用压缩
-A:保留文件的ACL属性信息
-n:测试同步过程,不做实际修改
--delete:删除目标文件夹内多余的文档
测试rsync上传、下载同步的基本用法
测试rsync的命令选项-a、-v、--delete、-n的用途
使用rsync从SSH服务器下载 /boot/ 目录
使用rsync将本地的/etc/ 目录到上传到SSH服务器
rsync 基本用法
创建测试目录
mkdir /opt/dir1 /opt/dir2
ifconfig br0 > /opt/dir1/ip.txt
mkdir /opt/dir1/dir1.1
[root@Final ~]# ll /opt/dir1/ /opt/dir2/
/opt/dir1/:
total 4
drwxr-xr-x 2 root root 6 Jul 5 10:09 dir1.1
-rw-r--r-- 1 root root 490 Jul 5 10:09 ip.txt
/opt/dir2/:
total 0
验证-a归档选项、源目录后是否跟/的作用
未添加-a选项时,当被同步的文档包含目录时,无法向下递归
[root@Final ~]# rsync /opt/dir1 /opt/di2
skipping directory dir1 //目录被跳过
ll /opt/dir1/ /opt/dir2/
/opt/dir1/:
total 4
drwxr-xr-x 2 root root 6 Jul 5 10:09 dir1.1
-rw-r--r-- 1 root root 490 Jul 5 10:09 ip.txt
/opt/dir2/:
total 0
添加-a选项,再次执行上述同步,被同步的是整个/opt/dir1目录(末尾无/)
[root@Final ~]# rsync /opt/dir1 /opt/dir2 -a
[root@Final ~]# !ll
ll /opt/dir1/ /opt/dir2/
/opt/dir1/:
total 4
drwxr-xr-x 2 root root 6 Jul 5 10:09 dir1.1
-rw-r--r-- 1 root root 490 Jul 5 10:09 ip.txt
/opt/dir2/:
total 0
drwxr-xr-x 3 root root 34 Jul 5 10:09 dir1 //作为整体被同步过来
修改上述操作,将源目录携程/opt/dir1/,只同步此目录下的文档
[root@Final ~]# !ll
ll /opt/dir1/ /opt/dir2/
/opt/dir1/:
total 4
drwxr-xr-x 2 root root 6 Jul 5 10:09 dir1.1
-rw-r--r-- 1 root root 490 Jul 5 10:09 ip.txt
/opt/dir2/:
total 4
drwxr-xr-x 3 root root 34 Jul 5 10:09 dir1 //上次同步过来的
drwxr-xr-x 2 root root 6 Jul 5 10:09 dir1.1 //分别独立被同步过来
-rw-r--r-- 1 root root 490 Jul 5 10:09 ip.txt //分别独立被同步过来
验证--delete选项的作用
在/opt/dir2/目录下添加测试文件3.txt,将测试子目录2.dir改名为3.dir,现在与/opt/dir1/目录的内容是不一致的:
[root@Final ~]# cat /proc/cpuinfo > /opt/dir2/3.txt
[root@Final ~]# mv /opt/dir2/
3.txt dir1/ dir1.1/ ip.txt
[root@Final ~]# mv /opt/dir2/dir1.1/ /opt/dir2/dir2.1
[root@Final ~]# !ll
ll /opt/dir1/ /opt/dir2/
/opt/dir1/:
total 4
drwxr-xr-x 2 root root 6 Jul 5 10:09 dir1.1
-rw-r--r-- 1 root root 490 Jul 5 10:09 ip.txt
/opt/dir2/:
total 8
-rw-r--r-- 1 root root 3848 Jul 5 10:19 3.txt
drwxr-xr-x 3 root root 34 Jul 5 10:09 dir1
drwxr-xr-x 2 root root 6 Jul 5 10:09 dir2.1
-rw-r--r-- 1 root root 490 Jul 5 10:09 ip.txt
未添加--delete选项时,rsync同步操作是单向的,只是把源目录的文档增量更新到目标目录,而目标目标残留的其他文档不会做任何处理
[root@Final ~]# rsync -a /opt/dir1/ /opt/dir2
[root@Final ~]# !ll
ll /opt/dir1/ /opt/dir2/
/opt/dir1/:
total 4
drwxr-xr-x 2 root root 6 Jul 5 10:09 dir1.1
-rw-r--r-- 1 root root 490 Jul 5 10:09 ip.txt
/opt/dir2/:
total 8
-rw-r--r-- 1 root root 3848 Jul 5 10:19 3.txt
drwxr-xr-x 3 root root 34 Jul 5 10:09 dir1
drwxr-xr-x 2 root root 6 Jul 5 10:09 dir1.1 //新同步来的子目录
drwxr-xr-x 2 root root 6 Jul 5 10:09 dir2.1
-rw-r--r-- 1 root root 490 Jul 5 10:09 ip.txt
若要保持两边一致,即删除“目标目录下有而源目录下没有”的文档,可以添加--delete选项。修改前一条操作如下,可以看到两个目录的内容完全相同(多余文档被删除):
[root@Final ~]# rsync -a --delete /opt/dir1/ /opt/dir2
[root@Final ~]# !ll
ll /opt/dir1/ /opt/dir2/
/opt/dir1/:
total 4
drwxr-xr-x 2 root root 6 Jul 5 10:09 dir1.1
-rw-r--r-- 1 root root 490 Jul 5 10:09 ip.txt
/opt/dir2/:
total 4
drwxr-xr-x 2 root root 6 Jul 5 10:09 dir1.1
-rw-r--r-- 1 root root 490 Jul 5 10:09 ip.txt
验证-v选项的作用
未添加-v选项时,rsync同步操作是静默执行的,除非报错否则无屏幕输出:
[root@Final ~]# rsync -a -v --delete /opt/dir1/ /opt/dir2
sending incremental file list
sent 101 bytes received 13 bytes 228.00 bytes/sec
total size is 490 speedup is 4.30
验证-n选项的作用
选项-n主要用来模拟同步过程,而并不会真正的执行,此选项通常与-v一起使用。比如说,可以先清空/opt/dir2/目录,然后rsync结合-nv选项来了解一下指定的同步会执行哪些操作
[root@Final ~]# rm -rf /opt/dir2/*
[root@Final ~]# !ll
ll /opt/dir1/ /opt/dir2/
/opt/dir1/:
total 4
drwxr-xr-x 2 root root 6 Jul 5 10:09 dir1.1
-rw-r--r-- 1 root root 490 Jul 5 10:09 ip.txt
/opt/dir2/:
total 0
[root@Final ~]# rsync -a -nv --delete /opt/dir1/ /opt/dir2
sending incremental file list
./
ip.txt //会同步
dir1.1/ //会同步
sent 110 bytes received 26 bytes 272.00 bytes/sec
total size is 490 speedup is 3.60 (DRY RUN)
[root@Final ~]# !ll
ll /opt/dir1/ /opt/dir2/
/opt/dir1/:
total 4
drwxr-xr-x 2 root root 6 Jul 5 10:09 dir1.1
-rw-r--r-- 1 root root 490 Jul 5 10:09 ip.txt
/opt/dir2/: //实际上并未执行
total 0
把上述同步操作中的-n选项去掉,才会真正地执行同步操作
[root@Final ~]# rsync -av --delete /opt/dir1/ /opt/dir2
sending incremental file list
./
ip.txt
dir1.1/
sent 644 bytes received 46 bytes 1,380.00 bytes/sec
total size is 490 speedup is 0.71
[root@Final ~]# !ll
ll /opt/dir1/ /opt/dir2/
/opt/dir1/:
total 4
drwxr-xr-x 2 root root 6 Jul 5 10:09 dir1.1
-rw-r--r-- 1 root root 490 Jul 5 10:09 ip.txt
/opt/dir2/:
total 4
drwxr-xr-x 2 root root 6 Jul 5 10:09 dir1.1
-rw-r--r-- 1 root root 490 Jul 5 10:09 ip.txt
rsync+ssh
将远程主机上的 /boot/目录下载备份到本地(选项-z表示压缩)
服务端240 客户端254
[root@Final pub]# rsync -az --delete root@192.168.10.240:/boot /bootbak
root@192.168.10.240's password:
[root@Final pub]# ll /bootbak/
total 4
dr-xr-xr-x 5 root root 4096 May 8 16:08 boot
将本地的/etc/selinux目录上传备份到远程主机
[root@Final ~]# du -sh /etc/selinux/
19M /etc/selinux/
[root@Final ~]# rsync -az /etc/selinux/ root@192.168.10.240:/opt/
root@192.168.10.240's password:
服务端240确认同步结果
[root@node1 ~]# du -sh /opt/selinux/
19M /opt/selinux/ //确认同步结果
rsync+rsync同步
在rsync源端将/usr/src 目录发布为同步资源:共享名为tools,仅允许用户 ruser 以密码 pass 访问
在rsync操作端测试 rsync+rsync下行同步
建立同步账号文件
[root@node1 ~]# chmod 600 /etc/rsyncd_users.db //严格权限,否则同步会失败
[root@node1 ~]# cat /etc/rsyncd_users.db
ruser:pass //用户名:密码,每行一个用户
othername:233
建立 /etc/rsyncd.conf 共享设置
[tools] //定义共享名
path = /usr/src //被共享的目录位置
comment = Rsync Share Test //同步资源说明
read only = yes //只读
dont compress = *.gz *.bz2 *.tgz *.zip //同步时不再压缩的文档类型
auth users = ruser //允许谁访问
secrets file = /etc/rsyncd_users.db //指定账号文件的路径
在上述配置文件中,若不添加最后两行认证配置,则默认以匿名方式提供
启用 rsync --daemon 服务端
[root@node1 ~]# du -sh /usr/src/
0 /usr/src/
[root@node1 ~]# cp /boot/ /usr/src/ -rf
[root@node1 ~]# du -sh /usr/src/ //确认待发布的同步目录
97M /usr/src/
chmod 777 -R /usr/src/*
systemctl start rsyncd
yum -y install xinetd
chkconfig rsync on //打开rsync服务开关
chkconfig xinetd on
service xinetd restart //通过xinetd启动
rsync + rsync下行同步测试
查看及列表同步资源
查看远程主机提供了哪些同步资源:
[root@Final ~]# rsync 192.168.10.240::
tools Rsync Share Test //共享名、共享说明
列出指定同步资源下的文档:
[root@Final ~]# rsync ruser@192.168.10.240::tools //浏览共享
Password: //验证ruser用户的口令
drwxr-xr-x 49 2019/07/05 13:53:08 .
drwxr-xr-x 6 2018/04/11 12:59:55 debug
drwxr-xr-x 6 2018/04/11 12:59:55 kernels
drwxr-xr-x 81 2019/07/05 13:53:10 selinux
rsync下行同步
[root@Final ~]# rsync -avz ruser@192.168.10.240::tools/ /root/mysrc/ //下行同步,删除多余文件
Password: //验证密码pass
[root@Final ~]# du -sh /root/mysrc/ //确认同步结果
103M mysrc/
网站镜像同步
yum install httpd -y
[root@node1 ~]# systemctl restart httpd
[root@node1 ~]# echo node1-rsyncd > /var/www/html/index.html
[root@node1 ~]# curl 192.168.10.240
node1-rsyncd
建立rsync同步账号文件
[root@node1 ~]# cat /etc/rsyncd_users.db
ruser:pass //原有用户可保留
othername:233
wuser:wpass //添加同步账号
chmod 600 /etc/rsyncd_users.db //注意确认文件权限
建立 /etc/rsyncd.conf 共享设置
vim /etc/rsyncd.conf
[wwwroot] //定义共享名
path=/var/www/html //被共享的目录位置
comment=Web DocumentRoot //同步资源说明
read only=yes //只读
dont compress = *.gz *.bz2 *.tgz *.zip //同步时不再压缩的文档类型
auth users= wuser //允许谁访问
secrets file = /etc/rsyncd_users.db //指定账号文件的路径
在上述配置文件中,若不添加最后两行认证配置,则默认以匿名方式提供。
启用 rsync --daemon 服务端
yum -y install xinetd
chkconfig rsync on //打开rsync服务开关
chkconfig xinetd on
service xinetd restart //通过xinetd启动
systemctl start httpd
rsync wuser@192.168.10.240::wwwroot/ /var/www/html -az
[root@Final html]# curl 192.168.10.254
node1-rsyncd
实现免交互的rsync同步操作
创建用来验证的密码文件,并将权限设为600:
[root@Final html]# chmod 600 /root/pwd.txt
[root@Final html]# cat /root/pwd.txt
wpass
执行免交互同步操作:
[root@Final html]# rsync -az --password-file=/root/pwd.txt wuser@192.168.10.240::wwwroot/ /var/www/html/
[root@Final html]# !cu
curl 192.168.10.254
node1-rsyncd
配置计划任务
service crond restart //确保启动计划任务服务
chkconfig crond on //设为开机自动运行
crontab -e //添加计划任务
0 */2 * * * rsync --password-file=/root/pwd.txt --delete -az wuser@192.168.4.5::webroot/ /var/www/html/
补充使用rsync同步,服务和客户端需要安装软件包
若rsync同步失败,确认下文件及目录权限,还有SELINUX是否关闭
可以同步yum源及epel源
镜像同步公网 yum 源上游 yum 源必须要支持 rsync 协议,否则不能使用 rsync 进行同步。
centos 源:rsync://rsync.mirrors.ustc.edu.cn/centos/
epel 源:rsync://rsync.mirrors.ustc.edu.cn/epel/
rsync -vrt --bwlimit=3000 --exclude=debug/ rsync://rsync.mirrors.ustc.edu.cn/epel/7/x86_64/ /data/mirrors/centos/epel7/x86_64/
rsync -vrt rsync://mirrors.ustc.edu.cn/centos/7/os/x86_64/ /data/mirrors/centos/7/os/x86_64/