MySQL備份還原——AutoMySQLBackup介紹

MySQL備份還原——AutoMySQLBackup介紹

2015-07-15 17:25 by 瀟湘隱者, 2672 閱讀, 1 評論, 收藏編輯html

AutoMySQLBackup是一個開源的MySQL備份腳本。能夠說它是一個輕量級的備份方案,AutoMySQLBackup的安裝、配置很是簡單、方便。AutoMySQLBackup的sourceforge上介紹有如它自己,也很是的簡單:mysql

Descriptionlinux

AutoMySQLBackup with a basic configuration will create Daily, Weekly and Monthly backups of one or more of your MySQL databases from one or more of your MySQL servers.sql

Other Features include:shell

- Email notification of backups (備份郵件通知)數據庫

- Backup Compression and Encryption(備份壓縮與加密)安全

- Configurable backup rotation (配置備份保留週期)服務器

- Incremental database backups (數據庫增量備份)多線程

 

下載地址socket

 

AutoMySQLBackup的下在地址爲http://sourceforge.net/projects/automysqlbackup/ 。 目前最新版本爲automysqlbackup-v3.0_rc6.tar.gz

 

 

安裝配置

 

1:將下載的安裝文件automysqlbackup-v3.0_rc6.tar.gz拷貝到/tmp目錄下

 

2:將automysqlbackup-v3.0_rc6.tar.gz解壓到新建的目錄automysqlbackup下。以下所示,解壓後總共有6個文件。

[root@DB-Server tmp]# tar -zxvf /tmp/automysqlbackup-v3.0_rc6.tar.gz  -C /tmp/automysqlbackup
automysqlbackup
automysqlbackup.conf
CHANGELOG
install.sh
README
LICENSE

automysqlbackup: AutoMySQLBackup的應用程序腳本

automysqlbackup.conf : AutoMySQLBackup的配置文件範本

CHANGELOG: AutoMySQLBackup功能添加、Bug修復歷史記錄

install.sh: AutoMySQLBackup的安裝腳本

README: AutoMySQLBackup的聲明、安裝、使用說明文檔

建議在安裝前看一下README文件,裏面有介紹如何安裝、配置automysqlbackup。AutoMySQLBackup提供了自動和手動安裝配置兩種功能(以下所示)

clip_image001

 

自動安裝配置

 

1:運行腳本install.sh安裝配置AutoMySQLBackup

[root@DB-Server tmp]# cd automysqlbackup
[root@DB-Server automysqlbackup]# ./install.sh
### Checking archive files for existence, readability and integrity.
 
automysqlbackup ... exists and is readable ... md5sum okay :)
automysqlbackup.conf ... exists and is readable ... md5sum okay :)
README ... exists and is readable ... md5sum okay :)
LICENSE ... exists and is readable ... md5sum okay :)
 
Select the global configuration directory [/etc/automysqlbackup]:
Select directory for the executable [/usr/local/bin]: /usr/bin
### Creating global configuration directory /etc/automysqlbackup:
 
success
 
### Copying files.
 
 
if you are running automysqlbackup under the same user as you run this install script,
you should be able to access it by running 'automysqlbackup' from the command line.
if not, you have to check if 'echo $PATH' has /usr/bin in it
 
Setup Complete!
[root@DB-Server automysqlbackup]#

clip_image002

以下所示,你會發現install.sh腳本將automysqlbackup.conf、 LICENSE、 README三個文件拷貝到/etc/automysqlbackup/目錄下,並複製了

automysqlbackup.conf文件生成了myserver.conf

[root@DB-Server ~]# cd /etc/automysqlbackup/

[root@DB-Server automysqlbackup]# ls

automysqlbackup.conf LICENSE myserver.conf README

[root@DB-Server automysqlbackup]#

 

2:配置本身的AutoMySQLBackup的配置文件myserver.conf

 

默認狀況下,這些AutoMySQLBackup的參數都是註釋的,你須要取消註釋,給予相關參數配置信息,例如使用那個帳號作備份、帳號密碼、

# Username to access the MySQL server e.g. dbuser
CONFIG_mysql_dump_username='root'    --備份帳號
 
# Password to access the MySQL server e.g. password
CONFIG_mysql_dump_password='123456'
 
# Host name (or IP address) of MySQL server e.g localhost
CONFIG_mysql_dump_host='localhost'
 
# "Friendly" host name of MySQL server to be used in email log
# if unset or empty (default) will use CONFIG_mysql_dump_host instead
#CONFIG_mysql_dump_host_friendly=''
 
# Backup directory location e.g /backups
CONFIG_backup_dir='/u03/mysqlbackup/'
 
....................................................

clip_image003

下面列出了部分主要的參數設置,具體能夠參考automysqlbackup.conf 或 myserver.conf裏面的參數的描述

參數設置

參數功能

樣例

CONFIG_mysql_dump_username

備份時鏈接數據庫的帳號

CONFIG_mysql_dump_username='root'

CONFIG_mysql_dump_password

備份帳號的密碼

CONFIG_mysql_dump_password='******'

CONFIG_mysql_dump_host

備份服務器名稱或IP,通常用'localhost'

CONFIG_mysql_dump_host='localhost'

CONFIG_mysql_dump_host_friendly

設置服務器別名,若是不設置或爲空,則使用CONFIG_mysql_dump_host替換

 

CONFIG_backup_dir

數據庫備份路徑

CONFIG_backup_dir='/mysqldata/db_backup/mysqlbackup'

CONFIG_multicore

壓縮數據時是否啓用多核,具體參考文檔解釋

 

CONFIG_multicore_threads

多線程數量

 

CONFIG_db_names

要備份的數據庫

 

CONFIG_db_month_names

要作月備份的數據庫

 

CONFIG_table_exclude

備份時要排除那些表

 

CONFIG_db_exclude

備份時要排除那些db

 

CONFIG_do_monthly

在每月的第幾天作月備份

 

CONFIG_do_weekly

星期幾作周備份

 

CONFIG_rotation_daily

日備的保留週期

 

CONFIG_rotation_weekly

周備的保留週期

 

CONFIG_rotation_monthly

月被的保留週期

 

CONFIG_mysql_dump_port

MySQL的端口號

 

CONFIG_mysql_dump_compression

備份文件採用的壓縮格式

CONFIG_mysql_dump_compression='gzip'

CONFIG_mysql_dump_differential

是否作差別備份

CONFIG_mysql_dump_differential='yes'

CONFIG_encrypt

備份是否加密

 

CONFIG_encrypt_password

備份加密的密碼

 

README裏面關於配置參數的一個參考樣本,通常參數設置應該根據具體狀況也業務需求來合理設定。

Default configuration
CONFIG_configfile="/etc/automysqlbackup/automysqlbackup.conf"
CONFIG_backup_dir='/var/backup/db'
CONFIG_do_monthly="01"
CONFIG_do_weekly="5"
CONFIG_rotation_daily=6
CONFIG_rotation_weekly=35
CONFIG_rotation_monthly=150
CONFIG_mysql_dump_usessl='yes'
CONFIG_mysql_dump_username='root'
CONFIG_mysql_dump_password=''
CONFIG_mysql_dump_host='localhost'
CONFIG_mysql_dump_socket=''
CONFIG_mysql_dump_create_database='no'
CONFIG_mysql_dump_use_separate_dirs='yes'
CONFIG_mysql_dump_compression='gzip'
CONFIG_mysql_dump_commcomp='no'
CONFIG_mysql_dump_latest='no'
CONFIG_mysql_dump_max_allowed_packet=''
CONFIG_db_names=()
CONFIG_db_month_names=()
CONFIG_db_exclude=( 'information_schema' )
CONFIG_mailcontent='log'
CONFIG_mail_maxattsize=4000
CONFIG_mail_address='root'
CONFIG_encrypt='no'
CONFIG_encrypt_password='password0123'

3:運行備份腳本並檢查備份

[root@DB-Server ~]# /usr/bin/automysqlbackup /etc/automysqlbackup/myserver.conf
Parsed config file "/etc/automysqlbackup/automysqlbackup.conf"
 
# Checking for permissions to write to folders:
base folder /u03 ... exists ... ok.
backup folder /u03/mysqlbackup/ ... exists ... writable? yes. Proceeding.
checking directory "/u03/mysqlbackup//daily" ... exists.
checking directory "/u03/mysqlbackup//weekly" ... exists.
checking directory "/u03/mysqlbackup//monthly" ... exists.
checking directory "/u03/mysqlbackup//latest" ... exists.
checking directory "/u03/mysqlbackup//tmp" ... exists.
checking directory "/u03/mysqlbackup//fullschema" ... exists.
checking directory "/u03/mysqlbackup//status" ... exists.
 
# Testing for installed programs
mysql ... found.
mysqldump ... found.
gzip ... found.
 
# Parsing databases ... done.
======================================================================
AutoMySQLBackup version 3.0
http://sourceforge.net/projects/automysqlbackup/
 
Backup of Database Server - localhost
Databases - mysql,performance_schema,test
Databases (monthly) - mysql,performance_schema,test
======================================================================
======================================================================
Dump full schema.
 
Rotating 4 month backups for 
 
======================================================================
 
======================================================================
Dump status.
 
Rotating 4 month backups for 
 
======================================================================
 
Backup Start Time Sat Jul 11 22:16:32 CST 2015
======================================================================
Daily Backup ...
 
Daily Backup of Database ( mysql )
Rotating 6 day backups for mysql
----------------------------------------------------------------------
Daily Backup of Database ( performance_schema )
Rotating 6 day backups for performance_schema
----------------------------------------------------------------------
Daily Backup of Database ( test )
Rotating 6 day backups for test
----------------------------------------------------------------------
 
Backup End Time Sat Jul 11 22:16:33 CST 2015
======================================================================
Total disk space used for backup storage...
Size - Location
750k /u03/mysqlbackup/
 
======================================================================
 
###### WARNING ######
Errors reported during AutoMySQLBackup execution.. Backup failed
Error log below..
Warning: Using a password on the command line interface can be insecure.
Warning: Using a password on the command line interface can be insecure.
Warning: Using a password on the command line interface can be insecure.
Warning: Using a password on the command line interface can be insecure.
Warning: Using a password on the command line interface can be insecure.
Warning: Using a password on the command line interface can be insecure.

檢查備份目錄,你會發現生成了daily、fullschema、 latest、 monthly、 status、 tmp、 weekly等目錄,剛剛備份的文件位於daily目錄下,以下所示

clip_image004

 

手動安裝配置

AutoMySQLBackup也能夠手動配置,在文檔README裏面有介紹如何手工配置,也很是的簡單

1. Create the /etc/automysqlbackup directory.

2. Copy in the automysqlbackup.conf file.

3. Copy the automysqlbackup file to /usr/local/bin and make executable.

4. cp /etc/automysqlbackup/automysqlbackup.conf /etc/automysqlbackup/myserver.conf

5. Edit the /etc/automysqlbackup/myserver.conf file to customise your settings.

6. See usage section.

 

AutoMySQLBackup的使用

AutoMySQLBackup使用mutt發送郵件,它是linux下的一個email程序,因爲咱們通常都使用sendmail發送郵件,因此通常在配置文件裏面不啓用改參數。因此使用AutoMySQLBackup時,通常本身寫shell腳本,調用AutoMySQLBackup來備份數據,以下所示automysqlbackup.sh

#REM backup script
#REM ----------------------------------------
#REM  Backup script for EEL Mysql
#REM  Tommy Wang -  08-14-2012
#REM  Kerry Kong -  07-12-2015 Modify this Script
#REM ----------------------------------------
#REM - USER DEFINED VARIABLES -
export DATESTAMP=`date '+%F'`
 
#REM - Logfiles -
export BACKUP_FULL_LOG=/mysqldata/db_backup/logs/auto_backup_$DATESTAMP.log
 
/usr/local/bin/automysqlbackup /etc/automysqlbackup/myserver.conf >>$BACKUP_FULL_LOG
chown mysql.mysql /mysqldata/db_backup -R >>$BACKUP_FULL_LOG
find  /mysqldata/db_backup -type f -exec chmod 400 {} \; >>$BACKUP_FULL_LOG
find  /mysqldata/db_backup -type d -exec chmod 700 {} \; >>$BACKUP_FULL_LOG
 
EMAILTMP=/mysqldata/db_backup/logs/auto_backup_db_$DATESTAMP.TMP
 
##################################### config email parameters ####################################
echo 'Content-Type: text/html' > $EMAILTMP
echo 'To: dba@xxxx.com' >> $EMAILTMP
echo 'Subject: Backup Status - The server xxxx  MYSQL (dump backup)' >> $EMAILTMP
echo '<pre style="font-family: courier; font-size: 9pt">' >> $EMAILTMP
#################################################################################################
 
echo "==============================================================================" >> $EMAILTMP
echo "======================== Daily Backup for Mysql Database =====================" >> $EMAILTMP
echo "==============================================================================" >> $EMAILTMP
echo " " >> $EMAILTMP
 
cat $BACKUP_FULL_LOG >> $EMAILTMP 2>&1
echo " " >> $EMAILTMP
echo "================================== End of Backup =============================" >> $EMAILTMP
echo "==============================================================================" >> $EMAILTMP
 
/usr/sbin/sendmail -t -f "BackupAdmin" < $EMAILTMP
rm $EMAILTMP
 
chown mysql.mysql $BACKUP_FULL_LOG

使用AutoMySQLBackup發送郵件時,會有下面告警提示,這個是由於咱們將鏈接數據庫的帳號密碼配置在配置文件中,這樣很是不安全。如何去掉這個告警提示呢?AutoMysqlBackup: Warning: Using a password on the command line interface can be insecure. 這篇博客裏面介紹了一種方法

clip_image005

找到automysqlbackup文件,找到removeIO,而後在其下面加上下面這段腳本後,你就不會收到告警信息。

[root@getlnx20 ~]# cd /usr/local/bin

[root@getlnx20 bin]# vi automysqlbackup

removeIO
 
Add this after removeIO:
 
# Remove annoying warning message since MySQL 5.6
 
if [[ -s "$log_errfile" ]]; then
 
sedtmpfile="/tmp/$(basename $0).$$.tmp"
 
grep -v "Warning: Using a password on the command line interface can be insecure." "$log_errfile" > $sedtmpfile
 
mv $sedtmpfile $log_errfile
 
fi

關於AutoMySQLBackup的介紹到這裏,其實AutoMySQLBackup還有不少細節地方值得咱們去學習、研究。限於篇幅,不在此一一贅述。

 

做者:瀟湘隱者

相關文章
相關標籤/搜索