代码敲得再六又怎么样...

ubuntu上使用shell备份MySQL数据库,并使用7z压缩sql文件

81 2020-12-16 08:06:56

安装7z

sudo apt-get install p7zip

shell备份脚本,保留两天的数据

#!/bin/sh
# Database info
DB_USER="root"
DB_PASS="123456"
DB_HOST="127.0.0.1"
DB_NAME="test"

# Others vars
BIN_DIR="/usr/bin/mysqldump"            #the mysql bin path
BCK_DIR="/home/DBback/"    #the backup file directory
# 获取今天日期
DATE=`date +%F`
# 前两天时间
LAST_DATE=`date -d "2 days ago" +%F`
NAME = $BCK_DIR$DB_NAME-$DATE
OLD_NAME = $BCK_DIR$DB_NAME-$LASE_DATE
# TODO
# /usr/bin/mysqldump --opt -ubatsing -pbatsingpw -hlocalhost timepusher > /mnt/mysqlBackup/db_`date +%F`.sql
# $BIN_DIR/mysqldump --opt -u$DB_USER -p$DB_PASS -h$DB_HOST $DB_NAME > $BCK_DIR/db_$DATE.sql

$BIN_DIR --opt -u$DB_USER -p$DB_PASS -h$DB_HOST $DB_NAME > $NAME.sql

#3.先导出为.sql脚本,然后再进行压缩(打包后删除原文件)
cd $BF_DIR/
/bin/7z a -t7z $NAME.7z $NAME.sql --remove &>/dev/null
if [  -f "$OLD_NAME.7z" ];then
rm -f $OLD_NAME.7z
fi