Get the App
SLTechnology News&Howtos  ›  Servers  › 

MySQL local database backup script handout

Shulou Source: shulou.com Published: 2022-06-03 06:43:17 10月01日 Update

This article mainly introduces the handout of the MySQL local database backup script, hoping to supplement and update some knowledge, if you have any other questions you need to know, you can continue to follow my updated article in the industry information.

This script is used for scheduled backups of local databases and scheduling tasks based on crontab

#! / bin/bash # Shell Command For Backup MySQL Database Everyday Automatically By Crontab USER=root # Database login user name PASSWORD= "1qaz2wsx" # Database login password DATABASE= "mysql" # Library to be backed up HOSTNAME= "192.168.18.5" # backup host IPWEBMASTER=test@qq.comBACKUP_DIR=/data/mysql _ back/ # backup file storage path LOGFILE=/data/data_backup.log # journal file path DATE= `date'+% Y% m% dhorse% H% M`` # date format (as file name) DUMPFILE=$DATE.sql # backup file name ARCHIVE=$DATE.sql.tgz # compressed file name OPTIONS= "- h$HOSTNAME-u$USER-p$PASSWORD $DATABASE" # mysqldump-help # determine whether the backup file storage directory exists Otherwise, create the directory if [!-d $BACKUP_DIR] Then mkdir-p "$BACKUP_DIR" fi# before starting backup Write the backup header to the diary file echo "> > $LOGFILEecho" > > $LOGFILEecho "- > > $LOGFILEecho" BACKUP DATE: "$(date +"% y-%m-%d% H:%M:%S ") > > $LOGFILEecho"-"> > $LOGFILE# to the backup directory cd $BACKUP_DIR# use the mysqldump command to back up the development database And use the formatted timestamp to name the backup file mysqldump $OPTIONS > $DUMPFILE# to determine whether the database backup is successful or not if [[$? = = 0]] Then # create an archive of backup files tar czvf $ARCHIVE $DUMPFILE > > $LOGFILE 2 > & 1 # enter the message of successful backup to the diary file echo "[$ARCHIVE] Backup Successful!" > > $LOGFILE # delete the original backup file and simply keep the compressed package of the database backup file rm-f $DUMPFILEelse echo "Database Backup Fail!" > > $LOGFILEfi# outputs a reminder that the backup process is over

Echo "Backup Process Done"

Read the above handouts on the MySQL local database backup script, hoping to give you some help in practical application. Due to the limited space in this article, it is inevitable that there will be deficiencies and need to be supplemented. If you need more professional answers, you can contact us on the official website for 24-hour pre-sales and after-sales to help you answer questions at any time.

Tags: Backup file data database script file name diary directory handout success format message path question storage update login solution original professional Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Xiaomi MariaDB Linux Apple