0%

备份数据库脚本并将备份数据库同步到又拍云存储空间

经历过一次意外,可能某拥有服务器权限的同事将测试器将全部数据给删除了,导致服务器数据库全部丢失,服务器崩溃,只能重装。特弄了一个简单的数据库脚本备份并将数据库同步到又拍云存储空间。

1
2
3
##又拍云UPX工具

https://github.com/polym/upx

同步脚本

1
2
3
4
5
6
7
8
9
10
11
12
[root@localhost backup]# cat backup.sh 
#!/bin/bash

##用于本地目录中的所有文件同步上传到又拍云存储,用时监控目录变化

service_name="testbackup"
username="******"
password="******"

/home/backup/upx login testbackup ****** ******

/home/backup/upx sync /home/backup

mysql备份脚本

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
[root@localhost backup]# cat xtrabackup-full.sh 
#!/bin/bash

user='root'
passwd='123456'
host='127.0.0.1'
port='3307'
database='cloud_yssj'
my_config='/usr/local/mysql-test1/my.cnf'
log=$database-$(date +%Y%m%d%H%M).log
str=$database-$(date +%Y%m%d%H%M).tar.gz
backup_dir='/home/backup/mysql'

echo "Start to backup at $(date +%Y%m%d%H%M)"
if [ ! -d "$backup_dir" ];then
mkdir -p $backup_dir
fi

/usr/bin/innobackupex --defaults-file=$my_config --user=$user --password=$passwd --port=$port --host=$host --database=$database --stream=tar $backup_dir 2>$backup_dir/$log | gzip 1>$backup_dir/$str

if [ $? -eq 0 ];then
echo "Backup is finish! at $(date +%Y%m%d%H%M)"
exit 0
else
echo "Backup is Fail! at $(date +%Y%m%d%H%M)"
exit 1
fi
echo "Backup Process Done"

mongodb、redis备份脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[root@localhost backup]# cat mongodb.sh 
#!/bin/bash

host='127.0.0.1:37018'
database='cloud_yssj'

backup_dir1='/home/backup/mongodb'
backup_dir2='/home/backup/redis'

if [ ! -d "$backup_dir1" ];then
mkdir -p $backup_dir1
fi

if [ ! -d "$backup_dir2" ];then
mkdir -p $backup_dir2
fi

/home/mongodb/bin/mongodump -h $host -d $database -o $backup_dir1

tar zcvf $backup_dir1/mongodb-$(date +%Y%m%d%H%M).tar.gz $backup_dir1/$database

rm -rf /home/backup/mongodb/cloud_yssj

tar zcvf $backup_dir2/redis-$(date +%Y%m%d%H%M).tar.gz /home/redis/data/dump8000.rdb

增加定时任务

1
2
3
4
[root@localhost backup]# crontab -l
30 1 * * * /home/backup/mongodb.sh
20 2 * * * /home/backup/xtrabackup-full.sh
40 3 * * * /home/backup/backup.sh