当前位置:网站首页>Linux MySQL data timing dump

Linux MySQL data timing dump

2022-04-23 16:49:00 A coir boat in the broken white clouds

One 、 Back up the database script file

#!/bin/sh
#  Database account information 
DB_USER="root"
DB_PWD="123456"
DB_HOST="192.168.110.195"
DB_PORT="3305"

# MYSQL In the directory 
MYSQL_DIR="/usr/local/mysql"
#  Backup file storage directory 
BAK_DIR="/data/backups"
#  Time format , Such as  20200902
DATE=`date +%Y%m%d`
#  The number of days the backup script is saved 
DEL_DAY=10

#  Database to back up , The blank space to separate 
DATABASES=("sales_release")

#  Create date directory 
mkdir $BAK_DIR/$DATE

echo "-------------------$(date +%F_%T) start ---------------"  >>${BAK_DIR}/aggrega
te_backup.log
for database in "${DATABASES[@]}"
do
    #  Execute backup command 
    $MYSQL_DIR/bin/mysqldump --opt -u$DB_USER -p$DB_PWD -h$DB_HOST -P$DB_PORT ${database} > $BAK_DIR/$DATE/${database}.sql
done

echo "--- backup file created: $BAK_DIR/aggregate_backup_$DATE.tar.gz"  >>${BAK_DIR}/aggregate_backup.log

#  Back up the good sql The script is compressed to aggregate_backup_yyyyMMdd.tar.gz
tar -czf $BAK_DIR/aggregate_backup_$DATE.tar.gz $BAK_DIR/$DATE

#  After the compression , Delete backup files and directories before compression 
rm -f $BAK_DIR/$DATE/*
rmdir $BAK_DIR/$DATE

#  So far, the backup operation has been completed , The following script command is used to clean up the old files backed up before , To free up disk space 

#  Traverse the compressed files in the backup directory 
LIST=$(ls ${
     BAK_DIR}/aggregate_backup_*)
#  obtain 10 The day before , Used for comparison , Files older than this time will be deleted 
SECONDS=$(date -d "$(date +%F) -${DEL_DAY} days" +%s) for index in ${LIST} do #  Format the file name , Take the time at the end of the name , The format is as follows  20200902 timeString=$(echo ${
      index} | egrep -o "?[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]") if [ -n "$timeString" ]
    then
        indexDate=${timeString//./-}
        indexSecond=$( date -d ${
     indexDate} +%s )
        #  Compare with the time of the day , Earlier than 10 Days of backup file deletion 
        if [ $(( $SECONDS- $indexSecond )) -gt 0 ]
        then
            rm -f $index
            echo "---- deleted old backup file : $index " >>${BAK_DIR}/aggregate_backup.log
        fi
    fi
done

echo "-------------------$(date +%F_%T) end ---------------"  >>${BAK_DIR}/aggregate_backup.log

Be careful : If it is Windows The document was sent directly to Linux Need to convert the format

sed -i 's/\r$//' aggregateBak.sh

Two 、 Give the script permission to execute

chmod 777 aggregateBak.sh

3、 ... and 、 utilize Linux Systematic crontab Timing task , To automatically execute scripts at a fixed time

crontab -e # edit 
# Enter the following code 
00 03 * * * /data/mysql_aggregate/aggregateBak.sh# stay  03:00 a.m  function 
:wq # Save and exit 
service crond restart # Restart the service , If it is ubuntu System , It is cron

Backup is a good habit , A habit that won't make you lose money

版权声明
本文为[A coir boat in the broken white clouds]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231400068254.html