準(zhǔn)備工作
- 備份的文件要同步到七牛 所有 注冊 七牛的賬號(hào) 注冊地址:https://portal.qiniu.com/signup?code=3lqod8l7its9e
- 創(chuàng)建數(shù)據(jù)備份目錄
cd ~ && mkdir backup && cd backup
mkdir ysqlbackup && mkdir mysql
- 設(shè)置七牛 上傳賬號(hào)
七牛上傳用命令行工具 qshell :https://developer.qiniu.com/kodo/tools/1302/qshell
設(shè)置 qshell 賬號(hào)
./qshell account AccessKey SecretKey
測試上傳
qshell rput <Bucket> <Key> <LocalFile> true
./qshell rput dbbackup test.log test.log true
在 linux 服務(wù)器中腳本目錄
mkdir /var/lib/gogomysqlbackup
cd /var/lib/gogomysqlbackup
創(chuàng)建shell 腳本文件
- 創(chuàng)建 備份腳本文件 項(xiàng)目名稱數(shù)據(jù)庫名稱數(shù)據(jù)庫類型_backup.sh
touch project_dbname_dbtype_backup.sh
vi project_dbname_dbtype_backup.sh
腳本文件內(nèi)容
#!/bin/sh
# mysql data backup script
#
# use mysqldump --help,get more detail.
#
BakDir=/root/back/ysqlbackup
LogFile=/root/back/mysql/mysqlbak.log
DATE=`date +%Y%m%d%H%M%S`
echo " " >> $LogFile
echo " " >> $LogFile
echo "-------------------------------------------" >> $LogFile
echo $(date +"%y-%m-%d %H:%M:%S") >> $LogFile
echo "--------------------------" >> $LogFile
cd $BakDir
DumpFile=$DATE.sql
GZDumpFile=$DATE.sql.tar.gz
mysqldump -u username -ppassword dbname -h 127.0.0.1> $DumpFile
echo "Dump Done" >> $LogFile
tar czvf $GZDumpFile $DumpFile >> $LogFile 2>&1
echo "[$GZDumpFile]Backup Success!" >> $LogFile
rm -f $DumpFile
#cd $BakDir/daily
cd $BakDir
echo "Backup Done!" >> $LogFile
/root/back/qshell rput dbbackup $GZDumpFile $GZDumpFile true
echo "copy it to your local disk or ftp to somewhere !!!" >> $LogFile
find $BakDir -ctime +30 -exec rm {} ;
echo "delete file over 30 days" >> $LogFile
執(zhí)行文件
bash project_dbname_dbtype_backup.sh
修改文件的執(zhí)行權(quán)限
# chmod 777 project_dbname_dbtype_backup.sh
用crontab定時(shí)執(zhí)行備份腳本代碼
crontab -e
# 每兩個(gè)小時(shí)執(zhí)行一次
1 */2 * * * /var/lib/gogomysqlbackup/gogo_vtigercrm_mysql_backup.sh
參考引用
linux中mysql備份shell腳本代碼 :www.jb51.net/article/34376.htm
tar命令的詳細(xì)解釋 : http://blog.csdn.net/eroswang/article/details/5555415/