Centos7利用rsync實現文件同步

0x01 測試環境

CentOS 7.4 Rsync服務端:192.168.204.130
CentOS 7.4 Rsync客戶端:192.168.204.168

0x02 rsync同步方式

第一種方式:rsync經過ssh方式同步

一、Rsync服務端和客戶端都須要安裝rsynclinux

[root@localhost ~]# yum -y install rsync

二、使用web

前提:需知道遠程服務器開啓ssh端口和帳號密碼shell

A、推文件: centos

[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經過服務的方式同步

服務端配置:ssh

一、編輯配置文件/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

二、建立用戶密碼文件測試

echo "admin:123456" > /etc/rsyncd.secrets
chmod 600 /etc/rsyncd.secrets

三、建立提示信息文件:ui

 echo  "rsync  info"  >  /etc/rsyncd.motd

四、啓動服務:

rsync  --daemon
echo  "rsync  --daemon"  >>  /etc/rc.local

客戶端配置:

建立密碼文件(免密碼輸入):

echo  "123456"  >  /root/passwd
chmod  600  /root/passwd

拉取:

rsync -avz --password-file=/root/passwd  admin@192.168.204.130::common /tmp 

推送:

rsync -avz --password-file=/root/passwd /tmp/ admin@192.168.204.130::common

 

定時任務:

一、新建一個rsync.sh文件,在文件中寫入執行同步的命令:

rsync -avz --password-file=/root/passwd  admin@192.168.204.130::common /tmp  >/dev/null 2>&1

chmod 755  rsync.sh

二、執行命令:crontab -e

  在定時文件中寫入定時執行任務,實例以下:
       * * * * * /home/rsync.sh                 每分鐘執行一次同步腳本;
        0 * * * * /home/rsync.sh                 每小時執行一次同步腳本;
        0 0 * * * /home/rsync.sh                 天天零點執行一次同步腳本; 
        0 9,18 * * * /home/rsync.sh            天天的9AM和6PM執行一次同步腳本; 

 

TIPS:匿名訪問測試

列舉整個同步目錄或指定目錄:
rsync 10.0.0.12 ::
rsync 10.0.0.12 :: www /

下載文件或目錄到本地:
rsync – avz 10.0.0.12 :: WWW/  /var/tmp
rsync – avz 10.0.0.12 :: www/  /var/tmp

上傳本地文件到服務端:
rsync -avz webshell 10.0.0.12 :: WWW /

 

 

參考文章:

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

相關文章
相關標籤/搜索