標籤:Database Backup 指令碼
一 前提
系統內容:centos6.1
指令碼作用:備份指定伺服器上的指定資料庫,並通過scp傳送到另一台備份主機,然後發送備份是否成功郵件到指定郵箱
思路:首先在備份指令碼中把備份報告匯出為一個db_backup.log檔案,然後郵件指令碼會把這個log內容作為郵件內容發送給指定使用者,也就實現了郵件通知
說明:如果需要備份到遠程主機,需要首先做好ssh密鑰認證,scp要使用到
二 備份指令碼
備份指令碼用法:如果需要備份某個資料庫,在指令碼後面添加一條備份命令,像下面這樣
backup_db 10.90.5.50 dbname mysql 123456 innodb
函數名 mysql所在IP 所要備份的資料庫名 串連資料庫使用者名稱 密碼 資料庫引擎類型
#!/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_backupMYSQLDUMP=‘/usr/local/mysql/bin/mysqldump‘#刪除上一次備份報告rm -f $HOMEDIR/db_backup.log#################################################################function#############################################################################backup functionbackup_db (){if [ $# -ne 5 ];thenecho "Usage: backupdb IP dbname user password dbtype"echo "Please try again"exit 1fiIP=$1DBNAME=$2USER=$3PASSWORD=$4DBTYPE=$5echo "--------------------------start backup $IP $DBNAME db-------------------------------------" >> $HOMEDIR/db_backup.logecho "start date: `date +%Y%m%d_%H%M%S`" >> $HOMEDIR/db_backup.logif [ $DBTYPE = "innodb" ];then$MYSQLDUMP -u$USER -p$PASSWORD -h$IP --single-transaction --flush-logs --routines --events --master-data=2 "$DBNAME" > $HOMEDIR/$DBNAME/${DBNAME}_$DATE.sqlif [ $? -ne 0 ];thenecho "$IP $DBNAME $DBTYPE db backup is not success ,please check it out!" >> $HOMEDIR/db_backup.logelseecho "$IP $DBNAME $DBTYPE db backup is success!" >> $HOMEDIR/db_backup.logfielse $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 fifiecho "End date: `date +%Y%m%d_%H%M%S`" >> $HOMEDIR/db_backup.loggzip $HOMEDIR/$DBNAME/${DBNAME}_$DATE.sqlSIZE=`du -sh $HOMEDIR/$DBNAME/${DBNAME}_$DATE.sql.gz | awk ‘{print $1}‘`echo "the file size is: $SIZE" >> $HOMEDIR/db_backup.logecho "--------------------------end backup $IP $DBNAME db-------------------------------------" >> $HOMEDIR/db_backup.log#delete the backup files which were create two month agorm -f $HOMEDIR/$DBNAME/${DBNAME}_$DATE2.sql.gz}#scp function : scp the backup file to another server as different place backupscp_function (){ if [ $# -ne 1 ];then echo "Usage: scp_function dbname" echo "Please try again" exit 1 fiDBNAME=$1scp -P52814 -i /home/lj/.ssh/id_rsa $HOMEDIR/$DBNAME/${DBNAME}_$DATE.sql.gz [email protected]:/data/dbbackup/if [ $? -ne 0 ];thenecho "$DBNAME db backup data transfer is not success ,please check it out!" >> $HOMEDIR/db_backup.logelseecho "$DBNAME db backup data transfer is success!" >> $HOMEDIR/db_backup.logfi}##########################################################################function###################################################################使用perl命令執行郵件指令碼,以發送備份報告,其中的sendmail.pl為下面發郵件指令碼perl /home/scripts/sendmail.pl
三 發送郵件指令碼
指令碼中需要安裝外掛程式,請確認你的系統中已安裝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 = ‘[email protected]‘;#郵件需要發給誰my @mail_account = (‘[email protected]‘);#郵件發送方郵箱my $mailuser = ‘[email protected]‘;#發送方郵箱密碼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點執行備份指令碼
本文出自 “fightering-cluter” 部落格,請務必保留此出處http://3974020.blog.51cto.com/3964020/1545114
mysql資料庫自動備份,並發送郵件通知