Recently in the study of database backup, scheduled to perform backup tasks, where the backup script is set to the crontab command timed execution, the script content is as follows:
[email protected] ~]# cat mysql-backup.sh
#!/bin/bash
Dbpwd= ' possw0rd123 '
Dbuser=root
host=127.0.0.1
port=3306
dbarg= "-u$dbuser-p$dbpwd-h$host-p$port--default-character=utf8"
Date= ' Date +%y%m%d '
Bak_path=/aebackup/backup/db_bak
log= $BAK _path/$DATE/backup.log
Keep_backup_days=7
Mkdir-p $BAK _path/$DATE/$port
For DB in ' echo Show Databases|mysql $dbarg |grep-v ' information_schema\|test\|mysql\| Database "'
Do
Echo-e "$DATE ' DATE +%t ' DUMP $port $db. \c ">> $LOG
Mysqldump-b $dbarg-R $db |gzip > $BAK _path/$DATE/$port/${db}_${port}.sql.gz
Echo-e "' Date +%t ' OK" >> $LOG
Done
For dir in ' ls $BAK _path|grep [0-9]$ '
Do
If [' Ls-l $BAK _path|grep [0-9]$|wc-l '-GT 2];then
Find ${bak_path}/$dir-name "*.sql.gz*"-ctime +${keep_backup_days}|xargs rm-f
Find ${bak_path}/$dir-name backup.log-ctime +${keep_backup_days}|xargs rm-f
Find ${bak_path}/$dir-type d-empty |xargs RM-RF
#清除空目录
Find ${bak_path}/$dir-type d-empty |xargs RM-RF
Fi
Done
1 4 * * * sh/db-backup/shell/db_backup.sh
This article from "Flat Light is true" blog, please be sure to keep this source http://ucode.blog.51cto.com/10837891/1793771
On-line MySQL backup script