使用rsync同步服務器和客戶端的文件夾
- 實現目的
- 實驗準備
- 實驗操作步驟
- 服務器操作
- 關閉防火墻和SELINUX
- 安裝rsync
- 修改服務器配置文件/etc/rsync.conf
- 創建服務器備份文件的目錄
- 創建rsync系統運行的用戶
- 修改備份文件的所有者和所屬組
- 創建rsync.passwd
- 啟動rsync服務并進行驗證
- 客戶端
- 服務器驗證
- 結尾
實現目的
利用rsync實現遠程服務器和電腦文件夾的同步
實驗準備
本次演示均使用兩臺虛擬機做準備
服務器 | 192.168.218.136 |
---|---|
客戶端 | 192.168.218.132 |
實驗操作步驟
服務器操作
關閉防火墻和SELINUX
1??關閉防火墻
[root@sxs home]# systemctl stop firewalld
[root@sxs scripts]# systemctl disable firewalld
Removed "/etc/systemd/system/multi-user.target.wants/firewalld.service".
Removed "/etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service".
2??關閉SELINUX
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
setenforce 0: #立即生效
安裝rsync
#因為之前已經安裝過了,所以顯示這結果
[root@sxs scripts]# yum -y install rsync
CentOS Stream 9 - BaseOS 3.4 kB/s | 3.8 kB 00:01
CentOS Stream 9 - BaseOS 5.6 MB/s | 8.0 MB 00:01
CentOS Stream 9 - AppStream 3.5 kB/s | 4.2 kB 00:01
CentOS Stream 9 - AppStream 2.8 MB/s | 19 MB 00:06
CentOS Stream 9 - Extras packages 9.1 kB/s | 6.9 kB 00:00
軟件包 rsync-3.2.3-19.el9.x86_64 已安裝。
依賴關系解決。
無需任何處理。
完畢!
修改服務器配置文件/etc/rsync.conf
#有好幾個都很重要,而且還涉及到權限的問題
[root@sxs scripts]# cat /etc/rsyncd.conf |grep -v "^#\|^$"
uid = rsync #指定rsync進程以什么用戶身份在后臺運行,默認是nobody
gid = rsync #
use chroot = no #是否將用戶鎖定在家目錄下
max connections =100 #最大連接數
pid file = /var/run/rsyncd.pid #指定pid文件保存在哪里
timeout = 900 #訪問超時時間
lock file = /var/lock/rsync.lock #指定lock文件保存在哪里
log file = /var/log/rsync.log #指定log日志文件保存在哪里
[ test ] #模塊名稱,十分重要
path = /home/backup/ #客戶端推送文件過來的時候,需要保存在哪里
read only = false #文件目錄只能下載,不能上傳
hosts allow = 192.168.218.0/24 #可以訪問的IP地址,這是表示只有192.168.218.0的網段可以訪問
auth users = vuserback #指定訪問模塊需要使用的用戶名稱,這里的是虛擬用戶(不是存在于/etc/passwd)
secrets file = /home/rsync.passwd #訪問模塊的用戶密碼保存在哪里,很重要,而且這rsync.passwd文件的權限只能是600
list = false #設置是否可以顯示全部的模塊列表
創建服務器備份文件的目錄
#這一步:對應/etc/rsync.passwd配置文件下的: path = /home/backup/
[root@sxs scripts]# mkdir -p /home/backup
創建rsync系統運行的用戶
[root@sxs]# groupadd rsync[root@sxs]# useradd -r -s /sbin/nologin -g rsync rsync
修改備份文件的所有者和所屬組
#這和上文的配置文件的uid和gid對應上,之前做實驗的時候,發現所有者和所屬組是root的時候,在客戶端推送文件的時候一直報錯,等我uid和gid修改成rsync的時候以及備份文件修改成rsync:rsync的時候才可以,這也是一個大坑。大家注意
[root@sxs home]# chown rsync:rsync /home/backup/
[root@sxs home]# ll
drwxr-xr-x 2 rsync rsync 21 2月 28 18:46 backup
創建rsync.passwd
#這rsync.passwd的權限記得是600,十分重要的一個點
[root@sxs home]# touch rsync.passwd
[root@sxs home]# echo "vuserback:123" >rsync.passwd
[root@sxs home]# chmod 600 rsync.passwd
[root@sxs home]# cat rsync.passwd
vuserback:123
[root@sxs home]# ll
總用量 4
drwxr-xr-x 2 rsync rsync 21 2月 28 18:46 backup
-rw------- 1 root root 14 3月 2 00:40 rsync.passwd
啟動rsync服務并進行驗證
#rsync的端口是873,默認是873
[root@sxs home]# rsync --damon
[root@sxs home]# netstat -tnlp |grep rsync
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 48666/rsync
tcp6 0 0 :::873 :::* LISTEN 48666/rsync
客戶端
直接推送文件
#顯示下文的這樣子,就表示推送成功了[root@localhost /]# cd /scripts/
[root@localhost scripts]# ll
總用量 4
-rwxr--r-- 1 root root 24 2月 28 18:46 test.sh
[root@localhost scripts]# rsync -avz /scripts/ vuserback@192.168.218.136::test
Password: #對應客戶端的rsync.passwd文件的密碼
sending incremental file list
./
test.shsent 137 bytes received 46 bytes 73.20 bytes/sec
total size is 24 speedup is 0.13
[root@localhost scripts]#
服務器驗證
在服務器可以看到對應的文件了
[root@sxs home]# ll
總用量 4
drwxr-xr-x 2 rsync rsync 21 2月 28 18:46 backup
-rw------- 1 root root 14 3月 2 00:40 rsync.passwd
[root@sxs home]# ll ./backup/
總用量 4
-rwxr--r-- 1 rsync rsync 24 2月 28 18:46 test.sh
結尾
可能有的寫的不是很好,歡迎大家指出來