Mysql数据库备份脚本(使用mysqldump)
Mysql(mysqldump)备份脚本 记录下服务器上一个备份 mysql 数据库的脚本,使用 mysql 自带的mysqldump命令 !/usr/bin/env bash USER=username PASSWORD=password MAXIMUM_BACKUP_FILES=10 BACKUP_FOLDER=/path/to/save/folder DATABASES=( db_name_0 db_name_1 ) # check mysqldump instlled _=$(command -v mysqldump) if [[ $? != 0 ]] then printf "You don't seem to mysqldump installed, exit..\n" exit 1 fi # create backup folder if [ ! -d $BACKUP_FOLDER ] then mkdir $BACKUP_FOLDER fi # backup for DB in ${DATABASES[@]} do echo backing up ${DB} database ... if $(mysqldump --host=localhost --user=${USER} --password=${PASSWORD} ${DB} | gzip -9 > ${BACKUP_FOLDER}/db_${DB}_$(date +"%Y%m%d").sql.gz) then echo dump db_${DB}_$(date +"%Y%m%d").sql.gz done. else echo dump db_${DB}_$(date +"%Y%m%d").sql.gz failed. fi done # remove older files find ${BACKUP_FOLDER} -type f -name *.sql.gz -mtime +${MAXIMUM_BACKUP_FILES} -delete 脚本就是使用 mysqldump 备份指定的数据库(在 DATABASES 使用空格分隔)然后 gzip 压缩保存到指定目录,使用系统自带的 find 命令删除旧文件 ...