新聞中心
本篇內(nèi)容主要講解“centos每天自動備份mysql數(shù)據(jù)庫的腳本分享”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學(xué)習(xí)“centos每天自動備份mysql數(shù)據(jù)庫的腳本分享”吧!
腳本如下
#!/bin/bash
#功能說明:本功能用于備份
#編寫日期:2010/12/06
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
export PATH
#數(shù)據(jù)庫用戶名
dbuser='root'
#數(shù)據(jù)庫密碼
dbpasswd='123456'
#數(shù)據(jù)庫名,可以定義多個數(shù)據(jù)庫,中間以空格隔開,如:test test1 test2
dbname='test1 test2'
#備份時間
backtime=`date +%Y%m%d%H%M%S`
#日志備份路徑
logpath='/second/backup'
#數(shù)據(jù)備份路徑
datapath='/second/backup'
#日志記錄頭部
echo ‘"備份時間為${backtime},備份數(shù)據(jù)庫表 ${dbname} 開始" >> ${logpath}/log.log
#正式備份數(shù)據(jù)庫
for table in $dbname; do
source=`mysqldump -u ${dbuser} -p${dbpasswd} ${table}> ${logpath}/${backtime}.sql` 2>> ${logpath}/mysqllog.log;
#備份成功以下操作
if [ "$?" == 0 ];then
cd $datapath
#為節(jié)約硬盤空間,將數(shù)據(jù)庫壓縮
tar jcf ${table}${backtime}.tar.bz2 ${backtime}.sql > /dev/null
#刪除原始文件,只留壓縮后文件
rm -f ${datapath}/${backtime}.sql
echo "數(shù)據(jù)庫表 ${dbname} 備份成功!!" >> ${logpath}/mysqllog.log
else
#備份失敗則進行以下操作
echo "數(shù)據(jù)庫表 ${dbname} 備份失敗!!" >> ${logpath}/mysqllog.log
fi
done
定時執(zhí)行腳本:
方式:
1、
執(zhí)行 crontab -e
輸入以下內(nèi)容:
______________________________________________________________________________
00 00 * * * /bin/bash yourpath/mysqlbak.sh
2、
打開自動執(zhí)行文件
vi /etc/crontab
在etc中加入如下內(nèi)容,讓其自動執(zhí)行任務(wù)。
00 00 * * * root /mysqlbak.sh
以上兩個 00 00 *** 為每天的凌晨自動執(zhí)行腳本
分 時 日 月 周 命令
M: 分鐘(0-59)。每分鐘用*或者 */1表示
H:小時(0-23)。(0表示0點)
D:天(1-31)。
m: 月(1-12)。
d: 一星期內(nèi)的天(0~6,0為星期天)。
3、
Redhat方法:
Redhat的crontab采用按時間調(diào)用4個目錄(/etc/cron.hourly:每小時;/etc/cron.daily:每
天;/etc/cron.weekly:每周;/etc/cron.monthly:每月)中腳本出來運行的方式。
Redhat中只需要將剛才編輯的腳本復(fù)制到相應(yīng)的目錄即可。
cp /autobackupmysql.sh etc/cron.daily
重啟etc
/etc/rc.d/init.d/crond restart
到此,相信大家對“centos每天自動備份mysql數(shù)據(jù)庫的腳本分享”有了更深的了解,不妨來實際操作一番吧!這里是創(chuàng)新互聯(lián)建站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
文章名稱:centos每天自動備份mysql數(shù)據(jù)庫的腳本分享-創(chuàng)新互聯(lián)
本文URL:http://fisionsoft.com.cn/article/ddpeeo.html