Centos7利用rsync實現文件同步
測試環境:
CentOS 7.4 Rsync服務端:192.168.99.112
CentOS 7.4 Rsync客戶端:192.168.99.136
第一種方式:rsync通過ssh方式同步
1、Rsync服務端和客戶端都需要安裝rsync
[root@localhost ~]# yum -y install rsync
2、使用
前提:需知道遠程服務器開啓ssh端口和賬號密碼
A、推文件:
[root@localhost tmp]# rsync -av /etc/passwd 192.168.204.168:/tmp/passwd.txt
B、拉文件
[root@localhost tmp]# rsync -av 192.168.204.168:/tmp/passwd.txt /tmp/test.txt
#指定ssh 端口
[root@localhost tmp]# rsync -av -e "ssh -p 22" 192.168.204.168:/tmp/passwd.txt /tmp/a.txt
第二種方式:rsync通過服務的方式同步
服務端配置:
1、編輯配置文件/etc/rsyncd.conf
motd file = /etc/rsyncd.motd transfer logging = yes log file = /var/log/rsyncd.log port = 873 address = 192.168.204.130 uid = nobody gid = nobody use chroot = no read only = no max connections = 10 [common] comment = rsync info path = /tmp ignore errors auth users = admin secrets file = /etc/rsyncd.secrets hosts allow = 192.168.204.0/255.255.255.0 hosts deny = * list = false
2、創建用戶密碼文件
echo "admin:123456" > /etc/rsyncd.secrets
chmod 600 /etc/rsyncd.secrets
3、創建提示信息文件:
echo "rsync info" > /etc/rsyncd.motd
4、啓動服務:
rsync --daemon
echo "rsync --daemon" >> /etc/rc.local
客戶端配置:
創建密碼文件(免密碼輸入):
echo "123456" > /root/passwd
chmod 600 /root/passwd
拉取:
rsync -avz --password-file=/root/passwd [email protected]::common /tmp
推送:
rsync -avz --password-file=/root/passwd /tmp/ [email protected]::common
定時任務:
1、新建一個rsync.sh文件,在文件中寫入執行同步的命令:
rsync -avz --password-file=/root/passwd [email protected]::common /tmp >/dev/null 2>&1
2、chmod 755 rsync.sh
3、執行命令:crontab -e
在定時文件中寫入定時執行任務,實例如下: * * * * * /home/rsync.sh 每分鐘執行一次同步腳本; 0 * * * * /home/rsync.sh 每小時執行一次同步腳本; 0 0 * * * /home/rsync.sh 每天零點執行一次同步腳本; 0 9,18 * * * /home/rsync.sh 每天的9AM和6PM執行一次同步腳本;
參考文章:
centos7安裝配置rsync
https://blog.51cto.com/12173069/2069243
日常運維--rsync同步工具
https://my.oschina.net/ccLlinux/blog/1859116
http://ju.outofmemory.cn/entry/42150
linux下匿名方式通過rsync同步文件
Linux下rsync的安裝及簡單使用
https://blog.51cto.com/13917261/2285348?source=dra
CentOS 7安裝部署Rsync數據同步服務器
https://www.linuxidc.com/Linux/2017-06/144757.htm