Online MySQL backup script
Recently, I am studying database backup and regularly execute the backup task. Here I set the backup script to the crontab command for regular execution. The script content is as follows:
[Root @ mysql-db ~] # Cat mysql-backup.sh
#! /Bin/bash
Dbpwd = 'ssw0rd123'
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 =/DBBackup/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/${{db1__{{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
# Clear empty directories
Find $ {BAK_PATH}/$ dir-type d-empty | xargs rm-rf
Fi
Done
1 4 *** sh/DB-Backup/shell/DB_backup.sh
This article permanently updates the link address: