MySQL database shell script automatic backup, mysqlshell script

Source: Internet
Author: User

MySQL database shell script automatic backup, mysqlshell script

Automatic Backup of shell scripts in MySQL database

It is a good habit to back up databases frequently. Although the probability of database corruption or data loss is very low, it is useless to regret such a situation. Generally, the back-end of a website or application has a button to back up the database, but it needs to be executed manually. We need a secure, daily automatic backup method. The following shell script allows you to back up the MySQL database every day by setting Crontab.

#! /Bin/bash # database authentication user = "" password = "" host = "" db_name = "" # other backup_path = "/path/to/your/home/_ backup/ mysql "date = $ (date +" % d-% B-% Y ") # Set the default permissions for Exported Files umask 177 # Dump database to SQL file mysqldump -- user = $ user -- password = $ password -- host = $ host $ db_name> $ backup_path/$ db_name- $ date. SQL

Through the above script, we can export an SQL backup file every day. The file name is generated based on the date of the day. Over time, such files will generate a lot. It is necessary to delete some old backup files on a regular basis. The following command is used for this task. You can add it to the end of the script above.

# Delete the backup file 30 days ago. find $ backup_path/*-mtime + 30-exec rm {}\;

I encountered a problem when using the above script. Crontab timed execution script export does not report an error, but the exported SQL file is empty, however, you have successfully backed up the script by logging on to the console. It was later found that the Crontab script was executed because the system environment information was missing and mysqldump could not be found. The correct method was to use the full path of mysqldump. The error message is not reported because mysqldump outputs the error message to stderr. After the command is followed by a message redirection command like "2> & 1", the error message is displayed:

mysqldump -ujoe -ppassword > /tmp/somefile 2>&1

Thank you for reading this article. I hope it will help you. Thank you for your support for this site!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.