Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Abin backup script

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

#! / bin/sh

#

# when it is executed for the first time, it will check whether there is a full backup, otherwise create a full library backup first

# when you run it again, it will perform incremental backups based on previous full-library backups according to the settings in the script

Commands for INNOBACKUPEX_PATH=innobackupex # INNOBACKUPEX

Command path of INNOBACKUPEXFULL=/usr/bin/$INNOBACKUPEX_PATH # INNOBACKUPEX

# mysql target server and username and password

MYSQL_CMD= "- host=192.16.2.11-user=root-password=123456-port=3306"

User name and password of MYSQL_UP= "--user=root-- password=123456-p3306" # mysql

TMPLOG= "/ tmp/innobackupex.$$.log"

Configuration file for MY_CNF=/etc/my.cnf # mysql

MYSQL=/usr/bin/mysql

MYSQL_ADMIN=/usr/bin/mysqladmin

Home directory of BACKUP_DIR=/backup # backup

FULLBACKUP_DIR=$BACKUP_DIR/full # Directory of full library backup

Directory for INCRBACKUP_DIR=$BACKUP_DIR/incre # incremental backups

Interval period of FULLBACKUP_INTERVAL=259200 # full library backups, time: seconds

KEEP_FULLBACKUP=1 # keeps at least a few full library backups

Logfiledate= backup.`date +% Y% m% d% H% M`.txt

# start time

STARTED_TIME= `date +% s`

# date +% s seconds since 00:00:00 UTC on January 1, 1970

#

# display an error and exit

#

Error ()

{

Echo "$1" 1 > & 2

Exit 1

}

# check the execution environment

If [!-x $INNOBACKUPEXFULL]; then

Error "$INNOBACKUPEXFULL is not installed or linked to / usr/bin."

Fi

#-x Test whether you have the right to execute

If [!-d $BACKUP_DIR]; then

Error "backup destination folder: $BACKUP_DIR does not exist."

Fi

If [- z "`$MYSQL_ADMIN $MYSQL_UP status | grep 'Uptime'`]; then

Error "MySQL is not up and running."

Fi

#-z empty string

If! `MYSQL_ 'exit' | $MYSQL-s $MYSQL_ CMD`; then

Error "the database username or password provided is incorrect!"

Fi

# mysql-s,-- silent / / one line of output, separated by tab

# header information of backup

Echo "-"

Echo

Echo "$0: MySQL backup script"

Echo "starts with: `date +% F'% T'% w`"

Echo

# the $0 above indicates the name of the currently running process or the name of the currently executed script

# create directories for full and differential backups

Mkdir-p $FULLBACKUP_DIR

Mkdir-p $INCRBACKUP_DIR

# find the latest full backup

LATEST_FULL_BACKUP= `find $FULLBACKUP_DIR-mindepth 1-maxdepth 1-type d-printf "% P\ n" | sort-nr | head-1`

#-mindepthd 1, indicating that the search starts at layer 0 of the $FULLBACKUP_DIR directory;-maxdepth 1, indicating that the depth of listing the files in $FULLBACKUP_DIR is up to 0 layer

# find the latest backup that was recently modified

LATEST_FULL_BACKUP_CREATED_TIME= `stat-c% Y $FULLBACKUP_DIR/$LATEST_FULL_ backUP`

# stat-c% Y get the modification time of the file

# if full and effective incremental backup is performed, otherwise perform a full backup

If ["$LATEST_FULL_BACKUP"-a `expr $LATEST_FULL_BACKUP_CREATED_TIME + $FULLBACKUP_INTERVAL + 5`-ge $STARTED_TIME]; then

# if the latest full directory is not expired, create a new directory under the incremental backup directory with the latest full file name

Echo-e "full backup $LATEST_FULL_BACKUP has not expired and will be named as the incremental backup directory according to the $LATEST_FULL_BACKUP name"

Echo ""

NEW_INCRDIR=$INCRBACKUP_DIR/$LATEST_FULL_BACKUP

Mkdir-p $NEW_INCRDIR

# find out whether the latest incremental backup exists. Specify the path of a backup as the basis for incremental backup

LATEST_INCR_BACKUP= `find $NEW_INCRDIR-mindepth 1-maxdepth 1-type d | sort-nr | head-1`

If [! $LATEST_INCR_BACKUP]; then

INCRBASEDIR=$FULLBACKUP_DIR/$LATEST_FULL_BACKUP

Echo-e "incremental backup will be based on $INCRBASEDIR"

Echo ""

Else

INCRBASEDIR=$LATEST_INCR_BACKUP

Echo-e "incremental backup will be based on $INCRBASEDIR"

Echo ""

Fi

Echo "uses $INCRBASEDIR as the basis for new incremental backups."

$INNOBACKUPEXFULL-defaults-file=$MY_CNF-use-memory=4G $MYSQL_CMD-incremental $NEW_INCRDIR-incremental-basedir $INCRBASEDIR > $TMPLOG 2 > & 1

Else

Echo "* *"

Echo-e "is performing a new full backup... just a moment, please."

Echo "* *"

$INNOBACKUPEXFULL-defaults-file=$MY_CNF-use-memory=4G $MYSQL_CMD $FULLBACKUP_DIR > $TMPLOG 2 > & 1

Fi

#-use-memory this parameter is used in prepare to control the amount of memory used by innodb instances in prepare

# keep a detailed log of a backup

Cat $TMPLOG > / backup/$logfiledate

If [- z "`tail-1$ TMPLOG | grep 'innobackupex: completed Okla``"]; then

Echo "$INNOBACKUPEX command execution failed:"; echo

Echo-e "- $INNOBACKUPEX_PATH error -"

Cat $TMPLOG

Rm-f $TMPLOG

Exit 1

Fi

THISBACKUP= `awk-"/ Backup created in directory/ {split (\\ $0, p,\"'\ "); print p [2]}" $TMPLOG`

Rm-f $TMPLOG

Echo-n "Database successfully backed up to: $THISBACKUP"

Echo

# prompt the starting point of the backup file that should be retained

LATEST_FULL_BACKUP= `find $FULLBACKUP_DIR-mindepth 1-maxdepth 1-type d-printf "% P\ n" | sort-nr | head-1`

Echo-e "must keep $KEEP_FULLBACKUP full and all incremental backups after $LATEST_FULL_BACKUP."

# Delete expired completeness

Echo-e "look for expired complete files and delete" > > / backup/$logfiledate

For efile in $(/ usr/bin/find $FULLBACKUP_DIR/-mtime + 6)

Do

If [- d $efile]; then

Rm-rf $efile

Echo-e "Delete expired complete files: $efile" > > / backup/$logfiledate

Elif [- f $efile]; then

Rm-rf $file

Echo-e "Delete expired complete files: $efile" > > / backup/$logfiledate

Fi

Done

If [$?-eq "0]; then

Echo

Echo-e "No expired complete files found to delete"

Fi

Echo

"echo" is completed at: `date +% F''% T'

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report