Backup to DropBox

Backing up files from your hosting to DropBox is a much simpler task than creating a complete DropBox Application because you are the only one who needs a code to provide the access to your backup App.

The first thing you need to do is to create a DropBox App. You need to give your App a name that hasn't already been used and then generate an access token. You can basically ignore everything else in the App setup as having a name and an access token is all you actually need to get your backups working.

The next thing you need is a copy of the DropBox Core API installed on your hosting. You only need to upload the dropbox-sdk folder without all the example code etc as you will not need to mess about with authorise code or anything else like that.

Finally we need to create the code to do the actual backup and insert it into whatever script we are using to do the backup. The code we need consists of rour lines of code to create the connection to your DropBox account and then three lines of code to backup a file. Simply repeat the three lines to backup additional files.

$accessToken = "your-access-token";
require_once "../dropbox-sdk/Dropbox/autoload.php";
use \Dropbox as dbx;
$dbxClient = new dbx\Client($accessToken, "PHP-Example/1.0");

$f = fopen('file-to-backup.zip', "rb");
$result = $dbxClient->uploadFile('backed-up-file-name.zip', dbx\WriteMode::add(), $f);
fclose($f);

We can skip all of the authorize processing that a real App would need becaue DropBox has provided us with an access token to use for texting our App and since we are never going to make it available to others that's all we need. All we do then is to use that access token and the DropBox API to establish a connection to our DropBox account.

Uloading a file is then as simple as opening the file to be uploaded and calling the DropBox API uploadFile method to copy the file across to DropBox. That's the only part of the API that we need to use in order to make simple backups of our files to DropBox.

 

This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow
Donate