Backing Up Your Ubuntu Server to Google Drive Using rclone on a Headless Machine

Backing up your server data is essential for data integrity and disaster recovery. If you're running an Ubuntu server on a cloud provider like Linode or any other platform, you might want to back up your home directory or important files to a reliable cloud storage service like Google Drive. This guide will walk you through the process of using rclone to back up your Ubuntu server to Google Drive on a headless machine (a server without a graphical user interface).


Table of Contents

  1. Introduction to rclone
  2. Prerequisites
  3. Installing rclone on a Headless Ubuntu Server
  4. Configuring rclone with Google Drive on a Headless Machine
  1. Testing the Configuration
  2. Backing Up Your Home Directory
  1. Automating Backups with Cron
  2. Troubleshooting Common Issues
  3. Conclusion
  4. Additional Resources

Introduction to rclone

rclone is a powerful command-line program that allows you to sync files and directories to and from numerous cloud storage providers, including Google Drive. It's especially useful for servers and headless machines where a graphical interface isn't available.


Prerequisites

  • An Ubuntu server (the steps apply to other Linux distributions with minor adjustments).
  • A Google account with sufficient storage space for your backup.
  • Access to another device with a web browser (for authorization steps).
  • Basic familiarity with the Linux command line.

Installing rclone on a Headless Ubuntu Server

Although rclone is available in the Ubuntu repositories, installing the latest version ensures you have all the latest features and bug fixes.

Installation Steps:

  1. Update Your Package List:
   sudo apt update
  1. Install Necessary Dependencies:
   sudo apt install curl -y
  1. Download and Run the rclone Installation Script:
   curl https://rclone.org/install.sh | sudo bash
  1. Verify the Installation:
   rclone --version

You should see output similar to:

   rclone v1.XX.X
   - os/arch: linux/amd64
   - go version: go1.XX

Configuring rclone with Google Drive on a Headless Machine

Since we're working on a headless server without a web browser, we'll perform a manual authorization process.

Step 1: Start the rclone Configuration

rclone config

Step 2: Create a New Remote

  • Prompt:
  No remotes found - make a new one
  n) New remote
  s) Set configuration password
  q) Quit config
  n/s/q>
  • Action: Type n and press Enter to create a new remote.

Step 3: Choose a Name for Your Remote

  • Prompt: name>
  • Action: Enter a name for your remote (e.g., gdrive) and press Enter.

Step 4: Choose Google Drive as the Storage Type

  • Prompt:
  Type of storage to configure.
  Enter a string value. Press Enter for the default ("").
  storage>
  • Action: Type drive and press Enter.

Step 5: Configure Google Drive Settings

Client ID and Client Secret

  • Prompt for client_id:
  Option client_id.
  OAuth Client Id.
  Leave blank normally.
  client_id>
  • Action: Press Enter to leave blank.
  • Prompt for client_secret:
  Option client_secret.
  OAuth Client Secret.
  Leave blank normally.
  client_secret>
  • Action: Press Enter to leave blank.

Set the Scope

  • Prompt:
  Option scope.
  Scope that rclone should use when requesting access from drive.
  Enter a string value. Press Enter for the default ("drive").
  scope>
  • Action: Press Enter to accept the default drive.

Service Account File

  • Prompt:
  Option service_account_file.
  Service Account Credentials JSON file path.
  Leave blank normally.
  service_account_file>
  • Action: Press Enter to leave blank.

Edit Advanced Configurations

  • Prompt:
  Edit advanced config? (y/n)
  y/n>
  • Action: Type n and press Enter.

Step 6: Perform Manual Authorization

Since we're on a headless machine:

  • Prompt:
  Use auto config?
   * Say Y if not sure
   * Say N if you are working on a remote or headless machine
  y/n>
  • Action: Type n and press Enter.

Get the Authorization URL

  • rclone will display a message like:
  Please go to the following link: https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=...
  Log in and authorize rclone to access your Google account.
  Enter verification code>

Authorize rclone on Another Device

  1. Copy the URL provided by rclone.
  2. Open a web browser on another device (your local computer, smartphone, etc.).
  3. Paste the URL into the browser's address bar and press Enter.
  4. Log into Your Google Account if prompted.
  5. Review Permissions and click Allow to grant rclone access to your Google Drive.
  6. Copy the Verification Code displayed after authorization.

Enter the Verification Code

  • Back on your server, at the Enter verification code> prompt, paste the verification code and press Enter.

Step 7: Configure Team Drive (Optional)

  • Prompt:
  Configure this as a Shared Drive (Team Drive)?
  y) Yes
  n) No
  y/n>
  • Action: Type n and press Enter unless you're using a Shared Drive.

Step 8: Confirm and Save the Configuration

  • Review the Configuration:
  [gdrive]
  type = drive
  scope = drive
  token = {"access_token":"ya29...","token_type":"Bearer","refresh_token":"1//...","expiry":"..."}
  • Prompt:
  y) Yes this is OK
  e) Edit this remote
  d) Delete this remote
  y/e/d>
  • Action: Type y and press Enter.

Step 9: Exit rclone Configuration

  • Prompt:
  e) Edit existing remote
  n) New remote
  d) Delete remote
  r) Rename remote
  c) Copy remote
  s) Set configuration password
  q) Quit config
  e/n/d/r/c/s/q>
  • Action: Type q and press Enter.

Testing the Configuration

To ensure that rclone is properly configured:

rclone ls gdrive:
  • Expected Result: A list of files and folders in your Google Drive.
  • If you receive an error:
  • Double-check your configuration with: rclone config
  • Ensure you entered the verification code correctly.

Backing Up Your Home Directory

Now that rclone is configured, you can back up your home directory to Google Drive.

Understanding the Backup Command

rclone sync /home/your_username gdrive:backup_folder
  • rclone sync: Syncs files from source to destination, making the destination match the source.
  • /home/your_username: The source directory you want to back up.
  • gdrive:backup_folder: The destination on Google Drive.
  • gdrive: The name of the remote we configured.
  • backup_folder: The folder in Google Drive where the backup will be stored. rclone will create it if it doesn't exist.

Example Backup Command

rclone sync /home/ubuntu gdrive:server_backup -P
  • Replace /home/ubuntu with your actual home directory.
  • -P: Shows the progress of the sync operation.

Excluding Certain Files or Directories

You might want to exclude directories like Downloads or cache files:

rclone sync /home/ubuntu gdrive:server_backup -P --exclude "Downloads/**" --exclude ".cache/**"

Automating Backups with Cron

To ensure regular backups, you can schedule the backup command to run automatically using cron.

Setting Up a Cron Job

  1. Edit the Crontab File:
   crontab -e
  1. Add a Cron Job Entry: To run the backup daily at 2 AM:
   0 2 * * * /usr/bin/rclone sync /home/ubuntu gdrive:server_backup --exclude "Downloads/**" --exclude ".cache/**" >> /var/log/rclone_backup.log 2>&1
  1. Save and Exit:
  • If using nano, press Ctrl + X, then Y, and Enter.

Verify the Cron Job

crontab -l

Troubleshooting Common Issues

Issue: Permission Denied Errors

Symptoms:

ERROR : file_name: Failed to copy: open /path/to/file: permission denied

Solution:

  • Ensure rclone has read permissions for the files:
  sudo chmod -R u+r /home/ubuntu

Issue: Failed to Create File System for "gdrive:": Didn't Find Section in Config File

Cause:

  • The remote name used in the command doesn't match any configured remote.

Solution:

  • Verify your remote names:
  rclone listremotes
  • Use the correct remote name in your command.

Issue: Files Not Uploading Due to Invalid Characters

Cause:

  • Some files have names with characters not supported by Google Drive.

Solution:

  • Rename files to remove unsupported characters.
  • Use rclone's filename encoding options if necessary.

Issue: Exceeding Google Drive Storage Quota

Solution:

  • Check your Google Drive storage usage.
  • Remove unnecessary files or consider upgrading your storage plan.

Conclusion

Backing up your Ubuntu server to Google Drive using rclone on a headless machine is a straightforward process once you understand the steps involved. By following this guide, you can ensure that your important data is securely stored in the cloud, providing peace of mind and a safeguard against data loss.

Key Takeaways:

  • rclone is a versatile tool for syncing data to cloud storage.
  • Manual authorization allows configuration on headless machines.
  • Automating backups with cron ensures regular data protection.

Additional Resources


Have questions or feedback? Feel free to leave a comment below or reach out to me directly.

🚀 **Support Our DevOps Blog with Your Amazon Shopping!** 🚀 Love shopping on Amazon? Now you can fuel your shopping spree *and* support our blog at no extra cost! Just use our link for your next purchase: **[Shop on Amazon & Support Us!] Browse Stuff on Amazon Every click helps us keep sharing the DevOps love. Happy shopping!

Leave a Comment