rsync+crontab 全量备份
一、需求说明
为防止生产环境服务器瘫痪导致数据及文件丢失,需做如下备份操作:
192.168.62.6和192.168.62.7为我们的后台服务器,现需要做/app/chinatower/ 下的文件增量备份,备份存至192.168.62.10备份服务器上。
二、操作说明
1,环境准备
在三台机器上均执行以下操作:安装rsync工具,关闭防火墙和selinux
[root@localhost app2]# yum -y install rsync
[root@localhost chinatower]# systemctl stop firewalld
[root@localhost chinatower]# setenforce 0
2,修改配置文件/etc/rsyncd.conf 【在62.6/7服务器上】
[root@localhost chinatower]# vim /etc/rsyncd.conf
uid = root
gid = root
use chroot = no
max connections = 2
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[app1]
path = /app/
ignore errors
read only = false
list = false
auth users = backup
secrets file = /etc/rsync.passwd
对62.7也是一样的配置,除了模块名[app1]改成[app2] 其他不变
3,编辑密码文件并修改权限
[root@localhost chinatower]# vim /etc/rsync.passwd
[root@localhost chinatower]# cat /etc/rsync.passwd
backup:123qwe
chown root:root /etc/rsync.passwd
chmod 600 /etc/rsync.passwd #权限必须为600
4,启动rsync服务
[root@localhost ~]# chkconfig rsync on
[root@localhost ~]# systemctl restart xinetd
这时一般会报错(不报错则跳过),解决办法安装xinetd并编辑配置文件
[root@localhost ~]# yum -y install xinetd
[root@localhost 6]#vim /etc/xinetd.d/rsync
service rsync
{
disable = no
flags = IPv6
socket_type = stream
wait = no
user = root
server = /usr/bin/rsync
server_args = --daemon
log_on_failure += USERID
}
注意排版!
然后再次输入以下启动服务命令。
[root@localhost ~]# chkconfig rsync on
[root@localhost ~]# systemctl restart xinetd
接下来进入备份服务器62.10
备份服务器的配置很简单只需三步
1,创建密码文件,要与之前的配置名称相同
[root@localhost app1]# vim /etc/rsync.passwd
123qwe
加权限
chown root:root /etc/rsync.passwd
chmod 600 /etc/rsync.passwd #权限必须为600
2,检验
[root@localhost ~]# which is rsync
/usr/bin/which: no is in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin)
/usr/bin/rsync
[root@localhost app2]# /usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/rsync.passwd backup@192.168.62.7::app2 /app/app2
2,方法一:直接创建计划任务
[root@localhost app1]# crontab -e
[root@localhost app1]# crontab -l
0 0,6,12,18 * * * /usr/bin/rsync -vzrtopg --progress --password-file=/etc/rsync.passwd backup@192.168.62.6::app1 /app/app1
0 0,6,12,18 * * * /usr/bin/rsync -vzrtopg --progress --password-file=/etc/rsync.passwd backup@192.168.62.7::app2 /app/app2
方法二,计划任务+脚本
1,写脚本(写两个注意模块名的修改)
[root@localhost app2]# vim /app/backup1.sh
#!/bin/bash
systemctl stop firewalld
setenforce 0
/usr/bin/rsync -vzrtopg --progress --password-file=/etc/rsync.passwd backup@192.168.62.7::app2 /app/app2
SYC_STAT=$?
if [ "$SYC_STAT" != 0 ];then
echo"app2 backup error!"
echo $(date +%Y"-"%m"-"%d" "%H":"%M":"%S) ' backup error' >> /var/log/rsyncd.log
else
echo "app2 backup ok!"
echo $(date +%Y"-"%m"-"%d" "%H":"%M":"%S) ' backup ok' >> /var/log/rsyncd.log
fi
systemctl start firewalld
[root@localhost app2]# vim /app/backup2.sh #配置同上,注意修改名称!
2,加权限
chmod +x /app/backup1.sh #备份APP1
chmod +x /app/backup2.sh #备份APP2
3,加入计划任务
[root@localhost app2]# crontab -e
0 0,6,12,18 * * * /app/backup1.sh #备份APP1
0 0,6,12,18 * * * /app/backup2.sh #备份APP2
为了以后用户权限,建议增加下面这条命令
[root@localhost app]# chown -R appuser.appuser /app