环境:CentOS 6.5
先关闭防火墙和Selinux
[root@rsync ~]# yum -y install rsync [root@rsync ~]# mkdir /etc/rsyncd [root@rsync ~]# touch /etc/rsyncd/rsyncd.conf //rsync的主配置文件 [root@rsync ~]# touch /etc/rsyncd/rsyncd.secrets //rsync的密码文件 [root@rsync ~]# touch /etc/rsyncd/rsyncd.motd //rsync的服务端信息提示文件 [root@rsync ~]# chmod 600 /etc/rsyncd/rsyncd.secrets [root@rsync ~]# chown root:root /etc/rsyncd/rsyncd.secrets [root@rsync ~]# useradd rsync [root@rsync ~]# useradd -g rsync yfshare [root@rsync ~]# useradd -g rsync jack [root@rsync ~]# cat /etc/rsyncd/rsyncd.secrets yfshare:123456 jack:123456 [root@rsync ~]# mkdir /rsync/input -p [root@rsync ~]# mkdir /rsync/test{1..2} -p && mkdir /rsync/test [root@rsync ~]# chown rsync:rsync /rsync/ -R [root@rsync ~]# chmod 775 /rsync/ -R
//启动rsync服务
[root@rsync ~]#/usr/bin/rsync --daemon --config=/etc/rsyncd/rsyncd.conf [root@rsync ~]#echo '/usr/bin/rsync --daemon --config=/etc/rsyncd/rsyncd.conf' >> /etc/rc.local
[root@rsync ~]#ps -ef|grep rsync |grep -v grep root 2267 1 0 17:44 ? 00:00:00 /usr/bin/rsync --daemon --config=/etc/rsyncd/rsyncd.conf [root@rsync ~]# [root@rsync ~]# netstat -tunlp |grep rsync tcp 0 0 192.168.31.100:873 0.0.0.0:* LISTEN 2106/rsync [root@rsync ~]# [root@rsync ~]# /etc/init.d/rsyncd restart Stopping rsync: [OK] Starting rsync: [OK] [root@rsync ~]# [root@rsync ~]# chkconfig --add rsyncd [root@rsync ~]# chkconfig rsyncd on
//客户端配置:
[root@client ~]# yum -y install rsync [root@client ~]# echo '123456' > /etc/rsync.password [root@client ~]# chmod 600 /etc/rsync.password [root@client ~]# rsync --list-only --password-file=/etc/rsync.password yfshare@192.168.31.100::test_rsync //这里要配置read only = yes drwxr-xr-x 4096 2016/02/23 23:01:40 . drwxr-xr-x 4096 2016/02/23 23:02:00 test1 [root@client ~]#
[root@client ~]# mkdir /backup [root@client ~]# rsync -auvzP --password-file=/etc/rsync.password yfshare@192.168.31.100::test_rsync /backup/ receiving incremental file list ./ test1/ test1/11.txt 0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=9/12) test1/12.txt 0 100% 0.00kB/s 0:00:00 (xfer#2, to-check=8/12) test1/13.txt 0 100% 0.00kB/s 0:00:00 (xfer#3, to-check=7/12) test1/14.txt 0 100% 0.00kB/s 0:00:00 (xfer#4, to-check=6/12) test1/15.txt 0 100% 0.00kB/s 0:00:00 (xfer#5, to-check=5/12) test1/16.txt 0 100% 0.00kB/s 0:00:00 (xfer#6, to-check=4/12) test1/17.txt 0 100% 0.00kB/s 0:00:00 (xfer#7, to-check=3/12) test1/18.txt 0 100% 0.00kB/s 0:00:00 (xfer#8, to-check=2/12) test1/19.txt 0 100% 0.00kB/s 0:00:00 (xfer#9, to-check=1/12) test1/20.txt 0 100% 0.00kB/s 0:00:00 (xfer#10, to-check=0/12) sent 260 bytes received 601 bytes 1722.00 bytes/sec total size is 0 speedup is 0.00 [root@client ~]# [root@client ~]# ls /backup/ test1 [root@client ~]# ls /backup/test1/ 11.txt 12.txt 13.txt 14.txt 15.txt 16.txt 17.txt 18.txt 19.txt 20.txt [root@client ~]#
//如果备份服务器的备份目录下文件比rsync上多,则被删除
[root@client test1]# touch aa{1..10}.txt [root@client test1]# ls 11.txt 13.txt 15.txt 17.txt 19.txt aa10.txt aa2.txt aa4.txt aa6.txt aa8.txt 12.txt 14.txt 16.txt 18.txt 20.txt aa1.txt aa3.txt aa5.txt aa7.txt aa9.txt [root@client test1]# [root@client test1]# rsync -auvzP --delete --password-file=/etc/rsync.password yfshare@192.168.31.100::test_rsync /backup/ receiving incremental file list deleting test1/aa9.txt deleting test1/aa8.txt deleting test1/aa7.txt deleting test1/aa6.txt deleting test1/aa5.txt deleting test1/aa4.txt deleting test1/aa3.txt deleting test1/aa2.txt deleting test1/aa10.txt deleting test1/aa1.txt test1/ sent 67 bytes received 238 bytes 610.00 bytes/sec total size is 0 speedup is 0.00 [root@client test1]# ls 11.txt 12.txt 13.txt 14.txt 15.txt 16.txt 17.txt 18.txt 19.txt 20.txt [root@client test1]#
[root@client ~]# cd /backup/test1/ [root@client test1]# touch bb{1..10}.txt [root@client ~]# cd /backup [root@client backup]# mkdir firefox [root@client backup]# touch firefox/fire{1..10}.txt [root@client backup]# cd [root@client ~]# rsync -auvzP --password-file=/etc/rsync.password yfshare@192.168.31.100::test_rsync /backup/ receiving incremental file list ./ sent 68 bytes received 405 bytes 946.00 bytes/sec total size is 0 speedup is 0.00 [root@client ~]# ls /backup/ firefox input test1 [root@client ~]#
//上传文件到rsync服务端
[root@client ~]# rsync -avSH /backup/test1/bb* yfshare@192.168.31.100::input_rsync Password: sending incremental file list bb1.txt bb10.txt bb2.txt bb3.txt bb4.txt bb5.txt bb6.txt bb7.txt bb8.txt bb9.txt sent 517 bytes received 198 bytes 286.00 bytes/sec total size is 0 speedup is 0.00 [root@client ~]#
备份脚本:
[root@client ~]# crontab -l 0 * * * * /bin/sh /root/backup_rsync.sh [root@client ~]#
参数说明:
-a 相当于-rlptgoD,-r是递归 -l是链接文件,意思是拷贝链接文件;-p表示保持文件原有权限;-t保持文件原有时间;-g保持文件原有用户组;-o 保持文件原有属主;-D 相当于块设备文件;
-u, –update 仅仅进行更新,也就是跳过所有已经存在于DST,并且文件时间晚于要备份的文件。(不覆盖更新的文件)
-z 传输时压缩;
-P 传输进度;
-v 传输时的进度等信息,和-P有点关系,自己试试。
–delete 表示客户端的数据要与服务器端完全一致,如果客户端目录里有服务器上不存在的文件,则删除。
转载于:https://blog.51cto.com/yfshare/1758400