Online MySQL backup script

Source: Internet
Author: User
Tags mysql backup

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:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.