Linux Shell经典面试题(其五)

Linux Shell经典面试题(其五)

参考:http://www.voidcn.com/article/p-kbsgeezi-bcx.html

1.如何实现对MySQL数据库进行分库备份,请用脚本实现。

############mysql的分库备份脚本mysql-differ-database-backups.sh 的脚本编写##############

$ vi mysql-differ-database-backups.sh

#!/bin/bash
MYUSER=root
MYPASS=password
SOCKET="/var/lib/mysql/mysql.sock"
MYCMD="mysql -u$MYUSER -p$MYPASS"
MYDUMP="mysqldump -u$MYUSER -p$MYPASS"
BACKUP_DIR="/work/backup/mysql"

for database in `$MYCMD -e "show database;"|sed '1,2d' | egrep -v "mysql|schema"`
do
    if [! -f $BACKUP_DIR/${database} ];then
        mkdir -p $BACKUP_DIR/${database}
    fi
    $MYDUMP ${database} | gzip > $BACKUP_DIR/${database}_`date +%F`.tar.gz
done

2.如何实现对MySQL数据库进行分库加分表备份,请用脚本实现。

############mysql的分库+分表备份脚本mysql-differ-database-table-backups.sh 的脚本编写##############

$ vi mysql-differ-database-table-backups.sh

#!/bin/bash
MYUSER=root
MYPASS=password
SOCKET="/var/lib/mysql/mysql.sock"
MYCMD="mysql -u$MYUSER -p$MYPASS"
MYDUMP="mysqldump -u$MYUSER -p$MYPASS"
BACKUP_DIR="/work/backup/mysql"

for database in `$MYCMD -e "show database;"|sed '1,2d' | egrep -v "mysql|schema"`
do
    if [! -f $BACKUP_DIR/${database} ];then
        mkdir -p $BACKUP_DIR/${database}
    fi
    $MYDUMP ${database} | gzip > $BACKUP_DIR/${database}_`date +%F`.tar.gz

    for i in $database
        do
            tables=`$MYCMD -e "use $i;show tables;"|sed 1d`
            for j in $tables
                do
                    MYDUMP   -B --databases $i --tables $j > $BACKUP_DIR/${i}-${j}-`date +%F`.sql
                done
        done
done
相关文章

相关标签/搜索