实战rsync全网数据备份
Posted ahtornado
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了实战rsync全网数据备份相关的知识,希望对你有一定的参考价值。
linux-node1,linux-node2 上数据通过推的方式,备份至backup服务器
备份服务器端:
1.backup 服务器上创建 rsyncd.conf 文件并编辑
[[email protected] ~]# 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条)
[[email protected] ~]# rsync --daemon #启动rsync 服务
[[email protected] ~]# 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
[[email protected] ~]# 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
[[email protected] ~]# 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 用户(不创建家目录)
[[email protected] ~]# useradd rsync -s /sbin/nologin -M
4. 创建备份目录 data ,share
[[email protected] ~]# mkdir /data /share
[[email protected] ~]# chown -R rsync.rsync /data #修改目录的访问权限
[[email protected] ~]# chown -R rsync.rsync /share
[[email protected] ~]# ls -ld /data/
drwxr-xr-x 2 rsync rsync 4096 Aug 25 14:32 /data/
[[email protected] ~]# ls -ld /share/
drwxr-xr-x 2 rsync rsync 4096 Aug 25 14:32 /share/
5.创建密码文件并查看
[[email protected] ~]# echo "rsync_backup:123456" >/etc/rsync.password
[[email protected] ~]# cat /etc/rsync.password
rsync_backup:123456
6.修改密码文件的查看权限
[[email protected] ~]# chmod 600 /etc/rsync.password
[[email protected] ~]# ll /etc/rsync.password
-rw------- 1 root root 20 Aug 25 15:08 /etc/rsync.password
[[email protected] ~]#
7.把rsync 服务加入开机自启动
[[email protected] ~]# which rsync
/usr/bin/rsync
[[email protected] ~]# echo "/usr/bin/rsync --daemon" >>/etc/rc.local
[[email protected] ~]# 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
[[email protected] ~]#
需要备份的客户端服务器:
客户端1:
[[email protected] ~]# echo "123456" >/etc/rsync.password
[[email protected] ~]# chmod 600 /etc/rsync.password
[[email protected] ~]# ll /etc/rsync.password
-rw------- 1 root root 7 Aug 25 15:26 /etc/rsync.password
[[email protected] ~]# cat /etc/rsync.password
123456
[[email protected] ~]#
#创建备份资料
[[email protected] ~]# mkdir /data
[[email protected] ~]# cd /data
[[email protected] data]# touch {1..5}.txt
[[email protected] 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
#推文件到备份服务器
[[email protected] 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:
[[email protected] ~]# echo "123456" >/etc/rsync.password
[[email protected] ~]# chmod 600 /etc/rsync.password
[[email protected] ~]# ll /etc/rsync.password
-rw------- 1 root root 7 Aug 25 15:26 /etc/rsync.password
[[email protected] ~]# cat /etc/rsync.password
123456
[[email protected] ~]#
#创建备份资料
[[email protected] ~]# mkdir /share
[[email protected] ~]# cd /share
[[email protected] share]# touch {a..f}.txt
[[email protected] 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
[[email protected] share]#
#推文件到备份服务器
[[email protected] 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
#备份服务器上查看是否备份成功
[[email protected] ~]# 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
[[email protected] ~]# 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)
[[email protected] share]# vi a.txt
dgadga
agafhfhaja
[[email protected] share]# rm -rf f.txt
[[email protected] 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
[[email protected] 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
[[email protected] share]#
备份服务器端查看:
[[email protected] 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
###########################################################
故障排除
[[email protected] 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]
原因分析:
[[email protected] 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
[[email protected] data]# telnet 10.89.7.9 873
Trying 10.89.7.9...
telnet: connect to address 10.89.7.9: Connection refused
考虑防火墙问题:
服务器端和客户端关闭防火墙
[[email protected] ~]# /etc/init.d/iptables stop
[[email protected] ~]# /etc/init.d/iptables status
iptables: Firewall is not running.
------------------
[[email protected] 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 ]
[[email protected] data]# /etc/init.d/iptables stop
[[email protected] data]# /etc/init.d/iptables status
iptables: Firewall is not running.
#还有种可能是服务器端rsync服务没有启动,此时启动服务器端的rsync服务即可。
[[email protected] share]# kill `cat /var/run/rsyncd.pid`
[[email protected] share]# ps -ef|grep rsync
root 6537 2881 0 17:56 pts/0 00:00:00 grep rsync
[[email protected] share]# lsof -i :873
#备份出错
[[email protected] 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全网数据备份的主要内容,如果未能解决你的问题,请参考以下文章
Linux-Rsync项目实战(详细) 备份全网服务器数据生产架构方案
网站集群架构实战(LVS负载均衡Nginx代理缓存Nginx动静分离Rsync+Inotify全网备份Zabbix自动注册全网监控)--技术流ken