#!/bin/bash
export PATH=/bin:/usr/bin:/usr/local/bin
# 备份目录名称
TODAY=`date +"%Y-%m-%d-%H-%M-%S"`
# 备份文件存放路径
DB_BACKUP_PATH='/data/backup/database'
# 数据库服务器地址
MYSQL_HOST='127.0.0.1'
MYSQL_PORT='3306'
# 备份账户,建议创建一个只读账户使用
MYSQL_USER='data_backup'
# 备份账户密码
MYSQL_PASSWORD='123456'
# 需要备份的数据库
DATABASES=("ical" "weather" "wordpress" "proftpd")
echo "Backup started ..."
mkdir -p ${DB_BACKUP_PATH}/${TODAY}
for db in ${DATABASES[@]}; do
echo "Backup started for database - $db ... "
mysqldump -h ${MYSQL_HOST} -P ${MYSQL_PORT} -u ${MYSQL_USER} -p${MYSQL_PASSWORD} \
--single-transaction --routines --triggers --quick \
${db} | gzip > ${DB_BACKUP_PATH}/${TODAY}/${db}.sql.gz
if [ $? -eq 0 ]; then
echo "Database backup successfully completed"
else
echo "Error found during backup"
exit 1
fi
done
# 删除3天前的备份
find /data/backup/database/* -mtime +3 -type d | xargs rm -rf
文章评论