How to Schedule Regular Database Backup to Dropbox

Previously, 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:

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.

16 comments

  1. 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?

    • 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.

      • 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?

  2. … 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)?   

    • 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.

      • 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…

  3. Thanks Damien! You saved the day again!

    Just one thing: the DropboxUploader class has been updated and I got the updated code from https://github.com/jakajancar/DropboxUploader/ 

    Thank you so much again! :)

  4. Hi, Thanks a lot for this awesome script but when i try to launch it with the url in my web browser i got this error ‘Fatal error: Uncaught exception ‘Exception’ with message ‘Cannot extract token! (form action=/login)’ in /homepages/30/d370839618/htdocs/DropboxUploader.php:140 Stack trace: #0 /homepages/30/d370839618/htdocs/DropboxUploader.php(86): DropboxUploader->extractToken(‘HTTP/1.1 200 OK…’, ‘/login’) #1 /homepages/30/d370839618/htdocs/DropboxUploader.php(74): DropboxUploader->login() #2 /homepages/30/d370839618/htdocs/backup.php(20): DropboxUploader->upload(‘/homepages/30/d…’, ‘sauvegarde_veh’) #3 {main} thrown in /homepages/30/d370839618/htdocs/DropboxUploader.php on line 140′

    but the sgl.gz backup is generated on my server and not transfered on my dropbox.

    (sorry for my english, hello from france ^^)

  5. Can you update the code and paste it in pastebin.com to show how to backup more than one database in it’s own .gz file?

    Thanks!

  6. when i try this method with 500mb file it’s not uploading to drop box. May be some one know trick.

    upload($backupdir.’/’.$backupfile2,$dropbox_dir);
    unlink($backupfile2);

    ?>

Comments are closed.

Sponsored Stories