實戰rsync全網數據備份
linux-node1,linux-node2 上數據通過推的方式,備份至backup服務器
備份服務器端:
1.backup 服務器上創建 rsyncd.conf 文件並編輯
[root@backup ~]# vi /etc/rsyncd.conf
#Created by alvin 20:06 2018-7-5
##rsync.conf start##
uid = rsync
gid = rsync
use chroot = no
max connections = 2000
timeout = 300
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
ignore errors
read only = false
list = false
hosts allow = 10.89.7.0/24
host deny = 0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync.password
##################################
[data]
comment = backup data by alvin 2018-7-6
path = /data/
[share]
comment = backup share by alvin 2018-7-6
path = /share/
#rsync_config___________________end
2.啟動rsync 服務,並查看服務是否啟動了(以下3條命令任選1條)
[root@backup ~]# rsync --daemon #啟動rsync 服務
[root@backup ~]# netstat -lntup|grep rsync
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 5847/rsync
tcp 0 0 :::873 :::* LISTEN 5847/rsync
[root@backup ~]# ps -ef | grep rsync
root 5847 1 0 14:43 ? 00:00:00 rsync --daemon
root 5855 2881 0 14:44 pts/0 00:00:00 grep rsync
[root@backup ~]# lsof -i:873
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
rsync 5847 root 3u IPv4 22567 0t0 TCP *:rsync (LISTEN)
rsync 5847 root 5u IPv6 22568 0t0 TCP *:rsync (LISTEN)
3.創建rsync 用戶(不創建家目錄)
[root@backup ~]# useradd rsync -s /sbin/nologin -M
4. 創建備份目錄 data ,share
[root@backup ~]# mkdir /data /share
[root@backup ~]# chown -R rsync.rsync /data #修改目錄的訪問權限
[root@backup ~]# chown -R rsync.rsync /share
[root@backup ~]# ls -ld /data/
drwxr-xr-x 2 rsync rsync 4096 Aug 25 14:32 /data/
[root@backup ~]# ls -ld /share/
drwxr-xr-x 2 rsync rsync 4096 Aug 25 14:32 /share/
5.創建密碼文件並查看
[root@backup ~]# echo "rsync_backup:123456" >/etc/rsync.password
[root@backup ~]# cat /etc/rsync.password
rsync_backup:123456
6.修改密碼文件的查看權限
[root@backup ~]# chmod 600 /etc/rsync.password
[root@backup ~]# ll /etc/rsync.password
-rw------- 1 root root 20 Aug 25 15:08 /etc/rsync.password
[root@backup ~]#
7.把rsync 服務加入開機自啟動
[root@backup ~]# which rsync
/usr/bin/rsync
[root@backup ~]# echo "/usr/bin/rsync --daemon" >>/etc/rc.local
[root@backup ~]# cat /etc/rc.local #檢查是否加入
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don‘t
# want to do the full Sys V style init stuff.
touch /var/lock/subsys/local
/usr/bin/rsync --daemon
[root@backup ~]#
需要備份的客戶端服務器:
客戶端1:
[root@linux-node1 ~]# echo "123456" >/etc/rsync.password
[root@linux-node1 ~]# chmod 600 /etc/rsync.password
[root@linux-node1 ~]# ll /etc/rsync.password
-rw------- 1 root root 7 Aug 25 15:26 /etc/rsync.password
[root@linux-node1 ~]# cat /etc/rsync.password
123456
[root@linux-node1 ~]#
#創建備份資料
[root@linux-node1 ~]# mkdir /data
[root@linux-node1 ~]# cd /data
[root@linux-node1 data]# touch {1..5}.txt
[root@linux-node1 data]# ll
total 16
-rw-r--r-- 1 root root 0 Aug 25 15:36 1.txt
-rw-r--r-- 1 root root 0 Aug 25 15:36 2.txt
-rw-r--r-- 1 root root 0 Aug 25 15:36 3.txt
-rw-r--r-- 1 root root 0 Aug 25 15:36 4.txt
-rw-r--r-- 1 root root 0 Aug 25 15:36 5.txt
#推文件到備份服務器
[root@linux-node1 data]# rsync -avz /data/ [email protected]::data --password-file=/etc/rsync.password
sending incremental file list
./
1.txt
2.txt
3.txt
4.txt
5.txt
sent 263 bytes received 106 bytes 246.00 bytes/sec
total size is 0 speedup is 0.00
客戶端2:
[root@linux-node2 ~]# echo "123456" >/etc/rsync.password
[root@linux-node2 ~]# chmod 600 /etc/rsync.password
[root@linux-node2 ~]# ll /etc/rsync.password
-rw------- 1 root root 7 Aug 25 15:26 /etc/rsync.password
[root@linux-node2 ~]# cat /etc/rsync.password
123456
[root@linux-node2 ~]#
#創建備份資料
[root@linux-node2 ~]# mkdir /share
[root@linux-node2 ~]# cd /share
[root@linux-node2 share]# touch {a..f}.txt
[root@linux-node2 share]# ll
total 0
-rw-r--r-- 1 root root 0 Aug 25 15:37 a.txt
-rw-r--r-- 1 root root 0 Aug 25 15:37 b.txt
-rw-r--r-- 1 root root 0 Aug 25 15:37 c.txt
-rw-r--r-- 1 root root 0 Aug 25 15:37 d.txt
-rw-r--r-- 1 root root 0 Aug 25 15:37 e.txt
-rw-r--r-- 1 root root 0 Aug 25 15:37 f.txt
[root@linux-node2 share]#
#推文件到備份服務器
[root@linux-node2 share]# rsync -avz /share/ [email protected]::share --password-file=/etc/rsync.password
sending incremental file list
./
a.txt
b.txt
c.txt
d.txt
e.txt
f.txt
sent 305 bytes received 125 bytes 860.00 bytes/sec
total size is 0 speedup is 0.00
#備份服務器上查看是否備份成功
[root@backup ~]# ll /data
total 0
-rw-r--r-- 1 rsync rsync 0 Aug 25 15:36 1.txt
-rw-r--r-- 1 rsync rsync 0 Aug 25 15:36 2.txt
-rw-r--r-- 1 rsync rsync 0 Aug 25 15:36 3.txt
-rw-r--r-- 1 rsync rsync 0 Aug 25 15:36 4.txt
-rw-r--r-- 1 rsync rsync 0 Aug 25 15:36 5.txt
[root@backup ~]# ll /share
total 0
-rw-r--r-- 1 rsync rsync 0 Aug 25 15:37 a.txt
-rw-r--r-- 1 rsync rsync 0 Aug 25 15:37 b.txt
-rw-r--r-- 1 rsync rsync 0 Aug 25 15:37 c.txt
-rw-r--r-- 1 rsync rsync 0 Aug 25 15:37 d.txt
-rw-r--r-- 1 rsync rsync 0 Aug 25 15:37 e.txt
-rw-r--r-- 1 rsync rsync 0 Aug 25 15:37 f.txt
#差異備份(編輯a.txt,刪除f.txt)
[root@linux-node2 share]# vi a.txt
dgadga
agafhfhaja
[root@linux-node2 share]# rm -rf f.txt
[root@linux-node2 share]# ll
total 8
-rw-r--r-- 1 root root 19 Aug 25 17:31 a.txt
-rw-r--r-- 1 root root 0 Aug 25 17:31 b.txt
-rw-r--r-- 1 root root 10 Aug 25 17:33 c.txt
-rw-r--r-- 1 root root 0 Aug 25 15:37 d.txt
-rw-r--r-- 1 root root 0 Aug 25 15:37 e.txt
[root@linux-node2 share]# rsync -avz --delete /share/ [email protected]::share --password-file=/etc/rsync.password #同步時加上 --delete 參數,完全同步。
sending incremental file list
./
deleting f.txt
a.txt
sent 167 bytes received 36 bytes 406.00 bytes/sec
total size is 35 speedup is 0.17
[root@linux-node2 share]#
備份服務器端查看:
[root@backup share]# ll
total 8
-rw-r--r-- 1 rsync rsync 25 Aug 25 17:49 a.txt
-rw-r--r-- 1 rsync rsync 0 Aug 25 17:31 b.txt
-rw-r--r-- 1 rsync rsync 0 Aug 25 17:33 c.txt
-rw-r--r-- 1 rsync rsync 0 Aug 25 15:37 d.txt
-rw-r--r-- 1 rsync rsync 0 Aug 25 15:37 e.txt
###########################################################
故障排除
[root@linux-node1 data]# rsync -avz /data/ [email protected]::data --password-file=/etc/rsync.password
rsync: failed to connect to 10.89.7.9: Connection refused (111)
rsync error: error in socket IO (code 10) at clientserver.c(124) [sender=3.0.6]
原因分析:
[root@linux-node1 data]# ping 10.89.7.9
PING 10.89.7.9 (10.89.7.9) 56(84) bytes of data.
64 bytes from 10.89.7.9: icmp_seq=1 ttl=64 time=1002 ms
64 bytes from 10.89.7.9: icmp_seq=2 ttl=64 time=0.279 ms
64 bytes from 10.89.7.9: icmp_seq=3 ttl=64 time=0.424 ms
^C
--- 10.89.7.9 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2422ms
rtt min/avg/max/mdev = 0.279/334.435/1002.603/472.466 ms, pipe 2
[root@linux-node1 data]# telnet 10.89.7.9 873
Trying 10.89.7.9...
telnet: connect to address 10.89.7.9: Connection refused
考慮防火墻問題:
服務器端和客戶端關閉防火墻
[root@backup ~]# /etc/init.d/iptables stop
[root@backup ~]# /etc/init.d/iptables status
iptables: Firewall is not running.
------------------
[root@linux-node1 data]# /etc/init.d/iptables stop
iptables: Setting chains to policy ACCEPT: nat mangle raw f[ OK ]
iptables: Flushing firewall rules: [ OK ]
iptables: Unloading modules: [ OK ]
[root@linux-node1 data]# /etc/init.d/iptables stop
[root@linux-node1 data]# /etc/init.d/iptables status
iptables: Firewall is not running.
#還有種可能是服務器端rsync服務沒有啟動,此時啟動服務器端的rsync服務即可。
[root@backup share]# kill `cat /var/run/rsyncd.pid`
[root@backup share]# ps -ef|grep rsync
root 6537 2881 0 17:56 pts/0 00:00:00 grep rsync
[root@backup share]# lsof -i :873
#備份出錯
[root@linux-node2 share]# rsync -avz --delete /share/ [email protected]::share --password-file=/etc/rsync.password
rsync: failed to connect to 10.89.7.9: Connection refused (111)
rsync error: error in socket IO (code 10) at clientserver.c(124) [sender=3.0.6]
#此時服務端開啟rsync 服務即可。
實戰rsync全網數據備份