BackUpMysql.sh script #!/bin/bash PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin export PATH #Database ip DBHOST='' #Database username DBUSER='' #Database password DBPASSWD='' #Databases that need to be backed up, multiple databases are separated by spaces DBNAME='' #Backup time backtime=`date +%Y-%m-%d_%H%M%S` #Backup path (current directory) BACKPATH=$(dirname $(readlink -f $0)) echo $BACKPATH #Log backup path LOGPATH="${BACKPATH}/log" #Data backup path DBPATH="${BACKPATH}/db" #Create a backup directory [ ! -d "${LOGPATH}" ] && mkdir -p "${LOGPATH}" [ ! -d "${DBPATH}" ] && mkdir -p "${DBPATH}" #Log record header echo "Backup time is ${backtime}, backup database table ${DBNAME} starts" >> ${LOGPATH}/mysqlback.log #Formal backup database for table in $DBNAME; do source=`mysqldump -u ${DBUSER} -h${DBHOST} -p${DBPASSWD} ${table}> ${LOGPATH}/${backtime}.sql` 2>> ${LOGPATH}/mysqlback.log; #Backup succeeds the following operations $? Get the result of the previous command, 0 represents success if [ "$?" == 0 ]; then cd ${LOGPATH} #To save hard disk space, compress the database tar -czf ${DBPATH}/${table}${backtime}.tar.gz ./${backtime}.sql > /dev/null #Delete the original file and keep only the compressed file rm -f ${LOGPATH}/${backtime}.sql #Delete the backup seven days ago, that is, only save the backup within 7 days find $DBPATH -name "*.tar.gz" -type f -mtime +7 -exec rm -rf {} \; > /dev/null 2>&1 echo "Database table ${DBNAME} is backed up successfully!!" >> ${LOGPATH}/mysqlback.log else #If the backup fails, perform the following operations echo "Database table ${DBNAME} backup failed!!" >> ${LOGPATH}/mysqlback.log fi done Use crontab to execute the BackUpMysql.sh script regularly, and configure it to execute at 12:00 every night Run crontab -e enter 59 23 * * * /data/mysqlbak/BackUpMysql.sh
The above is the detailed content of the Mysql database scheduled backup script. For more information about the Mysql scheduled backup script, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
>>: Analysis of two implementation methods for adding static routing in Linux
Prepare war package 1. Prepare the existing Sprin...
JDK download address: http://www.oracle.com/techn...
A brief analysis of rem First of all, rem is a CS...
This article records the installation tutorial of...
Table of contents Initial Vue Building a Vue deve...
Purpose Understand the role of nextTick and sever...
Although I have run some projects in Docker envir...
Because I need to use Ubuntu+Python 3.6 version t...
Table of contents Styles in uni-app Summarize Sty...
The property of centering text in CSS is very simp...
1. Pull the image First, execute the following co...
HTML tags have special tags to handle the title of...
Table of contents el-scrollbar scroll bar el-uplo...
This article mainly explains the style of buttons...
Let's first understand a wave of concepts, wh...