MySQL backup script

Source: Internet
Author: User
Tags mysql host mysql backup

#!/bin/bash

# Db_user is MySQL username

# db_passwd is MySQL password

# db_host is MySQL host


# -----------------------------

Db_user= "Ari"

db_passwd= "[Email protected]"

db_host= "localhost"


# The directory for story your backup file.

backup_dir= "/home/mybackup"


# Date format for backup file (year-month-day _ Time: minutes: seconds)

time= ' date + ' (%y-%m-%d) "'


# MySQL, mysqldump and some other bin ' s path

Mysql= "/usr/local/mysql/bin/mysql"

mysqldump= "/usr/local/mysql/bin/mysqldump"

Mkdir= "/bin/mkdir"

Rm= "/bin/rm"

cp= "/BIN/CP"

gzip= "/bin/gzip"

Keep_days=30

# Check the directory for store backup is writeable

Test!-W $backup _dir && echo "Error: $backup _dir is un-writeable." && Exit 0


# The directory for stories the newest backup

Test! -D "$backup _dir/backup.0/" && $MKDIR "$backup _dir/backup.0/"


# Get all databases

All_db= "$ ($MYSQL-u $db _user-h $db _host-p$db_passwd-bse ' show databases ')"


For DB in $all _db

Do

$MYSQLDUMP-u $db _user-h $db _host-p$db_passwd--single-transaction $db > "$backup _dir/backup.0/$time. $db. sql "

CD "$backup _dir/backup.0/"

TAR-ZCF $time. $db. tgz $time. $db. sql

Rm-f $time. $db. sql


Done


Find $backup _dir/backup.0/-mtime + $KEEP _days-exec rm {}\;


Exit 0;


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.