DEV Community

Daniel Werner
Daniel Werner

Posted on • Originally published at danielwerner.dev on

Automated linux server backup with Dropbox

Introduction

I was looking around for some low cost backup solution for my server when I got an idea. Let’s use dropbox for that. The free version of dropbox has 2 GB of space and it has an official linux client. The offered space is not too much, but for smaller sites it is good enough.

I’ve created a collection of scripts for the backup, it creates the backup of the given input directory, from the Nginx configurations and all databases. The backup is stored in the synchronized Dropbox directory the last 30 files are kept, the older ones are automatically deleted. Let’s see how to use it.

Set up the scripts

Create a dropbox user

sudo adduser --disabled-password dropbox
Enter fullscreen mode Exit fullscreen mode

Install Dropbox

sudo su dropbox cd ~ && wget -O - "https://www.dropbox.com/download?plat=lnx.x86\_64" | tar xzf -
Enter fullscreen mode Exit fullscreen mode

Start the dropbox daemon and open the link displayed by the daemon to link to your dropbox account with your server. For more information about installing dropbox for linux please visit the Dropbox’s official instructions here.

~/.dropbox-dist/dropboxd
Enter fullscreen mode Exit fullscreen mode

Install the backmeup scripts into the home directory of the Dropbox user:

cd /home/dropbox
git clone https://github.com/daniel-werner/backmeup.git
Enter fullscreen mode Exit fullscreen mode

Add dropbox daemon to crontab to start it automatically after reboot

crontab -e
Enter fullscreen mode Exit fullscreen mode

Insert the following line

@reboot ~/.dropbox-dist/dropboxd
Enter fullscreen mode Exit fullscreen mode

Add the backup script to run every day at 1:00 AM, replace the input directory and the database login credentials

0 1 * * * sh /home/dropbox/backmeup/bin/backup.sh -i /var/www/vhosts -o /home/dropbox/Dropbox/backup -u backup -p secret
Enter fullscreen mode Exit fullscreen mode

I’d recommend creating a new mysql user for the backup with the following global privileges: select, references, create view, show view, execute, lock tables.

That’s it, you just need to either manually start the dropbox daemon in background or restart the server to have the cron job start it.

How it works

If you are still reading and interested in details let’s take a look at the scripts:

backmeup/bin/ backup.sh

This script is responsible for creating the backup of the files and calling the database backup script. It accepts the following arguments:

-u the mysql username to use for the database backup

-p the password for mysql

-i the input directory for the backup

-o output directory where the backup will be placed

The backup file contains the following directory structure:

configs – contains the backup of nginx sites configuration (/etc/nginx/sites-available)

databases – all the mysql databases are dumped to separate gzipped files and placed in this directory

sites – the backup of the input directory

backmeup/bin/ dump-all-databases.sh

This script creates backup of all databases into separate files. The output can be plain text dump or gzipped. It accepts the following arguments:

-u mysql username

-p mysql password

-o output directory

-z use gzip output: if this argument is present the output file will be gzipped

The source code is available on GitHub, please find it here. If you have any comment or suggestion please share it in comments.

The post Automated linux server backup with Dropbox appeared first on Daniel Werner.

Top comments (0)