码迷,mamicode.com
首页 > 数据库 > 详细

MySQL备份脚本

时间:2014-12-23 17:44:14      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:mysql自动备份脚本

#!/bin/bash

# db_user is mysql username

# db_passwd is mysql password

# db_host is mysql host


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

db_user="ari"

db_passwd="yuiopkjhr3bfcfarbhbjiu@"

db_host="localhost"


# the directory for story your backup file.

 backup_dir="/home/mybackup"


# date format for backup file (年-月-日_时:分:秒)

 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 story 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备份脚本

标签:mysql自动备份脚本

原文地址:http://yanruohan.blog.51cto.com/9740053/1593608

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!