一 前提html
系統環境:centos6.1mysql
腳本做用:備份指定服務器上的指定數據庫,並經過scp傳送到另外一臺備份主機,而後發送備份是否成功郵件到指定郵箱sql
思路:首先在備份腳本中把備份報告導出爲一個db_backup.log文件,而後郵件腳本會把這個log內容做爲郵件內容發送給指定用戶,也就實現了郵件通知
數據庫
說明:若是須要備份到遠程主機,須要首先作好ssh密鑰認證,scp要使用到centos
二 備份腳本bash
備份腳本用法:若是須要備份某個數據庫,在腳本後面添加一條備份命令,像下面這樣
服務器
backup_db 10.90.5.50 dbname mysql 123456 innodb
函數名 mysql所在IP 所要備份的數據庫名 鏈接數據庫用戶名 密碼 數據庫引擎類型ssh
#!/bin/bash #name:backup_db.sh #Purpose: backup mysql database #Author: xxx #Date: 2014-08-26 #useage:backup_db IP databasename user password dbtype #example:backup_db 10.90.5.50 dbname mysql 123456 innodb #備份時間 DATE=`date -d"today" +%Y%m%d_%H%M%S` #前一個時間 DATE2=`date -d"1 month ago" +%Y%m%d_%H%M%S` #備份目錄 HOMEDIR=/data/dbdata_backup MYSQLDUMP='/usr/local/mysql/bin/mysqldump' #刪除上一次備份報告 rm -f $HOMEDIR/db_backup.log #################################################################function############################################################################ #backup function backup_db () { if [ $# -ne 5 ];then echo "Usage: backupdb IP dbname user password dbtype" echo "Please try again" exit 1 fi IP=$1 DBNAME=$2 USER=$3 PASSWORD=$4 DBTYPE=$5 echo "--------------------------start backup $IP $DBNAME db-------------------------------------" >> $HOMEDIR/db_backup.log echo "start date: `date +%Y%m%d_%H%M%S`" >> $HOMEDIR/db_backup.log if [ $DBTYPE = "innodb" ];then $MYSQLDUMP -u$USER -p$PASSWORD -h$IP --single-transaction --flush-logs --routines --events --master-data=2 "$DBNAME" > $HOMEDIR/$DBNAME/${DBNAME}_$DATE.sql if [ $? -ne 0 ];then echo "$IP $DBNAME $DBTYPE db backup is not success ,please check it out!" >> $HOMEDIR/db_backup.log else echo "$IP $DBNAME $DBTYPE db backup is success!" >> $HOMEDIR/db_backup.log fi else $MYSQLDUMP -u $USER -p$PASSWORD -h$IP -l --flush-logs --routines --events --master-data=2 "$DBNAME" > $HOMEDIR/$DBNAME/${DBNAME}_$DATE.sql if [ $? -ne 0 ];then echo "$IP $DBNAME $DBTYPE db backup is not success ,please check it out!" >> $HOMEDIR/db_backup.log else echo "$IP $DBNAME $DBTYPE db backup is success!" >> $HOMEDIR/db_backup.log fi fi echo "End date: `date +%Y%m%d_%H%M%S`" >> $HOMEDIR/db_backup.log gzip $HOMEDIR/$DBNAME/${DBNAME}_$DATE.sql SIZE=`du -sh $HOMEDIR/$DBNAME/${DBNAME}_$DATE.sql.gz | awk '{print $1}'` echo "the file size is: $SIZE" >> $HOMEDIR/db_backup.log echo "--------------------------end backup $IP $DBNAME db-------------------------------------" >> $HOMEDIR/db_backup.log #delete the backup files which were create two month ago rm -f $HOMEDIR/$DBNAME/${DBNAME}_$DATE2.sql.gz } #scp function : scp the backup file to another server as different place backup scp_function () { if [ $# -ne 1 ];then echo "Usage: scp_function dbname" echo "Please try again" exit 1 fi DBNAME=$1 scp -P52814 -i /home/lj/.ssh/id_rsa $HOMEDIR/$DBNAME/${DBNAME}_$DATE.sql.gz lj@10.90.5.50:/data/dbbackup/ if [ $? -ne 0 ];then echo "$DBNAME db backup data transfer is not success ,please check it out!" >> $HOMEDIR/db_backup.log else echo "$DBNAME db backup data transfer is success!" >> $HOMEDIR/db_backup.log fi } ##########################################################################function################################################################## #使用perl命令執行郵件腳本,以發送備份報告,其中的sendmail.pl爲下面發郵件腳本 perl /home/scripts/sendmail.pl
三 發送郵件腳本
ide
腳本中須要安裝插件,請確認你的系統中已安裝Net::SMTP_auth;Text::Iconv;MIME::Base64;Data::Dumper函數
#!/usr/bin/perl #Author: xxx #name:sendamil.pl #Purpose: Send file comment email to people #Date: 2014-08-26 #請確認下面插件已安裝 use strict; use Net::SMTP_auth; use Text::Iconv; use MIME::Base64; use Data::Dumper; #郵件服務器地址 my $mailhost = 'mail.qq.com'; #郵件從哪一個郵箱發出(from) my $mailfrom = 'monitor@qq.com'; #郵件須要發給誰 my @mail_account = ('xxx@qq.com'); #郵件發送方郵箱 my $mailuser = 'monitor@qq.com'; #發送方郵箱密碼 my $mailpasswd = 'jiankong'; #db_backup.log中的內容會做爲郵件內容發送,db_backup.log爲備份腳本所導出的這次備份報告 my $filename = '/data/dbdata_backup/db_backup.log'; #郵件主題 my $subject = 'dazhima_db_backup'; open(MYFILE,"$filename") || die "$!"; my $text; while (<MYFILE>){ $text .= $_."<br>"; } close(MYFILE); &SendMail($text,$subject); sub SendMail() { my $text = $_[0]; my $subject = $_[1]; my $smtp = Net::SMTP_auth->new( 'mail.medees.com', Timeout => 60, Debug => 0, Hello => 'medees.com' ) or die "Error.\n"; $smtp->auth( 'LOGIN', $mailuser, $mailpasswd ); foreach my $mailto (@mail_account) { $smtp->mail($mailfrom); $smtp->to($mailto); $smtp->data(); $smtp->datasend("Content-Type:text/html;charset=utf-8\n"); $smtp->datasend("Content-Transfer-Encoding:base64\n"); $smtp->datasend( "From:=?utf-8?B?" . encode_base64( 'JianKong', '' ) . "?= <$mailfrom> \n" ); $smtp->datasend( "To:=?utf-8?B?" . encode_base64( 'JianKong', '' ) . "?= <$mailto> \n" ); $smtp->datasend( "Subject:=?utf-8?B?" . encode_base64( $subject, '' ) . "?=\n\n" ); $smtp->datasend(); $smtp->datasend( encode_base64( $text, '' ) . " \n" ); $smtp->dataend(); } $smtp->quit; }
四 使用
添加一條crontab記錄
0 2 * * * /var/script/db_backup.sh
在天天的2點執行備份腳本