简单备份Mysql数据库

一:在服务器上配置备份目录代码:

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

 

你可能感兴趣的:(mysql)