#!/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