您当前位置: 首页 »

mysql

标签归档: mysql

【debian7】按需备份数据(mysql),并将备份好的文件通过邮件发送到指定邮箱

首先需要安装这几个包:
mutt、uuencode、sendmail

脚本如下:

#!/bin/bash

#need 2 var
if (($# != 2)); then
        echo "need input [DBNamefile] [mode]"
        echo "mode -> 0, test DBNamefile read!"
        echo "mode -> 1, do DB bak!"
        exit;
fi

if (($2 < 0 && $2 > 1)); then
        echo "err mode!"
        exit
fi

#filter some lines:
# 1. pure tab line
# 2. pure \r\n
# 3. pure space line
DBNameFilePath=$1
sed /^[[:space:]]*$/d $DBNameFilePath > $DBNameFilePath".tmp"

#pre-info
loop_count=0
bak_DBName_target=1
echo "DBNameFilePath: "$DBNameFilePath" <<<>>>  DBNameFilePath_tmp: "$DBNameFilePath".tmp"
echo ============ output all db Name $(date +"%Y%m%d")  ==================
while read -r line
do
    #echo $line
        bak_DBName_target[loop_count]=$line
        ((loop_count++))
done < $DBNameFilePath".tmp"

i=0
for ((;i<$loop_count && $loop_count>0;i++))
do
 echo ${bak_DBName_target[$i]}
done
echo "total - "$i
echo ============ end ==================

if (($2 == 0)); then
        echo "test over"
        exit;
fi

if (($i<=0)); then
 echo "empty db name!"
 exit
fi

#do work
i=0
for ((;i<$loop_count && $loop_count>0;i++))
do
        DBName_Tmp=${bak_DBName_target[$i]}
        DataBakName_tmp=Data_bak_${DBName_Tmp}_$(date +"%Y%m%d")

        #del db before 3 days
        rm -rf /home/backup/Data_$(date -d -3day +"%Y%m%d").tar.gz

        #expory mysql db
        mysqldump -u root -ppassword $DBName_Tmp > /tmp/mysql_db_bak/$DataBakName_tmp.sql

        #compact db file
        tar zcf /tmp/mysql_db_bak/$DataBakName_tmp.tar.gz /tmp/mysql_db_bak/$DataBakName_tmp.sql

        #del temporary sql
        rm -rf /tmp/mysql_db_bak/$DataBakName_tmp.sql

        #Send mail
        mutt -s $DataBakName_tmp "email@163.com" -a /tmp/mysql_db_bak/$DataBakName_tmp.tar.gz < mailbody.txt
done

上面脚本的用法是:

./a.sh 1.txt 0

1.txt是保存要备份数据库名称的文件。

2014-05-30 | | linux-shell

【debian7】按需备份数据(mysql),并将备份好的文件通过邮件发送到指定邮箱已关闭评论