Linux Server Backup Using Rclone

Linux Server Backup to S3 Object Storage using Rclone

In this post I want to show you how to create both manual and scheduled backup of any Linux server to S3 Object Storage using rclone.

This guide is can be used to back up any server running Linux provided by any company, no matter whether it is a Dedicated Server, VPS or VDSThe following commands were tested with Ubuntu 18.04(64 Bit), Ubuntu 20.04(64 Bit), Debian 9 and Debian 10. Other distributions like CentOS require a few adjustments here and there.

You need to have a root access to perform the commands listed in this article. Log in as root or elevate your rights by typing: 

 sudo -i 

Initial Linux Server Backup Configuration

1. Getting S3 Credentials to Store the Backup

I will be using Contabo Object Storage as a backup storage in this tutorial. I assume that you already have an active Object Storage subscription while following this guide.

First, you need credentials such as: access_key, secret_key and URL. For Contabo Object Storage, these can be find in the Object Storage section of your CCP.

S3 Object Storage credentials in CCP

2. How to Create a Backup File on Linux Server

First, we have to install the latest version of rclone 

curl https://rclone.org/install.sh | sudo bash 

With the following command we will create a backup of the current user and stores the backup file with date in /opt/backup/.

mkdir /opt/backup/  

cd /opt/backup && tar -cvzf backup-$(date +%d.%m.%Y).tar.gz --directory=/ --exclude=lost+found --exclude=dev/* --exclude=proc/* --exclude=run/* --exclude=sys/* --exclude=tmp/* --exclude=mnt/* --exclude=media/* --exclude=opt/backup/* . 

Explanation of the command:  

  • tar – tar creates us an archive of our data. In this example with the following parameters: 
  • c – (create) creates a new archive 
  • v – (verbose) detailed output 
  •  z – (gzip) the archive will be compressed with gzip 
  •  f – (file) name and location of the archive. Must be the last option passed, since everything after f is treated as a file path.
  • /path/filename.tar.gz – path to which the archive will be written.  
  • As you can see, i have excluded some of directory’s we don’t want to backup, because of storage space reasons or unwanted big stored files in tmp. 

3. Configuring Rclone on a Linux Server

We assume that the files and folder you want to backup are saved in folder /opt/backup/. 

Setup Rclone from our product documentation here. Now we are able to perform the Linux server backup with Rclone.

4. Transferring Your Backup File from Linux Server to S3

I named my object store eu2 and my backup folder backup. Now we need to create a bucket where we store all the files.

rclone mkdir eu2:backup 

After you have completed all the steps in the “Initial Configuration” section above, run the following command to save the files to your Object Storage.

All data from /opt/backup will transferred to your destination bucket. 

rclone copy -P /opt/backup eu2:backup/server1 

Copy all data  

rclone move –P /opt/backup eu2:bucketname/server1 

Move will delete files after transfer 

rclone sync –P /opt/backup eu2:bucketname/server1 

Sync updates files on remote storage

5. Scheduling Rclone Backups

After finishing the “Initial Configuration” section above, you can start schedule backups to be created automatically.

First, we have to create the script file :

sudo nano /var/rclone.sh or a name of your choice

And add this to the script :

#!/bin/bash

cd /opt/backup && tar -cvzf backup-$(date +%d.%m.%Y).tar.gz --directory=/ --exclude=lost+found --exclude=dev/* --exclude=proc/* --exclude=run/* --exclude=sys/* --exclude=tmp/* --exclude=mnt/* --exclude=media/* --exclude=opt/backup/* .

/usr/bin/rclone sync -P --update --verbose --transfers 30 --log-file=/var/log/upload.log "/opt/backup" "eu2:backup/"

Now we make the file executable

sudo chmod +x /var/rclone.sh

Now we have to add our script to cron

sudo crontab –e

Copy

0 2 * * * /var/rclone.sh

In this example, the file /var/rclone.sh is called every day at 02:00. You can learn more about different backup strategies in this article.

You can change the time and date as you wish, if you are new to cron, you can find here a crontab generator which will helps you out.

This is the easy way to keep your data complete and safe, please backup your Linux servers with a tool like rclone.

6. Restore Data with Rclone

rclone copy -P eu2:backup/server1 /opt/backup

To download your backup, you need to switch source and destination and specify which backup you actually want to download.

Additional Information’s

More information’s about flags and configuration can be found in the official documentation.

In some cases, you have to add –s3-chunk-size 200M to your script if your upload speed is very slow.