1. Installation

You will need three tools:

  • rsync: for local copying and data preparation.
  • rclone: to work with cloud storage services (supports Google Drive, Dropbox, and others).
  • tar: for creating archives.
sudo apt update
sudo apt install rsync tar
curl -O https://downloads.rclone.org/rclone-current-linux-amd64.deb
sudo dpkg -i rclone-current-linux-amd64.deb

2. Setting up rclone

Let’s assume we want to back up the folder /mnt/data to Google Drive.
We’ll name the configuration googledrive.

rclone config

Follow the prompts and provide the requested information. If an input can be left blank, do so. If you’re unsure what to answer, refer to the example below.

Example under the cut

Set the name (Enter name for new remote.) — googledrive.

Choose the type of cloud storage (Type of storage to configure.). For example, Google Drive is option 19 (there’s also option 18 for Google Storage, but that’s different). Don’t worry if some lines are in red — it’s just part of the design. 😊

After that, agree to everything until you reach the authentication step (Use web browser to automatically authenticate rclone with remote?). At this point, you’ll need to enter a token. However, since Google authentication uses a browser, and there’s no browser on your server, here’s what to do:

  • Answer no. You’ll then be prompted to copy the command rclone authorize "drive" to a local machine with a browser and run it there.
  • Copy and paste the command rclone authorize "drive" into the terminal on your local machine and execute it.
  • Authenticate via the browser that opens.
  • Return to the terminal, and find a line like this:mathematicaCopy codePaste the following into your remote machine ---> JSON token <---End paste Copy everything between ---> and <---.
  • Go back to the terminal on the server (where you’re configuring rclone) and paste the copied token at the prompt (config_token>).

Next, you’ll see another prompt:
Configure this as a Shared Drive (Team Drive)? — Answer no.

Finally, you’ll be asked:
Keep this “googledrive” remote? — Answer y.

That’s it! Configuration is complete.


3. Testing the connection

Run the following command on the server (don’t forget the colon : at the end). lsd stands for “List Directories.”

rclone lsd googledrive:

You should see a list of directories on your Google Drive.


4. Bash Script

On the server, create a script in the folder where you store your maintenance scripts. Use the following command, depending on your editor preference:
vim backup.sh or nano backup.sh.
(I prefer vim for larger scripts or programs and nano for small scripts or configuration files.)

Script contents:

#!/bin/bash
# Parameters
BACKUP_SRC="/mnt/data"             # Folder to back up
BACKUP_DEST="/tmp/backup"          # Temporary directory for archives
REMOTE="googledrive:backups"       # 'backups' is the folder name in the cloud
DATE=$(date +"%Y-%m-%d_%H-%M-%S")  # Date string for file name
ARCHIVE_NAME="backup_$DATE.tar.gz" # Backup file name (with date)

# Create the archive
mkdir -p $BACKUP_DEST # Create temporary folder
tar -czf $BACKUP_DEST/$ARCHIVE_NAME $BACKUP_SRC # Archive the folder

# Upload to the cloud
rclone copy $BACKUP_DEST/$ARCHIVE_NAME $REMOTE # Upload archive

# Clean up temporary files
rm -rf $BACKUP_DEST # Remove temporary folder

# Delete cloud archives older than 30 days
rclone delete --min-age 30d $REMOTE 

echo "Backup $ARCHIVE_NAME uploaded to $REMOTE"

5. Schedule Configuration (cron)

Add a line to execute the script (e.g., every day at 2:00 AM):

Open the cron editor:

crontab -e
0 2 * * * /path/to/backup.sh >> /var/log/backup.log 2>&1

The format is as follows: minutes hours day month day_of_week command

It might seem odd that day_of_week comes after month, but that’s the format…

Should you set the time in UTC or local time? Neither. The time should be set in the server’s timezone, which you can check using the following command:

timedatectl

6. Permissions

Grant execution rights to the script:

chmod +x /path/to/backup.sh

And to the log file (ensure you create the file beforehand with root permissions):

sudo touch /var/log/backup.log  
sudo chown your_user:your_user /var/log/backup.log

7. Testing

Run the script manually to ensure everything works as expected:

bash /path/to/backup.sh

Verify that the archive appears in your cloud storage.