一、目標
centos7下Rsync文件同步的配置
二、平臺
centos7.6,rsync.x86_64 0:3.1.2-6.el7_6.1
三、解析
Rsync是一個數據鏡像備份軟件。
Rsync主配置文件:/etc/rsyncd.conf
四、服務端的安裝設置
1.安裝rsync服務端軟件
[root@xserver ~]# yum install rsync -y
2.配置rsync配置文件
[root@xserver rsyncFolder]# vim /etc/rsyncd.conf 在配置文件中追加以下內容:
#設置服務器信息提示文件名稱,在這個文件rsyncd.moted中寫上提示信息
motd file = /etc/rsyncd.moted
#開啓Rsync數據傳輸日誌功能
transfer logging = yes
#日誌文件名
log file = /var/run/rsyncd.log
#Rsync進程號保存文件名
pid file = /var/run/rsyncd.pid
#設置鎖文件名稱
lock file = /var/run/rsyncd.lock
#Rsync服務端口
port 873
#Rsync偵聽地址(肯定是本機的ip地址咯)
address = 10.100.100.210
#設置進行數據傳輸時所用到的用戶名和組
uid = nobody
gid = nobody
use chroot = no
#這裏設置客戶端是否有可寫權限?yes代表可寫。
read only = yes
#設置Rsync的最大併發連接數
max connections = 10
# 同步文件夾名
[rsyncFolder]
#同步文件夾描述
comment = kahn's rsync folder
#同步文件夾路徑
path = /rsyncFolder
ignore errors
#exclude = dir
#開啓下列用戶能訪問本Rsync服務,這非常重要。
auth users = kahn,xRsync1,serena
#訪問Rsync的用戶名和密碼文件存放在什麼地方
secrets file = /etc/rsyncd.secrets
#允許哪些主機或網段訪問
hosts allow = 10.100.100.0/255.255.255.0
#拒絕哪些主機訪問
hosts deny = *
#客戶端請求顯示模塊列表是,本模塊名稱是否顯示,默認爲True,
list = false
3.創建訪問Rsync的用戶名和密碼文件
[root@xserver rsyncFolder]# echo "serena:x3721" > /etc/rsyncd.secrets
[root@xserver rsyncFolder]# echo "xRsync1:x3690" >> /etc/rsyncd.secrets
[root@xserver rsyncFolder]# echo "kahn:x123456" >> /etc/rsyncd.secrets
[root@xserver rsyncFolder]# chmod 600 /etc/rsyncd.secrets
[root@xserver rsyncFolder]#
[root@xserver rsyncFolder]# ll /etc/rsy*
-rw-r--r--. 1 root root 1785 Sep 2 07:45 /etc/rsyncd.conf
-rw-r--r--. 1 root root 458 Sep 2 07:18 /etc/rsyncd.conf.bak
-rw-------. 1 root root 40 Sep 2 07:48 /etc/rsyncd.secrets
-rw-r--r--. 1 root root 3232 Oct 30 2018 /etc/rsyslog.conf
/etc/rsyslog.d:
total 4
-rw-r--r--. 1 root root 49 Oct 30 2018 listen.conf
[root@xserver rsyncFolder]#
[root@xserver rsyncFolder]# cat /etc/rsyncd.secrets
serena:x3721
xRsync1:x3690
kahn:x123456
4.給Rsync服務器設置個歡迎信息。
[root@xserver rsyncFolder]# echo "Hello world ! this is kahn's Rsync Server" > /etc/rsyncd.motd
5.啓動Rsync服務,並設置Rsync服務開機自啓。
啓動Rsync服務:[root@xserver rsyncFolder]# rsync --daemon
設置Rsync開機自啓:[root@xserver]# echo "/usr/bin/rsync --daemon" >> /etc/rc.local
6.防火牆放開Rsync服務並重啓,
[root@xserver rsyncFolder]# firewall-cmd --permanent --add-port=873/tcp
success
[root@xserver rsyncFolder]# firewall-cmd --reload
success
[root@xserver rsyncFolder]#
五、Rsync客戶端的設置
1.安裝rsync軟件[root@xclient ~]# yum install rsync -y
有的已默認安裝了,可以用命令看下版本信息[root@xserver rsyncFolder]# rsync --version
2.創建一個文件夾用於同步來自服務器的文件。[root@xclient ~]# mkdir /xRsyncBAK
3.連接到Rsync服務器
[root@xclient ~]# rsync -vzrtopg --progress [email protected]::rsyncFolder /xRsyncBAK/
Password:
receiving incremental file list
./
hello.txt
36 100% 35.16kB/s 0:00:00 (xfr#1, to-chk=0/2)
sent 46 bytes received 146 bytes 42.67 bytes/sec
total size is 36 speedup is 0.19
[root@xclient ~]#
遇到一個小坑,執行諸如上面的代碼同步東西的時候報錯了,如下
[root@xclient ~]# rsync -avz [email protected]::rsyncFolder /xRsyncBAK/
rsync: failed to connect to 10.100.100.210 (10.100.100.210): Connection refused (111)
rsync error: error in socket IO (code 10) at clientserver.c(125) [Receiver=3.1.2]
[root@xclient ~]#
這樣是說明服務端的rsync服務沒啓動造成的。在服務器端執行rsync --daemon即可解決。
4.此時,僅僅只能同步一次,即敲一次命令[root@xclient ~]# rsync -vzrtopg --progress [email protected]::rsyncFolder /xRsyncBAK/ 同步一次,不敲命令不同步。或者這就叫手工同步。不信?下面再同步一次給你看
[root@xclient xRsyncBAK]# rsync -vzrtopg --progress [email protected]::rsyncFolder /xRsyncBAK/
Password:
receiving incremental file list
./
server1.txt
0 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=0/3)
sent 46 bytes received 136 bytes 40.44 bytes/sec
total size is 36 speedup is 0.20
[root@xclient xRsyncBAK]# ls
clientCreate hello.txt server1.txt
[root@xclient xRsyncBAK]#
六、在服務器端上執行,讓Rsync通過ssh從服務器端傳遞文件到客戶端
1.先將服務器和客戶端的主機名修改一下,便於我們寫rsync命令
[root@xserver ~]# hostnamectl set-hostname xserver 在服務器上執行該命令,即將服務器的主機名命名爲xserver
[root@xclient ~]# hostnamectl set-hostname xclient 在客戶端上執行該命令,即將客戶端主機名命名爲xclient
2.修改服務器端和客戶端的hosts文件修改一下,便於寫rsync命令
root@xclient ~]# vim /etc/hosts 客戶端和服務器端的hosts文件都追加以下兩行。
10.100.100.210 xserver
10.100.100.230 xclient
3.在服務器端使用rsync傳輸文件到客戶端
解析:-t複製時間,*就是普通的*,*就是將服務器端當前文件夾下的所有文件,xclient客戶端的主機名,/xRsyncBAK客戶端的物理文件夾路徑、目標文件夾路徑。 然後輸入yes回車,再輸入客戶端的root密碼回車。
[root@xserver ~]# rsync -t * xclient:/xRsyncBAK
The authenticity of host 'xclient (10.100.100.230)' can't be established.
ECDSA key fingerprint is SHA256:MScEQUpZIw7acLtgX6hFSlaCp+XbJTbOVmffaJB8KUA.
ECDSA key fingerprint is MD5:45:6f:f2:d3:39:c7:87:9e:2d:11:aa:9d:54:17:3d:09.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'xclient,10.100.100.230' (ECDSA) to the list of known hosts.
root@xclient's password:
[root@xserver ~]#
執行完畢後,在客戶端的相應文件夾下,你會發現文件已經同步過去了。這就是通過ssh同步文件的。
再注意一下,上面是不遞歸拷貝,要想遞歸拷貝加參數-r。使用看man手冊你會發現一大大坨參數。
七、使用rsync對本機不通文件夾的文件進行同步
1.將root家目錄下的所有文件遞歸同步到/home/kahn/xfolder下去。[root@xserver ~]# rsync -avzr * /home/kahn/xfolder/
[root@xserver ~]# tree ~
/root
├── anaconda-ks.cfg
├── original-ks.cfg
└── testServerFolder
└── server1.txt
1 directory, 3 files
[root@xserver ~]#
[root@xserver ~]# tree /home/kahn/xfolder/
/home/kahn/xfolder/
0 directories, 0 files
[root@xserver ~]#
[root@xserver ~]# rsync -avzr * /home/kahn/xfolder/
sending incremental file list
anaconda-ks.cfg
original-ks.cfg
testServerFolder/
testServerFolder/server1.txt
sent 2,396 bytes received 81 bytes 4,954.00 bytes/sec
total size is 5,008 speedup is 2.02
[root@xserver ~]#
[root@xserver ~]# tree /home/kahn/xfolder/
/home/kahn/xfolder/
├── anaconda-ks.cfg
├── original-ks.cfg
└── testServerFolder
└── server1.txt
1 directory, 3 files
[root@xserver ~]#
八、在客戶端上執行,讓Rsync通過ssh從服務器端傳遞文件到客戶端
1.下面的命令意思是,在客戶端上執行rsync,歸檔同步,從服務器端的/home/kahn/xfolder文件夾及文件夾本身的所有內容,注意是包含xolder文件夾本身,同步至本機(客戶機)的/tmp/kahnclient/文件夾下。
[root@xclient ~]# rsync -avzr xserver:/home/kahn/xfolder /tmp/kahnclient/
The authenticity of host 'xserver (10.100.100.210)' can't be established.
ECDSA key fingerprint is SHA256:Dhrir3IdiKyNSakp2tJ2of8RdvPpIgqn9u4NliLxuLg.
ECDSA key fingerprint is MD5:8b:27:cb:fb:5e:e7:12:96:49:37:fb:e0:5c:ba:8d:bc.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'xserver,10.100.100.210' (ECDSA) to the list of known hosts.
root@xserver's password:
receiving incremental file list
xfolder/
xfolder/anaconda-ks.cfg
xfolder/original-ks.cfg
xfolder/testServerFolder/
xfolder/testServerFolder/server1.txt
sent 93 bytes received 2,449 bytes 299.06 bytes/sec
total size is 5,008 speedup is 1.97
[root@xclient ~]#
[root@xclient ~]# ls /tmp/kahnclient/
xfolder
[root@xclient ~]# ls /tmp/kahnclient/xfolder/
anaconda-ks.cfg original-ks.cfg testServerFolder
[root@xclient ~]# ls /tmp/kahnclient/xfolder/testServerFolder/
server1.txt
[root@xclient ~]#
哎,本意想是拷貝服務器端的/home/kahn/xfolder下的所有文件(不包含xfolder本身),這樣的話就得這樣寫/home/kahn/xfolder/*
八、在客戶端上,查看服務器端的同步列表
1.首先得將服務器端的rsync配置文件/etc/rsyncd.conf中的list = false改爲true
2.在客戶端執行命令
命令解析,xRsync1就是配置的rsync虛擬用戶,10.100.100.210就是rsync服務器端。回車即可。
可以看到服務器端僅僅“共享”了一個文件夾叫rsyncFolder,後面的是描述。注意哦,無需輸入密碼,任何一個虛擬用戶都行。
[root@xclient ~]# rsync --list-only [email protected]::
rsyncFolder kahn's rsync folder
[root@xclient ~]#