mysql定時備份資料庫的shell指令碼詳解

來源:互聯網
上載者:User

1. vi /home/zhangy/database_bak.sh
2.
3. #!/bin/sh
4. # File: /home/zhangy/database_bak.sh
5. # Database info bakupmysql TANK 2009/11/04
6. DB_USER="root" #使用者名稱
7. DB_PASS="********" #密碼
8. DB_NAME="myblog" #要備份的資料名
9.
10. # Others vars
11. DATE=`date +%Y_%m_%d` #要備份的日期
12. YESTERDAY=`date -d yesterday +%Y_%m_%d` #刪除昨天的備份
13.
14. BIN_DIR="/usr/local/mysql/bin"
15. BCK_DIR="/home/zhangy/database_bak" #備份路徑
16.
17. cd $BCK_DIR
18.
19. #刪除以前該資料庫的備份,因為我的linux下面還有2G硬碟空間,鬱悶。
20. if [ -f $YESTERDAY$DB_NAME".sql" ]
21. then
22. rm -f $YESTERDAY$DB_NAME".sql"
23. fi
24.
25. # 備份
26. ${BIN_DIR}/mysqldump --opt -u${DB_USER} -p${DB_PASS} ${DB_NAME} > ${BCK_DIR}/${DATE}${DB_NAME}.sql
vi /home/zhangy/database_bak.sh
#!/bin/sh
# File: /home/zhangy/database_bak.sh
# Database info bakupmysql TANK 2009/11/04
DB_USER="root" #使用者名稱
DB_PASS="********" #密碼
DB_NAME="myblog" #要備份的資料名
# Others vars
DATE=`date +%Y_%m_%d` #要備份的日期
YESTERDAY=`date -d yesterday +%Y_%m_%d` #刪除昨天的備份
BIN_DIR="/usr/local/mysql/bin"
BCK_DIR="/home/zhangy/database_bak" #備份路徑
cd $BCK_DIR
#刪除以前該資料庫的備份,因為我的linux下面還有2G硬碟空間,鬱悶。
if [ -f $YESTERDAY$DB_NAME".sql" ]
then
rm -f $YESTERDAY$DB_NAME".sql"
fi
# 備份
${BIN_DIR}/mysqldump --opt -u${DB_USER} -p${DB_PASS} ${DB_NAME} > ${BCK_DIR}/${DATE}${DB_NAME}.sql

二,定期執行
把shell放到crontab裡面。
#查看crond是否已啟動
[root@BlackGhost cron]# ps -e|grep crond
21519 ? 00:00:00 crond
#開啟crontab
[root@BlackGhost cron]# crontab -e
#在裡面加上一行
00 18 * * * /home/zhangy/database_bak.sh
#查看一下是否已加上
[root@BlackGhost cron]# crontab -l
#
# DO NOT EDIT THIS FILE MANUALLY!! USE crontab -e INSTEAD.
#
#
01 * * * * /usr/sbin/run-cron /etc/cron.hourly
02 00 * * * /usr/sbin/run-cron /etc/cron.daily
22 00 * * 0 /usr/sbin/run-cron /etc/cron.weekly
42 00 1 * * /usr/sbin/run-cron /etc/cron.monthly
56 06 * * * /home/zhangy/www/bb.php
51 23 * * * /sbin/shutdown -h now
00 18 * * * /home/zhangy/database_bak.sh
然後退出

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.