备份脚本
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
74
75
76 1#!/bin/bash
2#每天早上4点, mysql备份数据
3#chmod 700 backup.sh
4#crontab -e
5#0 4 * * * /home/mysqlbackup/mysql_backup.sh
6
7
8#做大备份数量 20 天
9Backup_Max_Files=20
10#数据库备份目录
11WORKING_DIR=$(dirname "$0")
12export WORKING_DIR=$(cd -P "$WORKING_DIR"/ > /dev/null; pwd)
13Backup_Log="${WORKING_DIR}/mysql_backup.log"
14#数据库IP地址、帐号、密码
15DB_Host="XXXXXXX"
16DB_User="XXXXXX"
17DB_Password="XXXXXXX"
18#需要备份的数据库
19Databases=("mysql" "test")
20
21#==========
22echo "###########Bash Shell MySql Databases Backup Tools!"
23
24#定义变量=当前日期
25Date_Time=$(date +%F)
26
27#定义备份文件前缀
28BackDataFile=backup"${Date_Time}"
29cd $WORKING_DIR
30mkdir $BackData_File
31#统计需要备份数据库的个数
32count=0
33while [ "x${Databasescount}" != "x" ]
34do
35 count=$((count + 1))
36 echo "Databases=$count"
37done
38
39echo "+ ${count} The database is about to be backed up"
40
41for Databases in ${Databases@}
42do
43 echo "+ MySql-backing up: ${Databases}"
44 echo -n " MySql Backup Start: "
45 echo $(date +"%Y-%m-%d %H:%M:%S")
46 #使用Mysqdump工具进行逻辑备份(全备)并使用邮件脚本发送成功失败提醒
47 if $(mysqldump -h${DB_Host} -u${DB_User} -p${DB_Password} --single-transaction --flush-logs mysql > ${BackData_File}/${Databases}.sql )
48 then
49 echo " Success OK!: Mysql database backup success!"
50 python /root/test/mail.py "y@163.com" "${Databases}数据库备份成功" "${Databases}数据库备份成功"
51 echo "Success OK!: Mysql database ${Databases} backup success! at $(date +'%Y-%m-%d %H:%M:%S')" >> "$Backup_Log"
52 else
53 echo "Error NO!: Failure of MySQL database backup!"
54 echo "Error NO!: Failure of MySQL database ${Databases} backup! at $(date +'%Y-%m-%d %H:%M:%S')" >> "$Backup_Log"
55 #python /root/test/mail.py "y@163.com" "${Databases}数据库备份失败" "${Databases}数据库备份失败请查看"
56 fi
57
58 echo -n "Backup success: "
59 echo $(date +"%Y-%m-%d %H:%M:%S")
60done
61
62echo
63echo "+ Packaging and compressing folders with Tar ...."
64#使用tar命令打包并压缩生成的数据库文件
65cd $WORKING_DIR
66tar -cv ${BackData_File} | bzip2 > ${BackData_File}.tar.bz2 && rm -rf ${BackData_File}
67#删除n天以前备份的数据文件
68
69echo "+ Delete over time database backup files ...."
70find $WORKINGDIR -name "backup*.tar.bz2" -mtime +${Backup_Max_Files} -exec rm -rf {} \;
71
72#find $WORKINGDIR -name "backup*.tar.bz2" -mtime +${Backup_Max_Files} -exec ls {} \;
73
74
75
76
发邮件python
后补:邮件报警脚本
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 1#!/usr/bin/python
2coding=utf-8
3
4#测试方法:python 5.py XXXXX@163.com test test
5#能正常发送邮件说明成功
6import smtplib
7import sys
8from email.mime.text import MIMEText
9
10class send_mail():
11 mail_user="XXXXXX@163.com"
12 mail_host="smtp.163.com"
13 mail_user="XXXXXX@163.com" #此出不是登录密码。而是开头smtp的登录口令 mail_pass="XXXXXx"
14 mail_postfix="163.com"
15
16 def init(self,sub,content):
17 self.me="Backup-Alter"+"<"+self.mail_user+"@"+self.mail_postfix+">"
18 self.msg = MIMEText(content,_subtype='html',_charset='utf-8')
19 self.msg['From'] = self.me
20 self.msg['Subject'] = sub
21
22 def sendMessage(self,user):
23 self.msg['To'] = ";".join(user)
24 try:
25 s = smtplib.SMTP_SSL(host=self.mail_host,port=465)
26 s.login(self.mail_user,self.mail_pass)
27 s.sendmail(self.me, user, self.msg.as_string())
28 #发送邮件
29 s.close()
30 except Exception, e:
31 print str(e)
32
33if name == 'main':
34 mailto_list=[]
35 mailto_list.append(sys.argv1)
36 s = send_mail(sys.argv2,sys.argv3)
37 s.sendMessage(mailto_list)
38
39
备份到OSS上
- 安装ossfs
在 aliyun 仓库 中存在 类似 亚马逊云 oss mount 的工具,然后通过如下方式进行安装即可:
1
2
3 1# wget https://github.com/aliyun/ossfs/releases/download/v1.80.5/ossfs_1.80.5_centos7.0_x86_64.rpm
2# yum localinstall ossfs_1.80.5_centos7.0_x86_64.rpm --nogpgcheck
3
- 账户配置
创建 /etc/passwd-ossfs 文件并添加如下内容:
1
2
3
4
5
6 1--- 在 passwd-ossfs 文件中放入 bucket:AccessKeyID:AccessKeySecret 格式存放即可 ---
2# echo bucket:XXXX:XXXXX > /etc/passwd-ossfs
3
4--- 修改 passwd-ossfs 访问权限 ---
5# chmod 640 /etc/passwd-ossfs
6
- 自动挂载
修改 /etc/fstab 文件,实现自动 mount, 在其中增加如下配置即可
1
2
3
4
5
6 1--- 非阿里云主机 url 使用外部访问链接 ---
2ossfs#bucket /srv/ossfs fuse _netdev,url=http://oss-cn-hangzhou.aliyuncs.com,allow_other 0 0
3
4--- 阿里云主机中 url 使用 内部链接 节约流量 ---
5ossfs#bucket /srv/ossfs fuse _netdev,url=http://oss-cn-hangzhou-internal.aliyuncs.com,allow_other 0 0
6
然后使用 mount -a 来进行测试即可,如果没有报错即配置正确
注 ossfs 只能用 root 来进行操作
定时备份
mysql 执行在 docker 环境中,在宿主机上执行 docker 命令来生成相应的内容
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 1#!/bin/bash
2
3# 容器名称
4docker_name=mysql
5
6data_dir="/srv/ossfs/backup/db/"
7docker exec -t ${docker_name} mysqldump -uuser -ppasswd --all-databases > "$data_dir/data_`date +%Y%m%d`.sql"
8if [ $? -ne 0 ];then
9 # 任务失败,发送邮件
10 echo -e "Failed to backup the databases..." | mail -s 'Backup failed!!!' admin@xxx.com
11 exit -1
12fi
13# 删除 7 天前的备份
14find $data_dir -mtime +7 -name 'data_[1-9].sql' -exec rm -rf {} \;
15
然后通过 crontab -e 添加如下内容
1
2 10 2 * * * sh /srv/crons/mysql-backup.sh > /srv/logs/backup/mysql_dumps.log 2>&1
2
注 通过上述方式备份时 docker exec -it … 中的 i 选项需要去掉,详细参考 #Error “The input device is not a TTY”