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

How to use shell script to back up MySQL Database regularly

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

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the knowledge of "how to use shell scripts for regular backup of MySQL database". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

#! / bin/bash # Shell Command For Backup MySQL Database Everyday Automatically By Crontab # Author: Carlos Wong # Date: 2012-03-17 # configuration parameter USER=root # database user name "> user name PASSWORD= × × × # database user password DATABASE=TIENIUZAI # database name WEBMASTER=tieniuzai@qq.com # administrator mailbox address Used to send a backup failure message to remind BACKUP_DIR=/var/www/Data_Backup/topons/ # backup file storage path LOGFILE=/var/www/Data_Backup/topons/data_backup.log # journal file path DATE= `date'+% Y% m% dmi% H% M`` # date format (as file name) DUMPFILE=$DATE.sql # backup file name ARCHIVE=$DATE.sql.tgz # compressed file name OPTIONS= "- u$USER-p$PASSWORD-opt-extended-insert=false-triggers=false-R-hex-blob-flush-logs-B $DATABASE" # mysqldump parameters for more information, see help mysqldump-help # to 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 "> > $LOGFILE echo" > > $LOGFILE echo "--> > $LOGFILE echo" BACKUP DATE: "$(date +"% y-%m-%d% H:%M:%S ") > > $LOGFILE echo"-- > > $LOGFILE # switch to the backup directory Cd $BACKUP_DIR # backup the development database using the mysqldump command And name the backup file mysqldump $OPTIONS > $DUMPFILE # with the formatted timestamp to determine whether the database backup is successful or not if [$? = = 0]] Then # create the backup file archive 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 archive of the database backup file rm-f $DUMPFILE else echo "Database Backup Fail!" > > $LOGFILE # send an email reminder to the website administrator after the backup fails Need support for mailutils or similar terminal sending mail tools # mail-s "Database:$DATABASE Daily Backup Fail" $WEBMASTER fi # output echo "Backup Process Done" reminder message of the end of the backup process

Use:

Save the above code to: / usr/sbin/DataBackup (the file name is arbitrary, as long as it is not the same as the original command of the system; the code can be placed anywhere, just to facilitate execution under the sbin directory, and the file / directory under the sbin directory can be called directly on the terminal, similar to the directory specified by the following PATH variable)

Add executable permissions to the script: sudo chmod + x / usr/sbin/DataBackup

Execute script: sudo DataBackup

If you need to execute backup commands regularly, simply put the following code in the crontab file (sudo vim / etc/crontab):

01 3 * root / usr/sbin/DataBackup # it represents that the DataBackup script will be executed at 3 o'clock every day

This is the end of the content of "how to use shell script for regular backup of MySQL database". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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