• Shopping Cart Shopping Cart
    0Shopping Cart
London App Developer
  • Courses
  • Consulting
  • Tutorials
  • About
  • Click to open the search input field Click to open the search input field Search
  • Menu Menu
Linux penguin logo in front of the dropbox logo
Linux

Low cost backup solution for Linux using Dropbox and a shell script

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:

($HOME/.dropbox-dist/dropboxd &)&

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.

vi ~/Dropbox/backups/server_name/start_backup.sh

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:

crontab -e

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.

Share this entry
  • Share on Facebook
  • Share on X
  • Share on WhatsApp
  • Share on Pinterest
  • Share on LinkedIn
  • Share on Tumblr
  • Share on Vk
  • Share on Reddit
  • Share by Mail
https://i0.wp.com/londonappdeveloper.com/wp-content/uploads/2014/09/Low-cost-backup-solution-for-Linux-using-Dropbox-and-a-shell-script.png?fit=277%2C223&ssl=1 223 277 mark https://londonappdeveloper.com/wp-content/uploads/2018/12/lon_website_logo-300x110.png mark2014-09-20 21:51:172021-04-10 14:28:24Low cost backup solution for Linux using Dropbox and a shell script
4 replies
  1. Mike
    Mike says:
    July 21, 2015 at 1:53 am

    I used to use this exact technique for backing up servers – but if your server was hacked, not only would your server data be compromised, but the backups in the dropbox you made would also be compromised too, not to mention any files you happened to put in the dropbox – which I eventually started doing.
    In order to minimise potential data sabotage/loses, I used a dedicated dropbox account.
    I also copied new backup snapshots, like your command

    tar -zcvf $DROPBOX_BACKUP_DIR_URL/$DATE_NAME.tar.gz $DATE_NAME

    does, but then when you take a look in the server after 6, 12, 18months, you will have hundreds of these tarballs.
    Nowadays, I NEVER give a server any kind of access or credentials of any kind into “sensitive” locations, I only ever pull data FROM a server TO a safe/clean location – where the credentials access to the sensitive accounts reside in a safe location.
    Also, by using rsync instead of tarballing you can limit to to only one backup. I know it can potentially be handy to have a history of backups in case you need to go back to and old old version , but this could be achieved with some conditional checks in your script, but in my experience I never really needed to go back to some old backup. In fact I have actually never needed to restore a server from a backup like this , but I still would do it just in case.
    Nice article though, thanks

    • mark
      mark says:
      July 21, 2015 at 7:46 am

      Thank you for taking the time to leave such a detailed comment. Yes I completely agree and I would not recommend using this method in a live production environment however, it is interesting to see what you can do with a simple script and a free Dropbox account though 🙂

  2. Patrick Carter
    Patrick Carter says:
    July 12, 2016 at 1:43 am

    I entered my name and email address but was never given any opportunity to download you PDF step by step guide. Would you be kind enough to forward it on?
    Patrick

    • mark
      mark says:
      July 14, 2016 at 3:39 pm

      Hi Patrick, sorry about that. Which post were you trying to download a PDF for? As this one doesn’t have a PDF option.

Comments are closed.

Pages

  • About
  • Consulting
  • Home
  • Tutorials

Categories

  • AI
  • Android
  • Android Studio
  • Angular
  • Aptana Studio
  • AWS
  • Bootstrap
  • Career advice
  • Celery
  • Databases
  • Deployment
  • DevOps
  • Django
  • Django REST Framework
  • Docker
  • Eclipse
  • Getting Help
  • Git
  • Git Bash
  • Git Flow
  • GitHub Actions
  • Google App Engine
  • IDE
  • Ionic Framework
  • iOS
  • JavaScript
  • Job Search
  • Links
  • Linux
  • Mac OS X
  • macOS
  • Node.JS
  • OpenAI
  • PGAdmin
  • Postgres
  • Productivity
  • Python
  • React
  • Salt Stack
  • Stack Overflow
  • Tools
  • Travis-CI
  • Tutorials
  • Ubuntu
  • Uncategorized
  • Vagrant
  • VSCode
  • WebSockets
  • Windows 10
  • WordPress

London App Developer Ltd,

71-75 Shelton Street, Covent Garden, London, United Kingdom, WC2H 9JQ

Company registration number: 09718346

© London App Developer - London App Developer Ltd, Company Registration Number: 09718346, VAT: GB224061842
  • Link to X
  • Link to Youtube
  • Link to Facebook
  • Link to Reddit
Link to: How to use GitHub with Android Studio Link to: How to use GitHub with Android Studio How to use GitHub with Android StudioGithub and Android Studio software logos on a yellow background image Link to: How to use GitHub with Android Studio 1.x Link to: How to use GitHub with Android Studio 1.x Github and Android Studio software logos on a yellow background imageHow to use GitHub with Android Studio 1.x
Scroll to top Scroll to top Scroll to top

This site uses cookies. By continuing to browse the site, you are agreeing to our use of cookies.

OKLearn more

Cookie and Privacy Settings



How we use cookies

We may request cookies to be set on your device. We use cookies to let us know when you visit our websites, how you interact with us, to enrich your user experience, and to customize your relationship with our website.

Click on the different category headings to find out more. You can also change some of your preferences. Note that blocking some types of cookies may impact your experience on our websites and the services we are able to offer.

Essential Website Cookies

These cookies are strictly necessary to provide you with services available through our website and to use some of its features.

Because these cookies are strictly necessary to deliver the website, refusing them will have impact how our site functions. You always can block or delete cookies by changing your browser settings and force blocking all cookies on this website. But this will always prompt you to accept/refuse cookies when revisiting our site.

We fully respect if you want to refuse cookies but to avoid asking you again and again kindly allow us to store a cookie for that. You are free to opt out any time or opt in for other cookies to get a better experience. If you refuse cookies we will remove all set cookies in our domain.

We provide you with a list of stored cookies on your computer in our domain so you can check what we stored. Due to security reasons we are not able to show or modify cookies from other domains. You can check these in your browser security settings.

Google Analytics Cookies

These cookies collect information that is used either in aggregate form to help us understand how our website is being used or how effective our marketing campaigns are, or to help us customize our website and application for you in order to enhance your experience.

If you do not want that we track your visit to our site you can disable tracking in your browser here:

Other external services

We also use different external services like Google Webfonts, Google Maps, and external Video providers. Since these providers may collect personal data like your IP address we allow you to block them here. Please be aware that this might heavily reduce the functionality and appearance of our site. Changes will take effect once you reload the page.

Google Webfont Settings:

Google Map Settings:

Google reCaptcha Settings:

Vimeo and Youtube video embeds:

Other cookies

The following cookies are also needed - You can choose if you want to allow them:

Privacy Policy

You can read about our cookies and privacy settings in detail on our Privacy Policy Page.

Privacy Policy
Accept settingsHide notification only