On-line MySQL backup script

Source: Internet
Author: User
Tags mysql backup

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

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.