In this guide I will explain how to create a backup solution for your Linux server using a basic shell script and Dropbox. It’s good for text files but not so good for bigger media files such as audio and video.
It works as follows:
- We install Dropbox on our server
- Then we create a shell script that will copy all the directories you want to backup to a temp directory
- An finally we compress that temp directory into our Dropbox folder and remove the temp files
I use this method to backup all my Git repositories, MySQL databases, project source code and my server configuration files. A free Dropbox account gives you 2GB of storage, which should be enough for some compressed text files as well as a couple of databases.
Now for the how to.
First, sign register with Dropbox. Then follow the instructions at the following link to install it on your Linux box.
If you already started the Dropbox daemon in the above guide, press ctrl+c to kill this process. Now we are going to start it again but this time as a background process:
Now create a new directory in your Dropbox to store the backups. You should change ‘server_name’ to a name relevant to your server.
mkdir -p ~/Dropbox/backups/server_name
Next we will start to create our backup script. In this tutorial I use vi however you should use whichever text editor you prefer.
Modify and add the below content accordingly. I have added comments above each line to confirm what it does:
#!/bin/sh #Variable with current date and time (for the file name) DATE_NAME=$(date +'%Y-%m-%d-%H-%M-%S') #Your dropbox backup location. #Modify ‘username’ and ‘server_name’ accordingly DROPBOX_BACKUP_DIR_URL="/home/username/Dropbox/backups/server_name/" #The temp location to copy all files TEMP_DIR="/tmp" #The full url for the temp directory #including the date/time directory TEMP_BACKUP_DIR_URL="$TEMP_DIR/$DATE_NAME" #The full temp URLS for each directory you want to backup TEMP_GIT_BACKUP_DIR_URL="$TEMP_BACKUP_DIR_URL/git" TEMP_SITES_BACKUP_DIR_URL="$TEMP_BACKUP_DIR_URL/sites" TEMP_NGINX_CONFIG_DIR_URL="$TEMP_BACKUP_DIR_URL/configs/ngnx" TEMP_SUPERVISOR_CONFIG_DIR_URL="$TEMP_BACKUP_DIR_URL/configs/supervisor" TEMP_MYSQL_DATABASE_BACKUP_DIR_URL="$TEMP_BACKUP_DIR_URL/databases" #Create the directories above mkdir -p $TEMP_GIT_BACKUP_DIR_URL mkdir -p $TEMP_SITES_BACKUP_DIR_URL mkdir -p $TEMP_NGINX_CONFIG_DIR_URL mkdir -p $TEMP_SUPERVISOR_CONFIG_DIR_URL mkdir -p $TEMP_MYSQL_DATABASE_BACKUP_DIR_URL #Perform the backups, this is where you go to town #by copying all the files you want to backup cp -R "/usr/local/repo/" "$TEMP_GIT_BACKUP_DIR_URL" cp -R "/usr/local/sites/" "$TEMP_SITES_BACKUP_DIR_URL" cp -R "/etc/nginx/sites-available/" "$TEMP_NGINX_CONFIG_DIR_URL" cp /etc/nginx/*.common $TEMP_NGINX_CONFIG_DIR_URL cp -R "/etc/supervisor/conf.d/" "$TEMP_SUPERVISOR_CONFIG_DIR_URL" #This will backup all my databases #Modify ‘backup_username’ and ‘PASSWORD’ accordingly mysqldump -u backup_username -pPASSWORD --all-databases > "$TEMP_MYSQL_DATABASE_BACKUP_DIR_URL/databases.sql" #Now we change into the temp directory cd $TEMP_DIR #We tar up and compress the files to backup #and output the tar into our Dropbox tar -zcvf $DROPBOX_BACKUP_DIR_URL/$DATE_NAME.tar.gz $DATE_NAME #Now we can delete the temp backup directory rm -rf $TEMP_BACKUP_DIR_URL
Now we modify crontab. Make sure you are switched to the same user that you install Dropbox on, and edit your crontab file by typing the following command:
Next we are going to add two lines to the crontab file. The first line will ensure that Dropbox re-starts automatically if you reboot your server. The second will execute our backups daily at 7PM and output the backup log to your home directory.
@reboot ~/.dropbox-dist/dropboxd 0 19 * * * cd ~/Dropbox/backups/server_name/ && sh start_backup.sh > ~/backup.log
There is a great guide on crontab here if you are looking to learn more or change the times to schedule your backups.
Now we are done and your server backups will be automatically synchronized to your Dropbox. I do not recommend this for production environments where you may need to restore backups quickly, but you could use it for your personal or non-critical systems.