环境:CentOS 6.5
先关闭防火墙和Selinux
[[email protected] ~]# yum -y install rsync [[email protected] ~]# mkdir /etc/rsyncd [[email protected] ~]# touch /etc/rsyncd/rsyncd.conf //rsync的主配置文件 [[email protected] ~]# touch /etc/rsyncd/rsyncd.secrets //rsync的密码文件 [[email protected] ~]# touch /etc/rsyncd/rsyncd.motd //rsync的服务端信息提示文件 [[email protected] ~]# chmod 600 /etc/rsyncd/rsyncd.secrets [[email protected] ~]# chown root:root /etc/rsyncd/rsyncd.secrets [[email protected] ~]# useradd rsync [[email protected] ~]# useradd -g rsync yfshare [[email protected] ~]# useradd -g rsync jack [[email protected] ~]# cat /etc/rsyncd/rsyncd.secrets yfshare:123456 jack:123456 [[email protected] ~]# mkdir /rsync/input -p [[email protected] ~]# mkdir /rsync/test{1..2} -p && mkdir /rsync/test [[email protected] ~]# chown rsync:rsync /rsync/ -R [[email protected] ~]# chmod 775 /rsync/ -R
//启动rsync服务
[[email protected] ~]#/usr/bin/rsync --daemon --config=/etc/rsyncd/rsyncd.conf [[email protected] ~]#echo ‘/usr/bin/rsync --daemon --config=/etc/rsyncd/rsyncd.conf‘ >> /etc/rc.local
[[email protected] ~]#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 [[email protected] ~]# [[email protected] ~]# netstat -tunlp |grep rsync tcp 0 0 192.168.31.100:873 0.0.0.0:* LISTEN 2106/rsync [[email protected] ~]# [[email protected] ~]# /etc/init.d/rsyncd restart Stopping rsync: [OK] Starting rsync: [OK] [[email protected] ~]# [[email protected] ~]# chkconfig --add rsyncd [[email protected] ~]# chkconfig rsyncd on
//客户端配置:
[[email protected] ~]# yum -y install rsync [[email protected] ~]# echo ‘123456‘ > /etc/rsync.password [[email protected] ~]# chmod 600 /etc/rsync.password [[email protected] ~]# rsync --list-only --password-file=/etc/rsync.password [email protected]::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 [[email protected] ~]#
[[email protected] ~]# mkdir /backup [[email protected] ~]# rsync -auvzP --password-file=/etc/rsync.password [email protected]::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 [[email protected] ~]# [[email protected] ~]# ls /backup/ test1 [[email protected] ~]# ls /backup/test1/ 11.txt 12.txt 13.txt 14.txt 15.txt 16.txt 17.txt 18.txt 19.txt 20.txt [[email protected] ~]#
//如果备份服务器的备份目录下文件比rsync上多,则被删除
[[email protected] test1]# touch aa{1..10}.txt [[email protected] 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 [[email protected] test1]# [[email protected] test1]# rsync -auvzP --delete --password-file=/etc/rsync.password [email protected]::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 [[email protected] test1]# ls 11.txt 12.txt 13.txt 14.txt 15.txt 16.txt 17.txt 18.txt 19.txt 20.txt [[email protected] test1]#
[[email protected] ~]# cd /backup/test1/ [[email protected] test1]# touch bb{1..10}.txt [[email protected] ~]# cd /backup [[email protected] backup]# mkdir firefox [[email protected] backup]# touch firefox/fire{1..10}.txt [[email protected] backup]# cd [[email protected] ~]# rsync -auvzP --password-file=/etc/rsync.password [email protected]::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 [[email protected] ~]# ls /backup/ firefox input test1 [[email protected] ~]#
//上传文件到rsync服务端
[[email protected] ~]# rsync -avSH /backup/test1/bb* [email protected]::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 [[email protected] ~]#
备份脚本:
[[email protected] ~]# crontab -l 0 * * * * /bin/sh /root/backup_rsync.sh [[email protected] ~]#
参数说明:
-a 相当于-rlptgoD,-r是递归 -l是链接文件,意思是拷贝链接文件;-p表示保持文件原有权限;-t保持文件原有时间;-g保持文件原有用户组;-o 保持文件原有属主;-D 相当于块设备文件;
-u, --update 仅仅进行更新,也就是跳过所有已经存在于DST,并且文件时间晚于要备份的文件。(不覆盖更新的文件)
-z 传输时压缩;
-P 传输进度;
-v 传输时的进度等信息,和-P有点关系,自己试试。
--delete 表示客户端的数据要与服务器端完全一致,如果客户端目录里有服务器上不存在的文件,则删除。
时间: 2024-11-04 20:21:31