自动备份mysql数据库脚本

#!/bin/sh

# NCONF database backup

# Script by Chen


# add (and adapt) the following line to the corresponding user's crontab:

# 50 23 * * * /usr/local/nconf/ADD-ONS/backup_db.sh


# MYSQL connection parameters (see config/mysql.php)

DBHOST=localhost

DBPORT=3306

DBNAME=test

DBUSER=user

DBPASS=pass


# Other variables

DESTINATION=/usr/local/nconf/BACKUP/db

DATE=`date +%Y%m%d%H%M%S`

KEEPDAY=30


# run backup

mysqldump --host=$DBHOST --port=$DBPORT -u $DBUSER --password=$DBPASS --single-transaction $DBNAME | gzip > $DESTINATION/$DBNAME-$DATE.sql.gz


# delete old backups

find $DESTINATION/ -type f -name "$DBNAME-*.sql.gz" -mtime +$KEEPDAY -exec rm {} \;