又要开始做运维了…… 每次备份数据库都是个头痛的事情。
前几年维护服务器的时候曾经用过一个脚本,很省心。后来服务器电源崩掉导致 raid5 失效…… 这是个悲伤的故事。
下面分享出来一个脚本。原文地址。
#!/bin/bash
cd /root/backups # 备份文件路径
MAXIMUM_BACKUP_FILES=10 # 最大备份文件数
BACKUP_FOLDERNAME="database_backup" # 数据库备份文件的主目录
DB_HOSTNAME="localhost" # mysql 所在主机的主机名
DB_USERNAME="root" # mysql 登录用户名
DB_PASSWORD="password" # mysql 登录密码
# 如果是Ubuntu系统,可以试试下面这个选项
#DB_USERNAME="debian-sys-maint" # Ubuntu内置的系统维护用户
#DB_PASSWORD=$(cat /etc/mysql/debian.cnf | grep 'password' | head -n 1 | cut -d " " -f3) # 此用户对应的密码
DATABASES=(
"database_1"
"database_2" #备份的数据库名
)
#=========
echo "Bash Database Backup Tool"
#CURRENT_DATE=$(date +%F)
CURRENT_DATE=$(date +%F) #定义当前日期为变量
BACKUP_FOLDER="${BACKUP_FOLDERNAME}_${CURRENT_DATE}" #存放数据库备份文件的目录
mkdir $BACKUP_FOLDER #创建数据库备份文件目录
# 统计需要被备份的数据库
count=0
while [ "x${DATABASES[count]}" != "x" ];do
count=$(( count + 1 ))
done
echo "[+] ${count} databases will be backuped..."
# 循环这个数据库名称列表然后逐个备份这些数据库
for DATABASE in ${DATABASES[@]};do
echo "[+] Mysql-Dumping: ${DATABASE}"
echo -n "Began:";echo $(date)
if $(mysqldump -h ${DB_HOSTNAME} -u${DB_USERNAME} -p${DB_PASSWORD} ${DATABASE} > "${BACKUP_FOLDER}/${DATABASE}.sql");then
echo "Dumped successfully!"
else
echo "Failed dumping this database!"
fi
echo -n "Finished:";echo $(date)
done
echo
echo "[+] Packaging and compressing the backup folder..."
tar -cv ${BACKUP_FOLDER} | bzip2 > ${BACKUP_FOLDER}.tar.bz2 && rm -rf $BACKUP_FOLDER
BACKUP_FILES_MADE=$(ls -l ${BACKUP_FOLDERNAME}*.tar.bz2 | wc -l)
BACKUP_FILES_MADE=$(( $BACKUP_FILES_MADE - 0 ))
# 把已经完成的备份文件数的结果转换成整数数字
echo
echo "[+] There are ${BACKUP_FILES_MADE} backup files actually."
# 判断如果已经完成的备份文件数比最大备份文件数要大,那么用已经备份的文件数减去最大备份文件数, 打印要删除旧的备份文件
if [ $BACKUP_FILES_MADE -gt $MAXIMUM_BACKUP_FILES ];then
REMOVE_FILES=$(( $BACKUP_FILES_MADE - $MAXIMUM_BACKUP_FILES ))
echo "[+] Remove ${REMOVE_FILES} old backup files."
# 统计所有备份文件,把最新备份的文件存放在一个临时文件里,然后删除旧的文件,循环出临时文件的备份文件从临时目录里移到当前目录
ALL_BACKUP_FILES=($(ls -t ${BACKUP_FOLDERNAME}*.tar.bz2))
SAFE_BACKUP_FILES=("${ALL_BACKUP_FILES[@]:0:${MAXIMUM_BACKUP_FILES}}")
echo "[+] Safeting the newest backup files and removing old files..."
FOLDER_SAFETY="_safety"
if [ ! -d $FOLDER_SAFETY ]
then mkdir $FOLDER_SAFETY
fi
for FILE in ${SAFE_BACKUP_FILES[@]};do
mv -i ${FILE} ${FOLDER_SAFETY}
done
rm -rf ${BACKUP_FOLDERNAME}*.tar.bz2
mv -i ${FOLDER_SAFETY}/* ./
rm -rf ${FOLDER_SAFETY}
fi
最后记得将这个脚本给个 x 权限,并加入 crontab。这样就省心了。
发表回复