当前位置: > 数据库 > MySQL >

linux 下mysql自动备份shell脚本

时间:2014-11-30 15:30来源:linux.it.net.cn 作者:IT
#!/bin/bash
bak_path=/data/bak_path
mysql_bk_dir=$bak_path/mysql_bk_dir
log_file=$backup_path/backuplog
password=123456
for i in $(mysql -p$password -e "show databases\G" |grep Database | awk '{print $2}')
do
    new_file="$mysql_bk_dir"/$i$(date +%Y%m%d).tar.gz
    dump_file="$mysql_bk_dir"/$i$(date +%Y%m%d).sql
    old_file="$mysql_bk_dir"/$i$(date +%Y$m%d).tar.gz

    echo "------------------------------------------------------------" >> $log_file

    echo $(date +"%Y-%m-%d %H:%M:%S") >> $log_file

    echo "------------------------------------------------------------" >>  $log_file
    
    if [ -f $old_file ]; then
        rm -rf $old_file >> $log_file 2>&1;
        echo "[$old_file] Delete Old File Success!" >> $log_file


    else
        echo "[$old_file] No Old Backup File!" >> $log_file
    fi

    if [ -f $new_file ]; then
        echo "[$new_file] The backup file is exist, can't backup!" >> $log_file;
    else
        cd $mysql_bk_dir

/usr/local/mysql/bin/mysqldump --opt $i -p$password > $dump_file

        tar zcf $new_file $dump_file >> $log_file 2>&1

        echo "[$new_file] Backup success!" >> $log_file
        rm -rf $dump_file
    fi

    sleep 15
done

mail -s "mysql backup is success!" wangshangyou@mosh.cn < $log_file
 
(责任编辑:IT)
------分隔线----------------------------
栏目列表
推荐内容