MySQL backup script

1. Create a script folder.
# sudo mkdir /script

2. Create a backup folder and a destnation folder for the MySQL files.
# sudo mkdir /backup /backup/mysql

3. Create a script file.
# sudo nano /script/mysqlbackup.sh

At the start of the file add:
#!/bin/bash

On the next row write your script.

Here are a few script examples. In the examples the MySQL user is  root and the password is Qwerty12.

Example 1. Will backup alla databases into one single file.
#!/bin/bash
/usr/bin/mysqldump –u root pQwerty12 –all-databases > /backup/mysql/all-databases.sql

Example 2. Will backup the database named mysite.
#!/bin/bash
/usr/bin/mysqldump –u root pQwerty12 –databases mysite > /backup/mysql/mysite.sql

When done exit Nano and save he script file.

4. Verify that the script file is correct.
# sudo cat /script/mysqlbackup.sh

5. Give the script execution rights.
# sudo chmod +x /script/mysqlbackup.sh

6. Instal Cron (Same as schedule tasks in Windows)
# sudo aptitude install cron

7. Edit crontab.
# crontab -e

Add the following t crontab:
01 23 * * * /script/mysqlbackup.sh

This means that mysqlbackup.sh will start every day 23.01.

8. Verify crontab.
# crontab -l

9. Verify the next day that the backup file exists in /backup/mysql/.

Run the mysqlbackup script manually:
# /script/./mysqlbackup.sh


Posted

in

, ,

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.