本文实例为大家分享了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
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
|
#!/bin/bash #全备方式,一般在从机上执行,适用于小中型mysql数据库 #删除15天以前备份 #作者:fafu_li #时间:2015.08.10 source /etc/profile #加载系统环境变量 source ~/.bash_profile #加载用户环境变量 set -o nounset #引用未初始化变量时退出 #set -o errexit #执行shell命令遇到错误时退出 user= "root" password= "123456" host= "localhost" port= "3306" #需备份的数据库,数组 db=( "test" ) #备份时加锁方式, #MyISAM为锁表--lock-all-tables, #InnoDB为锁行--single-transaction lock= "--single-transaction" mysql_path= "/usr/local/mysql" backup_path= "${mysql_path}/backup" date =$( date +%Y-%m-%d_%H-%M-%S) day=15 backup_log= "${mysql_path}/backup.log" #建立备份目录 if [ ! -e $backup_path ]; then mkdir -p $backup_path fi #删除以前备份 find $backup_path - type f -mtime +$day - exec rm -rf {} \; > /dev/null 2>&1 echo "开始备份数据库:${db[*]}" #备份并压缩 backup_sql(){ dbname=$1 backup_name= "${dbname}_${date}.sql" #-R备份存储过程,函数,触发器 mysqldump -h $host -P $port -u $user -p$password $lock --default-character- set =utf8 --flush-logs -R $dbname > $backup_path/$backup_name if [[ $? == 0 ]]; then cd $backup_path tar zcpvf $backup_name. tar .gz $backup_name size=$( du $backup_name. tar .gz -sh | awk '{print $1}' ) rm -rf $backup_name echo "$date 备份 $dbname($size) 成功 " else cd $backup_path rm -rf $backup_name echo "$date 备份 $dbname 失败 " fi } #循环备份 length=${ #db[@]} for (( i = 0; i < $length; i++ )); do backup_sql ${db[$i]} >> $backup_log 2>&1 done echo "备份结束,结果查看 $backup_log" du $backup_path/*$ date * -sh | awk '{print "文件:" $2 ",大小:" $1}' |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://www.cnblogs.com/leffss/p/7832047.html