How to Backup your Ubuntu VPS to DropBox

      No Comments on How to Backup your Ubuntu VPS to DropBox

Automatically backing up files from a VPS to your DropBox account is simpler than you might think.

When you configure your VPS to automatically send backups to your DropBox storage account, you can be assured that critical files on your VPS are backed up in a cloud data center that features multiple levels of redundancy.

This type of backup scheme could be necessary for critical apps running on a VPS. If you found yourself in a disaster recovery scenario, it would be much easier to restore your files from a cloud backup than to wait on your data center to find the files on a tape backup.

In this article, we will assume you are using the last version of Ubuntu 16.04. So let’s get started! We will also assume that you already have a DropBox account. If you do not, go ahead and register for a free account now by visiting:

Once you are logged in, copy and paste the following address into your browser:

Click the “Create App” button and then select the following from the first question:

  • DropBox API

In the section titled “2. Choose the type of access you need” select:

  • App folderAccess to a single folder created specifically for your app.

Now select a name for your app, in our example we will use “easyBackup”. Accept the Terms and Conditions and then click “Create App”.

On the next screen you will need to press the “Generate” button under the section titled “Generate access token”, take note of the code and keep it since we will be using it in the next steps.

Now that our DropBox account is ready, let’s configure our VPS. First, we must install cURL. cURL is a command line tool for transferring data using various protocols – FTP, FTPS, HTTP, HTTPS and many more. To install it on our VPS, type the following command in an SSH session:

> sudo apt-get install curl

You will now need to download and install the DropBox Uploader script. As name suggests, the Dropbox Uploader is a simple bash script which can be used to upload, download, delete, and manipulate files with DropBox. To download and install it, simply execute the following commands:

> mkdir dropbox

> cd dropbox

> curl “https://raw.githubusercontent.com/andreafabrizi/Dropbox-Uploader/master/dropbox_uploader.sh” -o dropbox_uploader.sh

> chmod 755 dropbox_uploader.sh

> ./dropbox_uploader.sh

When the script is executed for the first time, it will ask you to customize your install. It will prompt you for the Access Token that we generated in previous steps. Copy and paste that into the terminal session and answer yes to the next question.

To test that everything is OK, let’s try to upload the dropbox_uploader.sh script itself. Simply run the following command:

> ./dropbox_uploader.sh -k upload dropbox_uploader.sh .

When you go to your DropBox account, you should now see the application folder and inside of it a folder with the name of your app, in this case “easyBackup”.

We are now ready to set up our daily backup script. Let’s create the following file in our home directory on the VPS:

> vim backup.sh

We are using vim, but you may use the editor that you prefer. Paste the following lines into the new file:

—–

#!/bin/bash

TMP_DIR=”/tmp/”

DATE=$(date +”%d-%m-%Y_%H%M”)

BKP_FILE=”$TMP_DIR/MyBkp_$DATE.tar”

BKP_DIRS=”/var/www /etc”

DROPBOX_UPLOADER=/path/to/dropbox_uploader.sh

tar cf “$BKP_FILE” $BKP_DIRS

gzip “$BKP_FILE”

$DROPBOX_UPLOADER -f /root/.dropbox_uploader upload “$BKP_FILE.gz” /

rm -fr “$BKP_FILE.gz”

—–

In this example we are assuming you will backup the following directories:

/var/www

/etc

In case you will want to change or add different ones, simply modify the following line of the script:

BKP_DIRS=”/dir1 /dir2 /dir3 … /dirX”

Now let’s add execution permissions to the backup script using the following command:

> chmod 755 backup.sh

The last step is to add the following line to the crontab:

00 00 * * * /path/to/backup.sh 2>&1 >> /var/log/backup.log

This will execute the script every night at 12AM and generate a log with the output of the execution.

Keep in mind that your DropBox account is not limitless, so remember to clean up some of the older files to avoid going past your quota.

If you’d rather not deal with the process of cleaning up your old backups, use this version of the script that only generates a single backup file. The caveat is that the previous day’s backup is overwritten when the newest backup job is executed. Use the same process described above with the following script:

—–

#!/bin/bash

TMP_DIR=”/tmp/”

BKP_FILE=”$TMP_DIR/MyBkp.tar”

BKP_DIRS=”/var/www /etc”

DROPBOX_UPLOADER=/path/to/dropbox_uploader.sh

tar cf “$BKP_FILE” $BKP_DIRS

gzip “$BKP_FILE”

$DROPBOX_UPLOADER -f /root/.dropbox_uploader upload “$BKP_FILE.gz” /

rm -fr “$BKP_FILE.gz”

—–

Leave a Reply

Your email address will not be published. Required fields are marked *