(0)備份目標
備份:tel數據庫
目錄:/mysqlbackup/
策略:天天3點mysql
(1)建立一個備份用戶,用戶名未backup 密碼backup
mysql>grant all on tel. to 'backup'@'localhost' identified by "backup";
提供更小的權限:
mysql>grant select,show view,lock tables,trigger on tel. to 'backup'@'localhost' identified by "backup";sql
(2)編寫備份腳本
shell> vim mysql_backup.sh
#!/bin/sh
###定義變量####
date=date +%Y%m%d
ago10date=date -d"10 day ago" +"%Y%m%d"
backuser=backup
db_host=localhost
backupdir=/mysqlbackupshell
###判斷存放目錄是否存在####
if [ ! -d $backupdir ]
then
mkdir -p $backupdir
fi
###判斷第二級存放目錄是否存在####
if [ ! -d $backupdir/$date ]
then
mkdir -p $backupdir/$date
fi數據庫
##########執行備份命令#############
/usr/bin/mysqldump -u$backuser -h $db_host -p'backup' --single-transaction tel > "$backupdir/$date/tel.sql"vim
##########刪除10天前的備份#########
if [ -d $backupdir/$ago10date ]
then
rm -rf $backupdir/$ago10date
fiide
(3)添加執行權限
shell>chmod +x mysql_backup.shcode
(4)添加定時任務
shell> crontab -l crontab