How to Schedule Regular Database Backup to Dropbox

backup-db-mainPreviously, I have shown you how to schedule a database backup using cron job and email yourself the database file, but if you have a big database, you will find that the emailing method might not work as well as you want it to be. A good alternative to this is to upload your database to Dropbox. Here is a simple script that you can use to backup your database to Dropbox.

1. Download the script here.

2. Extract the zip file. Open the backup.php with a text editor and edit your database and Dropbox credential.

backup-db-script

Optionally, you can create a backup folder in your Dropbox and insert the name of the folder to the backup.php. Your database will then be backed up to this folder.

3. Save and close the file. Upload the whole backup folder to your server. Note the uploaded location relative to the server root.

4. If you are using cPanel for your web hosting, log into it and go to the cron job section.

cron-cpanel

5. Create a new cron job. You can set the time and frequency to backup the database. Under the command field, copy and paste the following command:

/usr/bin/php -q /path-to-backup-folder/backup/backup.php

Change the “path-to-backup-folder” to the location (relative to the server root) where you upload the backup folder.

6. Save the cron job.

That’s it. Now, your server will backup your database and upload it to your Dropbox account.

Note: This is a generic method for backing up any database on a Linux server. However, if you are using WordPress, there are plugins that you can use to back up your database to Dropbox. One thing to note: if you have a big database, using the plugin to backup your database will use a lot of system resources and and might slow down (or even crash) your WordPress site if your server is not up to par.

Image credit: Backup word on white keyboard by Big Stock Photo.

  • http://profile.yahoo.com/4CZCDI4UUGGECG5FSOO67JQKSA Hansy

    Thanks Damien!!,
    Really informative & helpfull.

  • othon

    Thanks.

    Any chance to have the script for sugarsync or box instead of dropbox?

    • http://dilden.wordpress.com/ Dilsayar

      BackWPup plugin has SugarSync plus Dropbox integration

  • Dsl225

    Nice script, many thanks! Would it be possible to include several databases in the same script or is it better to keep a single script+cron for each database?

    • http://maketecheasier.com/ Damien Oh

      It doesn’t really matter. Personally, I would prefer to run several database backup in one script as it simplifies the process. However, if all your database are several hundreds MB or GB in size, you might want to schedule each backup with different cron job.

      • Dsl225

        Excellent. In that case may I simply add databases in a single line here:

        $dbname = “database1″;”database2″;”database3″;
        or put each one of them in a new line – assuming they all have the same user/pass?

  • Dsl225

    … and one more question: does this work only for databases or would it also be possible to do the same for directories – I mean backup entire directories to Dropbox instead of databases (and otpionnaly split them in blocks)?   

    • http://maketecheasier.com/ Damien Oh

      The code in the script is meant to backup database. If you are experience in coding, you can easily modify the code to zip up a particular directory and send it to dropbox.

      • Dsl225

        Not enough experience to do that, I’m afraid… If by any chance you have time to do this change/edit it would be much welcome though…

Use left/right keys to browse stories.