Rsync實操
一.rsync命令
?#類似于cp[root@user2 ~]# rsync info.sh root@192.168.168.130:/rootroot@192.168.168.130's password: [root@user1 ~]# lsanaconda-ks.cfg ceph-release-1-1.el7.noarch.rpm info.sh
二、使用rsync備份push方式
-
服務器:server 192.168.168.130(需要進行數據備份的主機)
-
客戶端:client 192.168.168.131 (備份存儲的主機)
?# 在客戶端上編寫rsync配置文件,創建一個存放備份的同步目錄[root@targetpc ~]# vim /etc/rsyncd.conf[root@targetpc ~]# cat /etc/rsyncd.confport=873address = 192.168.168.131 #客戶端地址uid = rootgid = rootuse chroot = yesmax connections = 4pid file = /var/run/rsyncd.pidlock file = /var/run/rsync.locklog file = /var/log/rsyncd.logmotd file = /etc/rsyncd.motd #可弄可不弄hosts allow = 192.168.168.0/24 #允許哪個網段傳輸數據[data] #網絡名path = /data/backup #數據存儲目錄 真實名 需要創建comment = bakcup dataread only = falselist = yes#獨立驗證auth users = rsyncusersecrets file = /etc/rsync.passwd #自己創建?#設置獨立賬戶密碼[root@targetpc ~]# vim /etc/rsync.passwd#添加權限[root@targetpc ~]# chmod 600 /etc/rsync.passwd[root@targetpc ~]# vim /etc/rsync.passwd[root@targetpc ~]# cat /etc/rsync.passwdrsyncuser:123#設置標語[root@targetpc ~]# echo "來送數據了" > /etc/rsyncd.motd[root@targetpc ~]# systemctl restart rsyncd?#測試 服務器測試[root@sourcepc data]# ls1.txt ?2.txt ?3.txt aaa[root@sourcepc data]# rsync -avz /data/* rsyncuser@192.168.168.131::data來送數據了?Password: sending incremental file list3.txt?sent 155 bytes received 36 bytes ?20.11 bytes/sectotal size is 0 speedup is 0.00#客戶主機結果[root@targetpc backup]# ls1.txt ?2.txt ?3.txt aaa
??
三、測試--delete命令push
?#--delete實驗 不能與*同時用[root@targetpc backup]# ls1.txt ?2.txt ?3.txt aaa del.txt[root@sourcepc data]# ls1.txt ?2.txt ?3.txt aaa?[root@sourcepc data]# rsync -avz --delete /data/* rsyncuser@192.168.168.131::data來送數據了?Password: sending incremental file list?sent 119 bytes received 13 bytes ?13.89 bytes/sectotal size is 0 speedup is 0.00[root@targetpc backup]# ls1.txt ?2.txt ?3.txt aaa del.txt#使用--delete時 去掉*[root@sourcepc data]# rsync -avz --delete /data/ rsyncuser@192.168.168.131::data來送數據了?Password: sending incremental file listdeleting del.txt./?sent 149 bytes received 31 bytes ?18.95 bytes/sectotal size is 0 speedup is 0.00[root@targetpc backup]# ls1.txt ?2.txt ?3.txt aaa
四、免密同步
?
#免密[root@sourcepc data]# echo "123" > /etc/rsync.passwd[root@sourcepc data]# rsync -avz --delete /data/ rsyncuser@192.168.168.131::data --password-file=/etc/rsync.passwd #注意時--password 全稱來送數據了?ERROR: password file must not be other-accessiblersync error: syntax or usage error (code 1) at authenticate.c(196) [sender=3.1.2]#設置權限[root@sourcepc data]# chmod 600 /etc/rsync.passwd[root@sourcepc data]# rsync -avz --delete /data/ rsyncuser@192.168.168.131::data --password-file=/etc/rsync.passwd來送數據了?sending incremental file list?sent 142 bytes received 13 bytes ?23.85 bytes/sectotal size is 0 speedup is 0.00
五、pull模式
以上的同步過程都是服務端主動推送數據給目標主機,這里演示下目標主機主動拉取數據進行同步
?
# 服務端上配置/etc/rsyncd.conf文件port=873address = 192.168.168.130uid = rootgid = rootuse chroot = yesmax connections = 4pid file = /var/run/rsyncd.pidlock file = /var/run/rsync.locklog file = /var/log/rsyncd.log#motd file = /etc/rsyncd.motdhosts allow = 192.168.168.0/24[data]path = /datacomment = dataread only = falselist = yesauth users = rsyncusersecrets file = /etc/rsync.passwd# 認證文件[root@sourcepc log]# cat /etc/rsync.passwdrsyncuser:123# 服務端上啟動rsync[root@sourcepc log]# systemctl start rsyncd[root@sourcepc log]# systemctl status rsyncd● rsyncd.service - fast remote file copy program daemonLoaded: loaded (/usr/lib/systemd/system/rsyncd.service; disabled; vendor preset: disabled)Active: active (running) since 四 2025-06-19 20:35:23 CST; 2s agoMain PID: 2097 (rsync)CGroup: /system.slice/rsyncd.service└─2097 /usr/bin/rsync --daemon --no-detach?6月 19 20:35:23 sourcepc systemd[1]: Started fast remote file copy program daemon.# 目標主機上拉取數據[root@client data]# rsync -avz rsyncuser@192.168.168.130::data /dataPassword: receiving incremental file list./1.txt2.txt3.txtaaa12/bbb/ccc/?sent 100 bytes received 305 bytes ?162.00 bytes/sectotal size is 0 speedup is 0.00[root@client data]# ls1.txt ?2.txt ?3.txt aaa12 backup bbb ccc
六、rsync+sersync 實現數據實時同步push
?#在需要備份主機處 安裝sersync 使用的外部安裝包#使用rz 調入安裝包[root@sourcepc ~]# rz[root@sourcepc ~]# lsanaconda-ks.cfg ? ? ? ? ? ? ? ? info.shceph-release-1-1.el7.noarch.rpm sersync2.5.4_64bit_binary_stable_final.tar.gz#解壓[root@sourcepc ~]# tar -xf sersync2.5.4_64bit_binary_stable_final.tar.gz[root@sourcepc ~]# lsanaconda-ks.cfg ? ? ? ? ? ? ? ? GNU-Linux-x86 sersync2.5.4_64bit_binary_stable_final.tar.gzceph-release-1-1.el7.noarch.rpm info.sh#進行配置[root@sourcepc ~]# cd GNU-Linux-x86/[root@sourcepc GNU-Linux-x86]# lsconfxml.xml sersync2[root@sourcepc GNU-Linux-x86]# vim confxml.xml #修改的部分</filter><inotify><delete start="true"/><createFolder start="true"/><createFile start="true"/><closeWrite start="true"/><moveFrom start="true"/><moveTo start="true"/><attrib start="true"/><modify start="true"/></inotify>?<sersync><localpath watch="/data"><remote ip="192.168.168.131" name="data"/><!--<remote ip="192.168.8.39" name="tongbu"/>--><!--<remote ip="192.168.8.40" name="tongbu"/>--></localpath><rsync><commonParams params="-artuz"/><auth start="true" users="rsyncuser" passwordfile="/etc/rsync.passwd"/><userDefinedPort start="false" port="874"/><!-- port=874 --><timeout start="false" time="100"/><!-- timeout=100 --><ssh start="false"/></rsync>?#執行文件[root@sourcepc GNU-Linux-x86]# ./sersync2 -d -r -o ./confxml.xml set the system paramexecute:echo 50000000 > /proc/sys/fs/inotify/max_user_watchesexecute:echo 327679 > /proc/sys/fs/inotify/max_queued_eventsparse the command paramoption: -d ? ? run as a daemonoption: -r ? ? rsync all the local files to the remote servers before the sersync workoption: -o ? ? config xml name: ./confxml.xmldaemon thread num: 10parse xml config filehost ip : localhost ? ? host port: 8008will ignore the inotify createFile event daemon start,sersync run behind the console use rsync password-file :user is rsyncuserpasswordfile is ? ? ? ? /etc/rsync.passwdconfig xml parse successplease set /etc/rsyncd.conf max connections=0 Manuallysersync working thread 12 ?= 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads) Max threads numbers is: 22 = 12(Thread pool nums) + 10(Sub threads)please according your cpu ,use -n param to adjust the cpu rate------------------------------------------rsync the directory recursivly to the remote servers onceworking please wait...execute command: cd /data && rsync -artuz -R --delete ./ rsyncuser@192.168.168.131::data --password-file=/etc/rsync.passwd >/dev/null 2>&1 [root@sourcepc GNU-Linux-x86]# run the sersync: watch path is: /data?#測試[root@sourcepc data]# ls1.txt ?2.txt ?3.txt aaa[root@sourcepc data]# mkdir bbb?[root@targetpc backup]# ls1.txt ?2.txt ?3.txt aaa bbb??[root@sourcepc data]# ls1.txt ?2.txt ?3.txt aaa bbb ccc[root@sourcepc data]# mv aaa aaa12[root@targetpc backup]# ls1.txt ?2.txt ?3.txt aaa12 bbb ccc