Website Backup to Dropbox using PHP
Dropbox auto backup can be done using PHP as explained in following steps:
- Download Dropbox SDK.
- Login to Dropbox.
- Get AccessToken for your login ID.
- Click this link -> Create new App & get its API app key, secret value.
- Store API key, secret key in JSON format.
- Invoke SDK upload functions from PHP.
The reference link provided above has complete explanation of how to do a Dropbox auto backup using PHP and other programming languages also. You may not like to go through a complete reference when you are in a hurry. That is where this article is helpful.
What we have here is a minimal PHP code that you can use to test whether the upload is happening correctly.
PHP Code to Upload file to Dropbox
Copy the below PHP code to your web server and replace the required values marked within ‘<>’ with actual values.
This code can be executed from your server, either using CRON jobs or any possible methods in your server than can execute the PHP code.
If you are using a CPANEL and want to know how to schedule a PHP code in CRON, refer this page.
<?php echo "\n Dropbox Backup Starts" ; require_once "/home/<FOLDER_PATH_WHERE_SDK_FILE_IS_UPLOADED>/dropbox-sdk/Dropbox/autoload.php"; use \Dropbox as dbx; $appInfo = dbx\AppInfo::loadFromJsonFile("<JSON_FILE_WITH_KEY_AND_SECRET.JSON>"); $accessToken = "<ACCESS_TOKEN_OBTAINED_FROM_DROPBOX_APP>"; $dropboxUserId = "<EMAIL_ID_FOR_YOUR_DROPBOX_APP>"; $dbxClient = new dbx\Client($accessToken, "PHP-Example/1.0"); $accountInfo = $dbxClient->getAccountInfo(); $f = fopen($outputFilePath , "rb"); $result = $dbxClient->uploadFile("/<TARGET_FILE_NAME_IN_DROPBOX>", dbx\WriteMode::add(), $f); fclose($f); echo($result); echo "\n Upload Ends" ; ?>
Dropbox Auto Backup using PHP
After executions if the code gives any error then verify the Key, Secret, AccessToken & Filepath are mentioned correctly in your code. These are the major things that will give you a negative result. If the code has run successfully, then login to your Dropbox and confirm the file you mentioned in the code is uploaded properly.
This code creates new version of file every time it runs. If you want to overwrite an existing file in Dropbox then modify the command ‘WriteMode::add()’ to ‘WriteMode::update()’.