[root@localhost~]# mkdir /qz
[root@localhost~]# cd /qz/[root@localhost qz]# touch q.txt z.txt
[root@localhost qz]# ls
q.txt z.txt
[root@localhost qz]# cd /opt/[root@localhost opt]# ls
rh
[root@localhost opt]# cd -/qz
[root@localhost qz]# rsync -avz /qz//opt/ 【a:归档模式;z:在传输文件时进行压缩;v:显示同步过程的详细信息】
sending incremental file list
./
q.txt
z.txt
sent 125 bytes received 53 bytes 356.00 bytes/sec
total size is 0 speedup is 0.00[root@localhost qz]# cd /opt/[root@localhost opt]# ls
q.txt rh z.txt
[root@localhost opt]# rm -rf /qz/[root@localhost opt]# rsync -avz /opt//qz/
sending incremental file list
created directory /qz
./
q.txt
z.txt
rh/
sent 151 bytes received 57 bytes 416.00 bytes/sec
total size is 0 speedup is 0.00[root@localhost opt]# cd /qz/[root@localhost qz]# ls
q.txt rh z.txt
【先在rsync端创建源目录/文件用于同步实验】
[root@rsync html]# echo "this is rsync">> q.txt
[root@rsync html]# echo "this is rsync2">> q2.txt
[root@rsync html]# ls
q2.txt q.txt
【同步格式一】
[root@client~]# systemctl stop firewalld.service
[root@client~]# setenforce 0[root@client~]# systemctl disable firewalld.service
[root@client/]# rsync -avz qz@192.168.131.10::wwwroot /qz/
Password:
receiving incremental file list
./
q.txt
sent 77 bytes received 164 bytes 160.67 bytes/sec
total size is 14 speedup is 0.06[root@client/]# cat /qz/q.txt 【查看,验证同步结果】
this is rsync
【同步格式二】
[root@client/]# rsync -vaz rsync://qz@192.168.131.10/wwwroot /qz/
Password:
receiving incremental file list
./
q2.txt
sent 77 bytes received 184 bytes 104.40 bytes/sec
total size is 29 speedup is 0.11[root@client/]# cat /qz/q2.txt
this is rsync2
2.2.3 免交互格式配置
【rsync端创建文件用于测试】
[root@rsync html]# echo "this is rsync3">> q3.txt
[root@rsync html]# ls
q2.txt q3.txt q.txt
【client端】
[root@client/]# echo "5514">/etc/server.pass
[root@client/]# cat /etc/server.pass
5514[root@client/]# chmod 600/etc/server.pass
[root@client/]# rsync -zva --password-file=/etc/server.pass qz@192.168.131.10::wwwroot /qz/
receiving incremental file list
./
q3.txt
sent 77 bytes received 199 bytes 552.00 bytes/sec
total size is 44 speedup is 0.16[root@client/]# cat /qz/q3.txt
this is rsync3
2.2.4 周期性计划任务
【每天的0点5分执行同步任务】
[root@client/]# crontab -e
no crontab for root - using an empty one
50***/usr/bin/rsync -az --delete --password-file=/etc/server.pass qz@192.168.131.10::wwwroot /qz/[root@client/]# systemctl restart crond.service