一、服务器端、备份客户端安装 rsync 服务。
1、环境:
CentOS 6 主 IP:172.16.3.18 备 IP:172.16.3.19
2、安装 rsync 软件
#yum install rsync -y
3、iptables 开放 873 端口
#iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 873 -j ACCEPT #service iptables save #service iptables restart
二、server 服务端操作
IP: 172.16.3.18
1、需要备份的文件
web、service1、service2
2、rsync 配置文件示例(默认没有、需要收到创建)
# cat /etc/rsyncd.conf
uid = root gid = root use chroot = no max connections = 100 timeout = 30 strict modes = yes pid file = /var/run/rsyncd.pid lock file = /var/run/rsync.lock log file = /var/log/rsyncd.log hosts allow = 172.16.3.18 # hosts deny = * list = false auth users = user
# rsync同步使用的账户 secrets file = /etc/user.passwd
# 需要手动创建密码文件 [web] path = /home/web/ ignore errors read only = no write only = no [service1] path = /opt/service1/ ignore errors read only = no write only = no [service2] path = /opt/service2/ ignore errors read only = no write only = no
3、密码文件 /etc/user.passwd , (默认没有、需要收到创建)
# cat /etc/user.passwd
user:passwd
# 格式 用户:密码
4、启动默认守护进行:
#/usr/bin/rsync --daemon
5、检测进程
#ps -ef |grep rsync #netstat -ano |grep 873
二、客户端 bak 设置操作
备 IP:172.16.3.19
1.创建密码文件 /etc/user.passwd , 只写密码就可以。 (默认没有、需要收到创建)
#cat /etc/user.passwd
passwd
# 格式 密码
2、服务的目录 同步 到备机 bak 上:
1、备机 bak 同步服务端上的web /usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/user.passwd salt@172.16.3.18::service /opt/bak/web/
2、bak 同步 service1 服务 /usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/user.passwd salt@172.16.3.18::service1 /opt/bak/service1/
3、bak 同步 service2 服务 /usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/user.passwd salt@172.16.3.18::service2 /opt/bak/service2/ # 注意 被同步的bak目录 在后,如果在前就是备的同步主的了。 # 备机本地的 bak 同步到服务端; /usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/user.passwd /opt/bak/web salt@172.16.3.18::web
三、 设置定时备份
1、同步脚本
# vi /opt/script/rsync-user.sh /usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/user.passwd salt@172.16.3.18::service /opt/bak/web
2、定时同步(每周日零点进行同步)
crontab -e
0 0 * * 0 /opt/script/rsync-user.sh > /opt/script/rsync-user.txt