建立兩個備份目錄mysql
mkdir backup mkdir oldbackup
建立備份的shell腳本sql
vi /backup/backup.sh
具體內容以下:注意shell腳本中都寫成具體的絕對路徑shell
#!bin/bash cd /home/mysql/backup echo "You are In Backup Directory" mv /home/mysql/backup/backup* /home/mysql/oldbackup echo "Old Databases are Moved to oldbackup folder" Now=$(date +"%Y%m%d%H%M%S") File=backup-$Now.sql mysqldump -u root -p root {dbname} {tablename1} {tablename2} > /home/mysql/backup/$File echo "Your Database Backup Successfully Completed"
建立crontab任務bash
crontab -e
內容以下:表示天天的凌晨3:00進行備份。spa
0 3 * * * sh /home/mysql/backup/backup.sh