Creating a Script for Scheduled MySQL BackupsEdwin Miguel Triana Gómez, November 2007 IntroductionHere is a step-by-step tutorial for configuring a scheduled backup of MySQL databases. In order to do a scheduled MySQL backup, I'm going to create a script that includes the MySQL backup command ( Note: I assume that you have a basic knowledge of server management, UNIX systems, and database servers. Requirements
My environment uses the following:
Creating the Script1. Create a text file using a text editor, such as vi, and for the first line, add >vi backupScript.sh #!/bin/sh 2. Define the backup's file name: #!/bin/sh filename="databaseName_backup_"`eval date +m%d_%H_%M_%S`".sql" The string The string
The string 3. Add the #!/bin/sh filename="databaseName_backup_"`eval date +m%d_%H_%M_%S`".sql" mysqldump --lock-tables -u userName -pUserPassword databaseName > \ /path/to/dump/the/database /$filename The MySQL reference manual provides the a lot of good information about the The string The database user name is specified by the following parameters:
4. Save and close the file (with vi, type 5. Configure secure copy ( The a. Generate a key: ssh-keygen -t rsa Here's the system response: Generating public/private rsa key pair. Enter file in which to save the key (/home/username/.ssh/id_rsa): <Press Enter to use the default> Enter passphrase (empty for no passphrase): <Enter, in my case> Enter same passphrase again: <Enter again> Your identification has been saved in /home/username/.ssh/id_rsa. Your public key has been saved in /home/username/.ssh/id_rsa.pub. The key fingerprint is: e4:f1:b5:3d:57:2e:37:87:2f:55:91:a7:2a:f9:f4:1d username@servername b. Copy the public key (which ends in scp /home/username/.ssh/id_rsa.pub user@remoteServer:. c. Save the key in the To do this, you must log in to the remote server. You can use You must include the public key to >data="`eval cat ./id_rsa.pub`" (I'm assuming that the file is in the user's home directory.) >echo $data >> ./.ssh/authorized_keys (If the d. Log out of the remote server. 6. Test the previous configuration by copying a file from the backup server to the remote server using scp ./somefile user@remoteServer:. If the prompt doesn't ask for a password, then congratulations. You have configured the key correctly. If it asks for a password, you must perform step 5 again. 7. Include the #!/bin/sh filename="databaseName_backup_"`eval date +m%d_%H_%M_%S`".sql" mysqldump --lock-tables -u userName -pUserPassword databaseName > \ /path/to/dump/the/database/$filename scp /path/to/dump/the/database/$filename \ user@remoteServer:/path/to/backups rm /path/to/dump/the/database/$filename The last line is optional if you don't want to save the backup on the MySQL server. The 8. Schedule the backup. So far, we have completed the backup script. So, now we are going to schedule the script's execution with a. Open the crontab to add the backup script: >crontab -e b. Add a new line for the path of the backup script and the time of the execution: min hour * * * /path/to/backupScript.sh For example: 00 02 * * * /home/username/backupScript.sh In this example, the backup script will be executed daily at 02 hours (24-hour format). If you want to configure the backup in a different way, take into account the following parameter values:
9. Save the file (in vi, use Note: The chmod +x /path/to/backupScript.sh 10. Enjoy. We have completed all the steps to do a backup of a MySQL database periodically. About the AuthorMy name is Edwin Miguel Triana Gómez. I am a Computer System Engineer at the National University of Colombia, where I work in the Telemedicine Center.
Comments (latest comments first)Discuss and comment on this resource in the BigAdmin Wiki
Unless otherwise licensed, code in all technical manuals herein (including articles, FAQs, samples) is provided under this License. |
BigAdmin SubscriptionsBigAdmin Areas
BigAdmin Sun Center
BigAdmin Topics | ||||||||||