编写脚本
[root@iZ232s4zbqvZ service]# vi mysql_dump.sh
#!/bin/sh
echo "mysqldump start..."
DB_NAME="imms_core"
DB_USER="root"
DB_PASS="smithhuang"
BIN_DIR="/usr/bin"
YEAR=`date +%Y`
BCK_DIR="/data/mysql_dump/"$YEAR/
DATE=`date +%Y%m%d_%H%M%S`
mkdir -p $BCK_DIR
$BIN_DIR/mysqldump -u$DB_USER -p$DB_PASS $DB_NAME | gzip > $BCK_DIR/$DB_NAME.dump_$DATE.sql.gz
sleep 1
#####
DB_NAME="imms_app"
$BIN_DIR/mysqldump -u$DB_USER -p$DB_PASS $DB_NAME | gzip > $BCK_DIR/$DB_NAME.dump_$DATE.sql.gz
echo "mysqldump end at "$DATE
注意:
目录/data/mysql_dump/和目录/data/logs/mysql要有www用户的权限 最好给他改成777权限
要先切换到www用户
[www@iZ232s4zbqvZ ~]$ crontab -e
# imms mysqldump
01 01 * * * /usr/local/service/mysql_dump.sh >> /data/logs/mysql/mysqldump.log
恢复时先用gunzip解压
如:
[root@iZ232s4zbqvZ 2015]# gunzip imms_core.dump_20150211_133301.sql.gz