Backup your home directory to Dropbox with rclone
Make use of this nifty utility
In this tutorial I’ll show you how to setup an system to backup your home directory to Dropbox — which is my cloud storage service of choice, but you can use pretty much every other major cloud storage service — using rclone, a command line utility to sync files and folders between your computer and cloud storage services, or between different cloud storage services.
Even if you’re not a command line user, at the end of this tutorial you will have a executable app that will back up your home directory to Dropbox, and move locally deleted files in an archive folder at each subsequent run.
This guide is for Mac users, but can be easily adapted for Windows or Linux with little tinkering.
Please note that this is not meant to replace an incremental backup system (even if it does offer some kind of fallback for deleted files); if you need that functionality, I suggest you to use a specific solution like Arq.
Disclaimer: I take no responsibility for lost files or unwanted behavior. This is working for me but might not work at all for you.
That said, let’s start.
Step 1: install and configure rclone
I know I said “install and configure”, but since it’s very straightforward I will actually skip the installation guide and suggest you to read the rclone installation guide and come back here when you’re done.
Once installed, we need to add a cloud storage service to rclone’s configuration. Depending on the service you’re going to use, this step may be a little different for you.
Here’s the example configuration for Dropbox:
- Open a terminal and type
rclone config
. - Press
n
to add a new remote. - Type the remote name, like
dropbox
(avoiding uppercases and spaces for the sake of simplicity), then pressEnter
. - Press
4
to select Dropbox from the list of supported cloud storage services. - When asked for Dropbox App Key and Dropbox App Secret, just leave blank and press
Enter
. - When asked to visit a Dropbox url, like https://www.dropbox.com/1/oauth2/authorize?client_id=1234567890&response_type=code, do it, click `Allow` to allow rclone to access your Dropbox account, then copy the resulting code.
- Paste that code in the terminal, where rclone should be asking for it.
- Press
y
to save the remote’s configuration, thenq
to quit the configuration utility.
Step 2: Setup your Dropbox account
We need to pick a destination folder for storing our backup, so open your Dropbox folder and create a new sub-folder called Backups
.
When rclone will run, it’ll store data two folders down, the first will have your computer’s name, and the second one will have your user name. In my case, the full path on Dropbox would be /Backups/macbook-pro-di-alan/tegola
.
As a bonus point, if you delete a file or folder locally and then launch a backup, that file/folder will be moved in the Archive folder — located, in my case, at /Backups/macbook-pro-di-alan/tegola_archive
— so you can find accidentally deleted items there, or clean it up if you want.
We don’t want the Backups
folder to be synced back to our computer, so we need to use Dropbox’s Selective sync feature to disable syncing just for this folder:
- Click the Dropbox icon on the menu bar, then click the Cog icon (top right) and select
Preferences
. - Click the
Account
tab, the clickChange Settings
nearSelective Sync
. - Uncheck the
Backups
folder and clickUpdate
, then click againUpdate
to confirm when the Dropbox app asks for a confirmation.
Step 3: Prepare the backup script
This is the gist of the backup process. Open a terminal and type the following commands, pressing Enter
after each one:
cd ~/Desktop
mkdir backup
cd backup
touch backup.sh
chmod +x backup.sh
touch exclude.txt
You should now have a new folder on your desktop called backup
. Inside there will be two files, including backup.sh
; we need to make this one open with a double click.
- Right click
backup.sh
and choose Get Info from the menu. - Expand the
Open with
section and selectOther
from the dropdown menu. - Navigate to Applications/Utilities, then select
All Applications
from theEnable
dropdown. - Select
Terminal
and clickAdd
. You can now close the Info window.
Well done, copy the code below and paste it inside the same file with a text editor — even TextEdit will do the job
#!/bin/bashCOMPUTER_NAME=$(hostname -s)
USER_NAME=$(whoami)
SOURCE_PATH=~/
DESTINATION_PATH=”dropbox:Backups/${COMPUTER_NAME}/${USER_NAME}”
ARCHIVE_DESTINATION_PATH=”dropbox:Backups/${COMPUTER_NAME}/${USER_NAME}_archive”rclone sync $SOURCE_PATH $DESTINATION_PATH \
— backup-dir $ARCHIVE_DESTINATION_PATH \
— exclude-from exclude.txt \
— delete-excluded
Then copy the code below and paste it inside the file called exclude.txt
. This is the list of files and folder we want to exclude from our backup, starting from the home directory. Here I’ve excluded the most common macOS resource files and, obviously, the Dropbox folder, but you can customize it as you like. My personal setup also excludes virtual machines.
._*
.DS_Store
Dropbox/**
Library/Caches/**
Keep in mind that some cloud storage services do not support some files. For example, here’s the file limitations help page for Dropbox.
Step 4: Backup!
Everything’s ready! The only thing left to do is starting the backup process. Just double click the backup.sh file to open it in a terminal and let it do its things.
Every time you want to refresh your backup, just double click the file again.
Remember: if you delete a file locally, that file will be moved inside the archive folder on Dropbox. You can disable this feature by removing the --backup-dir $ARCHIVE_DESTINATION_PATH \
line from the backup script.
Have fun, and remember to backup your data!