#!/bin/sh
# mysql_backup.sh: backup mysql databases and keep newest 5 days backup.
# —————————–
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
db_user="db name"
db_passwd="123456"
db_host="10.10.1.6"
#mysql>show databases; to obtain the databases name, list the name below
#for the db you wish to backup. e.g. databases=”db1 db2 db3″
databases="yiwang_shop"
# the directory for story your backup file.
backup_dir="/var/backup/mall"
# date format for backup file (dd-mm-yyyy)
time="$(date +”%d-%m-%Y”)"
######………$(date +"%d-%m-%Y")…date…..
# mysql, mysqldump and some other bin’s path
#MYSQL="$(which mysql)"
#MYSQLDUMP="$(which mysqldump)"
#MKDIR="$(which mkdir)"
#RM="$(which rm)"
#MV="$(which mv)"
#GZIP="$(which gzip)"
#########………………………………..
# if the directory for storing backup does not exist and not writeable, quit
test ! -w $backup_dir && echo "Error: $backup_dir is un-writeable." && exit 0
# otherwise make the directory to store the newest backup
test ! -d "$backup_dir" && mkdir "$backup_dir"
#####………..$backup_dir………..
# get all databases
for db in $databases
do
mysqldump -u $db_user -h $db_host -p$db_passwd $db | gzip -9 > "$backup_dir/$time.$db.gz"
.........................................................