So, it’s on you, the website owner, to make sure your site is backed up. The server that runs your site is just like your computer’s hard drive—if it crashes without a backup, your files will be unrecoverable and lost forever.
Backups need to be stored safely off-site, in a different location than the server running your site. An important component of a backup strategy is off-site storage of your backup files. If your backups are stored on the same server as your site, you’ll lose your backups along with your site if your server fails or if your site gets compromised.
There are lots of backup solutions and options available on the web, but most of them are too hard to configure and meant only for business and professional usage. If you want a free and simple solution that will work on any Linux distribution, then this article will definitely guide you. In this article, we will setup a script which runs on a daily basis and backup your files to Dropbox cloud storage.
Backing up files automatically from a VPS to your DropBox account is simpler than you might think. As you may know, DropBox is a cloud storage service which offers 2 GB of free storage. If you refer your friends to Dropbox, you can earn up to 16 GB of additional space. This is more than enough to backup a fully working WordPres website.
When you setup 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. 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 latest version of Ubuntu 16.04. To setup simple backup solution that uses DropBox you need of four things: DropBox account, Dropbox access token, cURL and script called dropbox_uploader.sh.
So let’s get started!
Table of Contents
Get Dropbox Account
We assume that you already have a DropBox account. If you do not, go ahead and register for a free account now by visiting:
Also, make sure that you refer some friends to sign in order to get some additional free space.
Get Dropbox Access Token
Once you are logged in into Dropbox, use the following address to get your access token:
Click on the “Create App” button and then select the following options:
- Choose an API: Dropbox API
- Choose the type of access you need: App folder – Access to a single folder created specifically for your app.
- Name your app: in our example, we used “tronarBackup”
Then, 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.
cURL is a command line tool for transferring data using various protocols – FTP, FTPS, HTTP, HTTPS and many more. To install it on VPS, type the following command in an SSH session:
sudo apt-get install curl
Setup DropBox Uploader script
Dropbox Uploader is a BASH script which can be used to upload, download, delete, list files (and more!) from 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 “tronarBackup”.
We are now ready to set up our daily backup script. Let’s create the following file in our home directory on the VPS:
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:
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 may have certain file storage limits, so remember to clean up some of the older files regularly.
If you do not want to deal with the process of cleaning up your old backups, use following version of the script. This script only generates a single backup file. This script overwrites the previous day’s backup file 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"