简体   繁体   English

使用rsnapshot和mysqldump备份远程mySQL数据库

[英]Using rsnapshot and mysqldump to backup remote mySQL Databases

I am using rsnapshot to backup our remote servers and I need to add MySQL backups. 我正在使用rsnapshot备份我们的远程服务器,并且需要添加MySQL备份。

rsnapshot calls a remote script that dumps the mysql DB's on the server. rsnapshot调用一个远程脚本,该脚本将mysql DB转储到服务器上。 The problem is, the script does not exclude the information_schema database and therefore it dies )or appears to die and I have a second problem.) 问题是,脚本未排除information_schema数据库,因此该脚本死了或似乎死了,而我还有第二个问题。)

I'm not sure how I would exclude the information_schema DB from this script: 我不确定如何从此脚本中排除information_schema数据库:

### SETUP MYSQL LOGIN ###
MUSER='USER'
MPASS='PASSWORD'
MHOST="127.0.0.1"

### Set to 1 if you need to see progress while dumping dbs ###
VERBOSE=0

### Set bins path ###
GZIP=/bin/gzip
MYSQL=/usr/bin/mysql
MYSQLDUMP=/usr/bin/mysqldump
RM=/bin/rm
MKDIR=/bin/mkdir
MYSQLADMIN=/usr/bin/mysqladmin
GREP=/bin/grep

### Setup dump directory ###
BAKRSNROOT=/tmp/rsnapshot/mysql

#####################################
### ----[ No Editing below ]------###
#####################################
### Default time format ###
TIME_FORMAT='%H_%M_%S%P'

### Make a backup ###
backup_mysql_rsnapshot(){
        local DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')"
        local db="";
        [ ! -d $BAKRSNROOT ] && ${MKDIR} -p $BAKRSNROOT
        ${RM} -f $BAKRSNROOT/* >/dev/null 2>&1
        [ $VERBOSE -eq 1 ] && echo "*** Dumping MySQL Database ***"
        [ $VERBOSE -eq 1 ] && echo -n "Database> "
        for db in $DBS
        do
                local tTime=$(date +"${TIME_FORMAT}")
                local FILE="${BAKRSNROOT}/${db}.${tTime}.gz"
                [ $VERBOSE -eq 1 ] && echo -n "$db.."
                ${MYSQLDUMP} -u ${MUSER} -h ${MHOST} -p${MPASS} $db | ${GZIP} -9 > $FILE
        done
                [ $VERBOSE -eq 1 ] && echo ""
                [ $VERBOSE -eq 1 ] && echo "*** Backup done [ files wrote to $BAKRSNROOT] ***"
}

### Die on demand with message ###
die(){
        echo "$@"
        exit 999
}

### Make sure bins exists.. else die
verify_bins(){
        [ ! -x $GZIP ] && die "File $GZIP does not exists. Make sure correct path is set in $0."
        [ ! -x $MYSQL ] && die "File $MYSQL does not exists. Make sure correct path is set in $0."
        [ ! -x $MYSQLDUMP ] && die "File $MYSQLDUMP does not exists. Make sure correct path is set in $0."
        [ ! -x $RM ] && die "File $RM does not exists. Make sure correct path is set in $0."
        [ ! -x $MKDIR ] && die "File $MKDIR does not exists. Make sure correct path is set in $0."
        [ ! -x $MYSQLADMIN ] && die "File $MYSQLADMIN does not exists. Make sure correct path is set in $0."
        [ ! -x $GREP ] && die "File $GREP does not exists. Make sure correct path is set in $0."
}

### Make sure we can connect to server ... else die
verify_mysql_connection(){
        $MYSQLADMIN  -u $MUSER -h $MHOST -p$MPASS ping | $GREP 'alive'>/dev/null
        [ $? -eq 0 ] || die "Error: Cannot connect to MySQL Server. Make sure username and password are set correctly in $0"
}

### main ####
verify_bins
verify_mysql_connection
backup_mysql_rsnapshot

Look into mysqldump and its --all-databases parameter. 查看mysqldump及其--all-databases参数。 Don't reinvent the wheel! 不要重新发明轮子!

mysqldump does not dump theINFORMATION_SCHEMA database. mysqldump不转储INFORMATION_SCHEMA数据库。 If you name that database explicitly on the command line,mysqldump silently ignores it. 如果您在命令行上明确命名该数据库,mysqldump会静默忽略它。

If you have a version of mysqldump that does really not exclude INFORMATION_SCHEMA and you need all DBs in seperate files, just exclude it from your loop: 如果您的mysqldump版本确实没有排除INFORMATION_SCHEMA,并且您需要将所有DB放在单独的文件中,则将其从循环中排除:

for db in $DBS 
do 
    if [ $db -ne "INFORMATION_SCHEMA" ] ; do
        YOUR_STUFF()
    done
dome

Replace the following line: 替换以下行:

local DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')"

with

local DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases' -s --skip-column-names|grep -vi information_schema)"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM