linux下備份MYSQL資料庫的方法
來源:互聯網
上載者:User
只需按照下面3步做,一切都在你的掌控之下:
第一步:在伺服器上配置備份目錄代碼:
--------------------------------------------------------------------------------
mkdir /var/lib/mysqlbackup
cd /var/lib/mysqlbackup
--------------------------------------------------------------------------------
第二步:編寫備份指令碼代碼:
--------------------------------------------------------------------------------
vi dbbackup.sh
--------------------------------------------------------------------------------
粘帖以下代碼,務必更改其中的username,password和dbname。
代碼:
--------------------------------------------------------------------------------
#!/bin/sh #Username to access the MySQL server USERNAME="username" # Username to access the MySQL server PASSWORD="password" # List of DBNAMES for Backup DBNAME="dbname" #date timestamp for log message DATE=`/bin/date +%Y-%m-%d_%Hh%Mm` #output file OUTDIR="/var/lib/mysqlbackup/" OUTFILE="ip_bindass".$DATE."sql.gz" #working directory DIR="/var/lib/mysqlbackup/"#cd $DIR # MySQL Backup /usr/bin/mysqldump --database $DBNAME --opt --single-transaction -u$USERNAME -p$PASSWORD | /usr/bin/gzip -9 > $OUTDIR$OUTFILE
--------------------------------------------------------------------------------
更改備份指令碼許可權
代碼:
--------------------------------------------------------------------------------
chmod +x dbbackup.sh
--------------------------------------------------------------------------------
第三步:用crontab定時執行備份指令碼代碼:
--------------------------------------------------------------------------------
crontab -e
--------------------------------------------------------------------------------
若每天下午3點20備份,添加如下代碼,
代碼:
--------------------------------------------------------------------------------
20 15 * * * /var/lib/mysqlbackup/dbbackup.sh
--------------------------------------------------------------------------------
搞定!