vim mysqlbak.sh
内容如下
BACKUP=/home/backup/sql/
DATETIME=$(date +%Y-%m-%d_%H%M%S)
echo "===备份开始==="
echo "备份文件存放于${BACKUP}/$DATETIME.sql.gz"
DUMP=/usr/bin/mysqldump
HOST=localhost
PORT=3306
DB_USER=root
DB_PW=root
[ ! -d "${BACKUP}/$DATETIME" ] && mkdir -p "${BACKUP}/$DATETIME"
DATABASE=znk
$DUMP -u${DB_USER} -p${DB_PW} -h${HOST} -P${PORT} -q -R --databases $DATABASE | gzip > ${BACKUP}/$DATETIME/$DATABASE.sql.gz
cd $BACKUP
tar -zcvf $DATETIME.tar.gz $DATETIME
rm -rf ${BACKUP}/$DATETIME
find $BACKUP -mtime +10 -name "*.sql.gz" -exec rm -rf {} \;
echo "===备份成功==="
crontab -e
内容如下
# */1 * * * * /home/backup/mysqlbak.sh
0 14,21 * * * /home/backup/mysqlbak.sh
网友评论