Automatically Sync Data to Google Drive/Dropbox using Rclone on Linux

Every desktop/laptop user has some important data which needs be kept reliably. Linux system also contains many configuration files of different applications. If your system gets corrupted and you install another Linux operating system, lack of a backup will require you to setup your system from beginning to come back to your workflow. This post will guide you on setting up automatic sync of data on Linux machines using rclone.

Rclone is a command line program used to sync files and directories between supported platforms like Google drive, Dropbox, Onedrive and a lot more. So, you can use this guide if your favourite cloud service is supported by rclone.

1. Install Rclone

Rclone can be installed on Debian based systems with following commands.

sudo apt update
sudo apt install rclone

Rclone is also present in the software repositories of popular operating systems including Arch, Manjaro and Fedora.

2. Configure Rclone to work with Cloud Drives

Now you have to add cloud services to Rclone. Just use the rclone config command and Rclone will interactively ask you for your cloud drive details.

  1. Type ‘n’ to add a new cloud drive and give it a name.
  2. Choose your cloud service from the list.
  3. Give configuration details for cloud drive. Just hitting enter will use default settings. If you are an advanced user, you can provide your own configuration.
  4. Confirm the configs, and you are ready to go!

3. Setup automatic Sync

First, check whether Rclone is able to access your drive. The following command will list all files in your cloud drive.

rclone ls your_remote_name:/

If the above command succeeds, your configurations are right and you can sync like following.

rclone sync ~/sync_folder/ your_remote_name:new_folder

The above command will sync folder named ‘sync_folder’ to ‘new_folder’ in your cloud drive. Now we have to add the Rclone sync command to our crontab so that the sync will happen at regular intervals. Use crontab -e command to edit your crontab. If you have to create a sync between a directory named mydata in your home folder to Google drive root folder, you can have it like the following.

*/10 * * * * /usr/bin/flock -n /tmp/some.lockfile rclone sync ~/Synced/ googledrive:Synced

We have used flock tool to prevent overlapping of cron jobs. Flock comes preinstalled on most of the Linux distributions. i.e, flock will ensure that previous sync task was complete before starting the new one. You can also adjust the sync interval by editing cron interval. If you are not familiar with cron expressions, you can create custom expressions in this link. Now, all the data you adding or deleting from the sync folder will be reflected automatically in corresponding remote folder.

I hope the tutorial was useful. Thanks for reading and use comment section below if you encounter any issues.

Leave a Reply

Your email address will not be published. Required fields are marked *