linux自动备份数据库

@fright  November 16, 2017
#!/bin/sh

cd /data/backup/
#dt=`date '+%Y%m%d-%H%M%S'`
dt=`date '+%Y%m%d'`
backupdir="/data/backup/";

备份结构和数据并压缩

filename="bx_db-${dt}.sql"
websqlname="${backupdir}${filename}"
mysqldump -h192.168.0.108 -uroot -proot baoxiu > ${websqlname}
tar -zcvf ${filename}.tar.gz ${filename}
rm -rf ${filename}

备份结构压缩

filename="bx-nodata-db-${dt}.sql"
websqlname="${backupdir}${filename}"
mysqldump -h192.168.0.108 -uroot -proot --no-data --databases baoxiu > ${websqlname}
tar -zcvf ${filename}.tar.gz ${filename}
rm -rf ${filename}

多数据备份案例

#!/bin/sh
cd /data/backup/
dt=`date '+%Y%m%d-%H%M%S'`
#dt=`date '+%Y%m%d'`
backupdir="/data/backup/";

databaseArr=("db1" "db2" "db3")
for database in ${databaseArr[@]};
do
    filename="${database}-${dt}.sql"
    websqlname="${backupdir}${filename}"
    mysqldump -h127.0.0.1 -u用户名 -p密码 ${database} > ${websqlname}
    tar -zcvf ${filename}.tar.gz ${filename}
    rm -rf ${filename}
done

评论已关闭