终极版rsync-VPS每日定时同步自动备份方法

需要备份的网站所在VPS为A,用于备份用的VPS为B,系统均为CentOS,
B定时备份A的数据。

一、VPS A 上面的具体部署
1. 安装 rsync

1
yum -y install rsync

把rsync加入开机启动

1
echo 'rsync --daemon' >> /etc/rc.d/rc.local

2. 设置rsync密码

1
2
echo '你的用户名:你的密码' > /etc/rsyncd.scrt
chmod 600 /etc/rsyncd.scrt

这里设置的用户名和密码,在VPS B上会用到

3. 配置rsync

1
vim /etc/rsyncd.conf

放入以下内容, #后面是注释

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
uid = root
gid = root
use chroot = no
read only = yes
max connections = 10

port = 873
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
#log file = /var/log/rsync.log # 偶不想记录log
log format = %t %a %m %f %b
syslog facility = local3
timeout = 300

[www]
path = /var/www/
comment = 21andy.com
ignore errors
read only = yes
list = no
auth users = andy
secrets file = /etc/rsyncd.scrt
#exclude = 21andy.com/new/cache/ #不需要备份的目录,我使用exclude from方法来排除
exclude from = /etc/rsync_exclude.txt
hosts allow = 备份服务器的IP
hosts deny = *

4. 排除不备份的目录

1
vim /etc/rsync_exclude.txt

输入不备份的目录,每行一个,不可以用绝对路径,而必须用上面配置文件中path的相对路径,如

1
2
xiaohost.com/new/cache/
xiaohost.com/manual/

5. 制作一个重启rsync的脚本

1
vim /root/rsyncd_restart.sh

放入以下内容

1
2
3
4
kill -9 `cat /var/run/rsyncd.pid`
rm -f /var/run/rsyncd.pid
rm -f /var/run/rsyncd.lock
rsync --daemon

然后改权限

1
2
chmod 600 /root/rsyncd_restart.sh
chmod +x /root/rsyncd_restart.sh

现在直接用 /root/rsyncd_restart.sh 来重新启动 rsync 进程

6. 备份 MySQL 的脚本
此脚本可同时备份多个数据库,并进行gzip压缩,按日期目录保存,3天之前的备份将被自动删除

1
vim /root/mysql_backup.sh

写入以下内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/bin/bash

# 以下配置信息请自己修改
mysql_user="USER" #MySQL备份用户
mysql_password="PASSWORD" #MySQL备份用户的密码
mysql_host="localhost"
mysql_port="3306"
mysql_charset="utf8" #MySQL编码
backup_db_arr=("db1" "db2") #要备份的数据库名称,多个用空格分开隔开 如("db1" "db2" "db3")
backup_location=/var/www/mysql  #备份数据存放位置,末尾请不要带"/",此项可以保持默认,程序会自动创建文件夹
expire_backup_delete="ON" #是否开启过期备份删除 ON为开启 OFF为关闭
expire_days=3 #过期时间天数 默认为三天,此项只有在expire_backup_delete开启时有效

# 本行开始以下不需要修改
backup_time=`date +%Y%m%d%H%M`  #定义备份详细时间
backup_Ymd=`date +%Y-%m-%d` #定义备份目录中的年月日时间
backup_3ago=`date -d '3 days ago' +%Y-%m-%d` #3天之前的日期
backup_dir=$backup_location/$backup_Ymd  #备份文件夹全路径
welcome_msg="Welcome to use MySQL backup tools!" #欢迎语

# 判断MYSQL是否启动,mysql没有启动则备份退出
mysql_ps=`ps -ef |grep mysql |wc -l`
mysql_listen=`netstat -an |grep LISTEN |grep $mysql_port|wc -l`
if [ [$mysql_ps == 0] -o [$mysql_listen == 0] ]; then
        echo "ERROR:MySQL is not running! backup stop!"
        exit
else
        echo $welcome_msg
fi

# 连接到mysql数据库,无法连接则备份退出
mysql -h$mysql_host -P$mysql_port -u$mysql_user -p$mysql_password <<end
use mysql;
select host,user from user where user='root' and host='localhost';
exit
end

flag=`echo $?`
if [ $flag != "0" ]; then
        echo "ERROR:Can't connect mysql server! backup stop!"
        exit
else
        echo "MySQL connect ok! Please wait......"
        # 判断有没有定义备份的数据库,如果定义则开始备份,否则退出备份
        if [ "$backup_db_arr" != "" ];then
                #dbnames=$(cut -d ',' -f1-5 $backup_database)
                #echo "arr is (${backup_db_arr[@]})"
                for dbname in ${backup_db_arr[@]}
                do
                        echo "database $dbname backup start..."
                        `mkdir -p $backup_dir`
                        `mysqldump -h$mysql_host -P$mysql_port -u$mysql_user -p$mysql_password $dbname --default-character-set=$mysql_charset | gzip > $backup_dir/$dbname-$backup_time.sql.gz`
                        flag=`echo $?`
                        if [ $flag == "0" ];then
                                echo "database $dbname success backup to $backup_dir/$dbname-$backup_time.sql.gz"
                        else
                                echo "database $dbname backup fail!"
                        fi

                done
        else
                echo "ERROR:No database to backup! backup stop"
                exit
        fi
        # 如果开启了删除过期备份,则进行删除操作
        if [ "$expire_backup_delete" == "ON" -a  "$backup_location" != "" ];then
                 #`find $backup_location/ -type d -o -type f -ctime +$expire_days -exec rm -rf {} \;`
                 `find $backup_location/ -type d -mtime +$expire_days | xargs rm -rf`
                 echo "Expired backup data delete complete!"
        fi
        echo "All database backup success! Think you!"
        exit
fi

改权限

1
2
chmod 600 /root/mysql_backup.sh
chmod +x /root/mysql_backup.sh

加入 crontab 每天00:00定时自动备份

1
00 00 * * * /root/mysql_backup.sh

至此,网站所在VPS A上的部署已经都完成了!
接下来在备份VPS B上进行设置来拉备份。

二、VPS B 上面的具体部署
1. 安装 rsync

1
yum -y install rsync

这里不需要加入开机启动了,因为是客户端,不是服务端

2. 设置rsync密码

1
2
echo '你在A上设置的密码' > /etc/rsync.pass
chmod 400 /etc/rsync.pass

3. 测试一下同步
先建个存储备份的地方

1
mkdir -p /var/rsync/

测试一下同步
rsync -avzP –delete –password-file=/etc/rsync.pass 用户名@192.168.0.100::www /var/rsync/xiaohost.com/

这条命令,我说明一下几个要点:

1
2
3
4
5
6
-avzP是啥,自己搜索介绍
--delete 是为了比如A上删除了一个文件,同步的时候,B会自动删除那个文件
--password-file 刚才VPS B中 /etc/rsync.pass 设置那个密码,要和VPS A的 /etc/rsyncd.scrt 中的密码一样,这样cron运行的时候,就不需要密码了
这条命令中的"用户名"为VPS A的 /etc/rsyncd.scrt 中的用户名
这条命令中的 192.168.0.100 为VPS A的IP地址
::www,注意是2个 : 号,www为VPS A的配置文件 /etc/rsyncd.conf 中的[www],意思是根据A上的/etc/rsyncd.conf来同步其中的[www]段内容,一个 : 号的时候,用于不根据配置文件,直接同步指定目录

4. 加入crontab每天00:30同步

1
30 00 * * * rsync -avzP --delete --password-file=/etc/rsync.pass 用户名@192.168.0.100::www /var/rsync/xiaohost.com/ > /dev/null 2>&1

OK!至此大功告成!不怕丢数据了,天天自动备份!
如果还要再保险一点,再加个VPS C
C来同步B,双重备份,哪个挂了都不怕!

原文链接:https://xiaohost.com/273.html,转载请注明出处。
0

评论0

请先