Noupe Editorial Team March 15th, 2009

10 Ways to Automatically & Manually Backup MySQL Database

MySQL is one of the most popular open source database management system for the development of interactive Websites.

If your site stores its sensitive data in a MySQL database, you will most definitely want to backup that information so that it can be restored in case of any disaster (we all have been there).

There are several ways to backup MySQL data. In this article we'll look at how to backup your databases using different methods, we will also learn how to achieve an automatic backup solution to make the process easier. Starting with the mysqldump utility that comes with MySQL, we will review several examples using mysqldump, including the backup of your database to a file, another server, and even a compressed gzip file and send it to your email.

1. Automatically backup mysql database to Amazon S3

MySQL Backup Solution

Many of users use Amazon S3 to backup their mysql databases. Here is an automated script which does this task of taking the backup of a mysql database and then moving it to the Amazon S3.

2. How to Backup MySQL Database automatically (for Linux users)

15 2 * * * root mysqldump -u root -pPASSWORD --all-databases | gzip > /mnt/disk2/database_`data ' %m-%d-%Y'`.sql.gz 

This post will show you how to backup MySQL Database automatically if you are a linux user. You can use cron to backup your MySQL database automatically."cron" is a time-based scheduling utility in Unix/Linux operating system.

3. Backup your MySQL databases automatically with AutoMySQLBackup

AutoMySQLBackup has some great features to: backup a single database, multiple databases, or all the databases on the server; each database is saved in a separate file that can be compressed (with gzip or bzip2); it will rotate the backups and not keep them filling your hard drive (as normal in the daily backup you will have only the last 7 days of backups, the weekly if enabled will have one for each week, etc.).

4. Backing Up With MySQLDump

mysqldump ---user [user name] ---password=[password]  
[database name] > [dump file]

In this article we'll look at how to backup our databases using the mysqldump utility that comes with MySQL. Several examples will be reviewed using mysqldump, including the backup of your database to a file, another server, and even a compressed gzip file.

5. Backup Your Database into an XML File Using PHP

mysqldump ---user [user name] ---password=[password]  
[database name] > [dump file]

Here’s a PHP snippet that outputs your database as XML. XML isn’t the easiest format to restore a table but it can be easier to read.

6. How to - Using PHP To Backup MySQL Database

Execute a database backup query from PHP file. Below is an example of using SELECT INTO OUTFILE query for creating table backup:

<?php
include 'config.php';
include 'opendb.php';

$tableName  = 'mypet';
$backupFile = 'backup/mypet.sql';
$query      = "SELECT * INTO OUTFILE '$backupFile' FROM $tableName";
$result = mysql_query($query);

include 'closedb.php';
?> 

To restore the backup you just need to run LOAD DATA INFILE query like this :

<?php
include 'config.php';
include 'opendb.php';

$tableName  = 'mypet';
$backupFile = 'mypet.sql';
$query      = "LOAD DATA INFILE 'backupFile' INTO TABLE $tableName";
$result = mysql_query($query);

include 'closedb.php';
?>

7. Backup MySQL Database Via SSH

A simple solution to backup your large MySQL databases through SSH. You will need to enable shell access inside your Plesk control panel and use a utility such as PuTTY to log into your server via SSH.

8. How to e-mail yourself an automatic backup of your MySQL database table with PHP

This script will send an e-mail to you with an .sql file attached, thus enabling you to back up specific tables easily. You could even set up an e-mail account just to receive these backups…

9. Ubuntu Linux Backup MySQL server Shell Script

If you have a dedicated VPS server running Ubuntu Linux. Here is how to backup all your mysql server databases to your ftp server

10. How to backup MySQL databases, web server files to a FTP server automatically

This is a simple backup solution for people who run their own web server and MySQL server on a dedicated box or VPS. The main advantage of using FTP or NAS backup is a protection from data loss.First you will need to backup each database with mysqldump command, Automating tasks of backup with tar, Setup a cron job and generate FTP backup script.

$ mysqldump -u root -h localhost -pmypassword faqs | gzip -9 > faqs-db.sql.gz

11. MySQL Export: How to backup your MySQL database?

MySQL Backup Solution

You can easily create a dump file(export/backup) of a database used by your account. In order to do so you should access the phpMyAdmin tool available in your cPanel.

Worth Reading

- 10 things you need to know about backup solutions for MySQL

Are you using someone else’s backup solution for your MySQL data? Do you care a lot about your data? Are you sure you’re getting a reliable, recoverable backup that’ll work for your business and your application, and won’t impact your critical processes while it runs? Here are ten questions you need to be able to answer.

Noupe Editorial Team

The jungle is alive: Be it a collaboration between two or more authors or an article by an author not contributing regularly. In these cases you find the Noupe Editorial Team as the ones who made it. Guest authors get their own little bio boxes below the article, so watch out for these.

115 comments

  1. Your automatic backup script for Linux will not work, use this instead in the crontab:

    15 2 * * * /usr/bin/mysqldump –opt –all-databases -u root -pPASSWORD > /foo/bar/db-`date +%Y-%m-%d`.sql

    1. xmlsamurai As posted here it works fine for me.. Your system must be different or you are doing something wrong. Next time you want to post try using terms like.. It didn’t work for me.. This way you don’t openly say that the instructions is wrong or not working that someone so kindly posted on the web .
      I have tested it and it does work.

      1. Your example crontab line has the “data ‘ %m-%d-%Y'” instead of “date ‘ %m-%d-%Y'”. That’s why it doesn’t work.

  2. How about using logrotate. Its the best way to keep backup only for X days. Also use rsync to transfer the file to a remote server just for another backup.

    /var/backups/db.sql.gz {
    daily
    rotate 14
    nocompress
    create 640 root adm
    postrotate
    mysqldump db -u user -psecret >/var/backups/db.sql
    rsync -az /var/backups/db.sql –password-file /root/rsync.password remote@server::backup
    gzip -9f /var/backups/db.sql
    endscript
    }

    1. hey Vivek –

      I like your method the best. I found /usr/sbin/logrotate is installed by default in OpenSolaris (SXCE b123) so I have found me a “export & retain the last 14” method!

      thanks!
      C-YA

    1. dear sir,
      i am using 2003server as domain and oracle database installed in domain if i do bdc how i can backup every secound from pdc to bdc

  3. Dump a remote db locally in one easy line:

    ssh user@server “/usr/bin/mysqldump -u user -p password database_name” | dd of=/where/you/want/the/dump.sql

Leave a Reply

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