Home arrow MySQL arrow Page 3 - Backup and Restore a MySQL Database

Automating MySQL Backups with Cron and PHP - MySQL

This is a beginner tutorial that will show you how to perform MySQL backups and restoration.

  1. Backup and Restore a MySQL Database
  2. MySQL Back Up and Restore using phpMyAdmin GUI Method
  3. Automating MySQL Backups with Cron and PHP
By: Codex-M
Rating: starstarstarstarstar / 0
July 12, 2011

print this article



Using a Cron and a PHP Script

Now that you have a basic understanding of how to do backups and restoration of MySQL databases, it's time to automate the backup process. One of the simplest methods is to use a PHP script to backup a MySQL database, then use your hosting cron feature to periodically run the PHP script depending on your backup needs (daily, weekly, or monthly).

A sample PHP script to get started is here: http://www.php-developer.org/php-script-to-backup-mysql-database-using-hosting-cron/. The key feature is that it will allow you to backup up to 4 MySQL database in your server using cron. Then the database backups are saved in your server specified by the $full_serverpath_to_backup variable.

For details, you can download the script, extract and test. Read the details inside cronmysqlbackup.php, those commented lines provide a good introduction for beginners. The actual steps are illustrated in Step 11 inside cronmysqlbackup.php and it is:

1.) Define your website MySQL login credentials such as username, password, hostname, database name, and so forth.

2.) Define the path to your cronmysqlbackup folder as defined by $full_serverpath_to_backup. You can optionally add other databases aside from your main website database.

3.) Decide how many days will you delete the old MySQL files. The unit is in days.

4.) Finally upload the cronmysqlbackup folder with cronmysqlbackup.php and .htaccess to the path above your web root.

5.) Enable hosting cron to automatically run this script at intervals you choose e.g. weekly, monthly

The path that you have uploaded should be the same path as what you have declared in $full_serverpath_to_backup

The concept of implementation for that script relies on mysqldump command, as you can see in these lines:

$command1 = "mysqldump -u$username1 -p$password1 -h$hostname1 $database1 > $MySQLbackupfile1";
system($command1, $result1);
echo $result1;

>>> More MySQL Articles          >>> More By Codex-M

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Oracle Unveils MySQL 5.6
- MySQL Vulnerabilities Threaten Databases
- MySQL Cloud Options Expand with Google Cloud...
- MySQL 5.6 Prepped to Handle Demanding Web Use
- ScaleBase Service Virtualizes MySQL Databases
- Oracle Unveils MySQL Conversion Tools
- Akiban Opens Database Software for MySQL Use...
- Oracle Fixes MySQL Bug
- MySQL Databases Vulnerable to Password Hack
- MySQL: Overview of the ALTER TABLE Statement
- MySQL: How to Use the GRANT Statement
- MySQL: Creating, Listing, and Removing Datab...
- MySQL: Create, Show, and Describe Database T...
- MySQL Data and Table Types
- McAfee Releases Audit Plugin for MySQL Users

Developer Shed Affiliates


Dev Shed Tutorial Topics: