Linux下 MYSQL 定時備分指令碼

Michael_DD發表於2014-10-31
 Linux下 MYSQL 定時備分指令碼

1  建立備份目錄 /backup/mysql
[root@mysqltest1 ~]# mkdir -p /backup/mysql/

2  建立備份指令碼backmysql.sh
[root@mysqltest1 ~]# cd /backup/mysql/
[root@mysqltest1 mysql]# ll
total 8
-rwxr-xr-- 1 root root  398 Oct 31 21:16 backupmysql.sh
-rw-r--r-- 1 root root 1870 Oct 31 21:23 testdb20141031.sql
[root@mysqltest1 mysql]# vi backupmysql.sh
#增加以下內容
DB_NAME='testdb'
USER='root'
PWD='123456'
TIME=$(date +%Y%m%d)
DEL_TIME=$(date --date="7 days ago" +%Y%m%d)
DB_BAK_NAME=testdb${TIME}.sql
DB_DEL_NAME=testdb${DEL_TIME}.sql
BACKUP_PATH='/backup/mysql/'
echo $BACKUP_PATH$DB_BAK_NAME
if test -f $BACKUP_PATH$DB_DEL_NAME
then
echo "Delete history dump file"
rm -f $BACKUP_PATH$DB_DEL_NAME
fi
mysqldump -u $USER -p$PWD $DB_NAME>$BACKUP_PATH$DB_BAK_NAME
~
儲存退出!

3  配置crontab定期執行指令碼
[root@mysqltest1 mysql]# crontab -e
no crontab for root - using an empty one
00 00 * * * root /bakcup/mysql/backupmysql.sh

4  恢復
[root@mysqltest1 mysql]# mysql -uroot -p123456 testdb < /backup/mysql/testdb20141031.sql


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29500582/viewspace-1315801/,如需轉載,請註明出處,否則將追究法律責任。

相關文章