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
There is a picture in a big box. When you put the...
Table of contents 1. Route navigation 2. History ...
Official documentation: https://dev.mysql.com/doc...
I searched for three-level linkage on the Interne...
Table of contents 1. Global level 2. Database lev...
Table of contents 1. Operators Summarize 1. Opera...
Table of contents 1. router-view 2. router-link 3...
Table of contents introduction 1. What is one-cli...
Table of contents DOM node Element node: Text nod...
Copy code The code is as follows: <html> &l...
When creating a time field DEFAULT CURRENT_TIMEST...
Table of contents 1. Introduction 2. Main text 2....
When I first started, I found a lot of errors. In...
First of all, we need to know what a state manage...
Preface In MySQL, we can use the EXPLAIN command ...