How to Automatically Backup your Database

From Free Hosting Wiki
(Difference between revisions)
Jump to: navigation, search
[quality revision][checked revision]
m (Typo Corrections)
(Alternative 1: Cron)
 
(One intermediate revision by one user not shown)
Line 8: Line 8:
 
To use cron, go to your cPanel and choose '''cron jobs'''. For large, busy sites you can set it to backup every day, but for most sites, once a week is enough. Choose '''once a week''' in the '''common settings''' dropbox. The command to use is:
 
To use cron, go to your cPanel and choose '''cron jobs'''. For large, busy sites you can set it to backup every day, but for most sites, once a week is enough. Choose '''once a week''' in the '''common settings''' dropbox. The command to use is:
 
<pre>
 
<pre>
php -q  /home/*cpanelusername*/public_html/path/to/backup.php *cpanelusername_databasename* 1 1 1
+
/usr/local/bin/php -q  /home/*cpanelusername*/public_html/path/to/backup.php *cpanelusername_databasename* 1 1 1
 
</pre>
 
</pre>
  
 
so if my cpanel username was ''viggeswe'', my database is ''wordpress'' and I uploaded pMBP to ''public_html'', the command would look like:
 
so if my cpanel username was ''viggeswe'', my database is ''wordpress'' and I uploaded pMBP to ''public_html'', the command would look like:
 
<pre>
 
<pre>
php -q  /home/viggeswe/public_html/mysql/backup.php viggeswe_wordpress 1 1 1
+
/usr/local/bin/php -q  /home/viggeswe/public_html/mysql/backup.php viggeswe_wordpress 1 1 1
 
</pre>
 
</pre>
  
 
If you want to backup several databases, you separate them by a comma, like:
 
If you want to backup several databases, you separate them by a comma, like:
 
<pre>
 
<pre>
php -q  /home/*cpanelusername*/public_html/path/to/backup.php *cpanelname_database1*,*cpanelname_database2* 1 1 1
+
/usr/local/bin/php -q  /home/*cpanelusername*/public_html/path/to/backup.php *cpanelname_database1*,*cpanelname_database2* 1 1 1
 
</pre>
 
</pre>
  

Latest revision as of 18:53, 22 September 2011

In this tutorial you will learn how to automatically backup your databases using PhpMyBackupPro. Backing up your mySQL databases is an essential step and can save hours of frustration if something would happen to your databases (hard drive corruption, database corruption, mistakes, rollbacks etc).

When you have installed PhpMyBackupPro and configured it in the way you want, there are 2 alternatives to set up automatic backups.

[edit] Alternative 1: Cron

This is a slightly advanced alternative as it might not work at first try, but if it works it works every time. Remember that x10 only allows 1 cron every 5 minutes. If you already have a cron running every 5 minute you need to go with alternative 2, even thought you wont backup every 5 minute, but when the backup runs, the other cron is also running, resulting in your account being suspended.

To use cron, go to your cPanel and choose cron jobs. For large, busy sites you can set it to backup every day, but for most sites, once a week is enough. Choose once a week in the common settings dropbox. The command to use is:

/usr/local/bin/php -q  /home/*cpanelusername*/public_html/path/to/backup.php *cpanelusername_databasename* 1 1 1

so if my cpanel username was viggeswe, my database is wordpress and I uploaded pMBP to public_html, the command would look like:

/usr/local/bin/php -q  /home/viggeswe/public_html/mysql/backup.php viggeswe_wordpress 1 1 1

If you want to backup several databases, you separate them by a comma, like:

/usr/local/bin/php -q  /home/*cpanelusername*/public_html/path/to/backup.php *cpanelname_database1*,*cpanelname_database2* 1 1 1

More info about the commands can be found in documentation/SHELL_MODE.txt of your pMBP download.

[edit] Alternative 2: Sheduled backup

In the pMBP interface, go to the Sheduled backup tab. As backup period once a week is recommended in most cases. In the Select directory where the PHP file will be placed: dropbox, choose the directory where your site's index.php file is located. Select the databases you want to be backed up, you can choose multiple by holding CTRL. Now click Show script. Before the last include("backup.php"); line, add:

@chdir("/home/cpanelusername/public_html/path/to/pMBP/install");

so if I would have installed it in my public_html/dev/ folder, I would have done:

@chdir("/home/viggeswe/public_html/dev/mysql/");

Copy the edited contents of the field, and save it as backupcron.php next to your site's index.php file. Then, at the very end of your index.php file, but before the last ?>, add this line:

require_once('backupcron.php');

You should now be done. Note that the visitor that will trigger the script when time is right will have to wait for the backup to be done before he can see your site!

[edit] Related links

Cron

Personal tools
Namespaces

Variants
Actions