时间:2021-07-01 10:21:17 帮助过:26人阅读
1 #!/bin/bash 2 #define var 3 user="root" 4 pass="1314520" 5 path="/mysql/backup" 6 cmd="mysql -u${user} -p${pass}" 7 dump="mysqldump -u${user} -p${pass} -B --events -x --master-data=2" 8 #system function 9 . /etc/init.d/functions 10 . /etc/profile 11 #judge dir 12 function jdir(){ 13 if [ ! -e $path ];then 14 mkdir $path -p 15 fi 16 } 17 #dump database 18 function bk(){ 19 for dbname in `$cmd -e ‘show databases;‘|awk ‘NR>1{print $0}‘|grep -v "performance_schema"` 20 do 21 $dump $dbname|gzip >${path}/${dbname}_$(date +%F).sql.gz 22 sleep 1 23 if [ -s ${path}/${dbname}_$(date +%F).sql.gz ];then 24 action "dump $dbname success!" /bin/true 25 else 26 action "dump $dbname failed" /bin/false 27 fi 28 done
29 } 30 function main(){ 31 jdir 32 bk 33 } 34 main
想和大家重点分享的是开发的思路,技术很基础。
经过测试可以完成,如果有错误,欢迎指正。
ps 附上分表备份的脚本供参考
1 #!/bin/bash 2 #define var 3 user="root" 4 pass="1314520" 5 path="/mysql/backup" 6 cmd="mysql -u${user} -p${pass}" 7 dump="mysqldump -u${user} -p${pass} --events -x --master-data=2" 8 #system function 9 . /etc/init.d/functions 10 . /etc/profile 11 #judge dir 12 function jdir(){ 13 if [ ! -e $path ];then 14 mkdir $path -p 15 fi 16 } 17 #dump database 18 function bk(){ 19 for dbname in `$cmd -e ‘show databases;‘|awk ‘NR>1{print $0}‘|grep -v "performance_schema"` 20 do 21 for tname in `$cmd -e "show tables from ${dbname}"|sed "1d"` 22 do 23 $dump $dbname $tname|gzip >${path}/${dbname}_${tname}_$(date +%F).sql.gz 24 25 if [ -e ${path}/${dbname}_${tname}_$(date +%F).sql.gz ];then 26 echo "${dbname}_${tname}" >>$path/mysql_table.log 27 28 fi 29 done 30 done 31 } 32 function main(){ 33 jdir 34 bk 35 } 36 main
分享一个MySQL分库分表备份脚本(原)
标签:执行 时间 思路 错误 etc 脚本 rom mkdir grep