Linux

How To Sync/Backup Files From Linux Servers To OneDrive

It’s recommended to follow a 3-2-1 backup strategy when taking Linux backups of important data. So the idea is that you have three copies of your data. (3) Local Backups on the server. (2) Backups located off the server. (1) Backups located in the cloud. Most providers like first2host.co.uk provide backups. In the case of Linux VPS and Linux Dedicated Servers, you might choose to store backups locally on the server. That’s fine. But if you do decide to do this it’s also important that you store backups somewhere off the server. So you can install OneDrive for Linux to accomplish this and sync/backup Linux Files to OneDrive or regularly sync files to OneDrive.

When taking backups of Linux VPS servers we store your backups offsite. If you store backups in your server you have already taken care of (1). We cover you for (2). For (3) and the ultimate redundancy, you can install OneDrive for Linux on all F2H servers. OneDrive will allow you to sync files and folders to OneDrive. Here we will install OneDrive for Linux, configure OneDrive for Linux VPS, and Sync some files and folders to OneDrive.

Install OneDrive Client Ubuntu 22

You can install the OneDrive Linux client on most Linux distributions. For a full list of compatible Operating Systems please see here. We’re installing the OneDrive client on a Ubuntu 22 NVMe VPS Server. You must follow the instructions for your OS. If you are not using Ubuntu see the link above and find your OS. Basic install instructions are located above. Run each command below to install the Ubuntu OneDrive Client on your Linux Server.

wget -qO - https://download.opensuse.org/repositories/home:/npreining:/debian-ubuntu-onedrive/xUbuntu_22.04/Release.key | gpg --dearmor | sudo tee /usr/share/keyrings/obs-onedrive.gpg > /dev/null

echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/obs-onedrive.gpg] https://download.opensuse.org/repositories/home:/npreining:/debian-ubuntu-onedrive/xUbuntu_22.04/ ./" | sudo tee /etc/apt/sources.list.d/onedrive.list
 
apt-get update
apt install --no-install-recommends --no-install-suggests onedrive

Now you have the OneDrive client on your server, it’s time to connect it to OneDrive. In the console run the following command and copy + paste the URL in the console to your web browser. Sign in with the OneDrive account you want to link to your Ubuntu 22 server.

onedrive
sync linux onedrive

Once you have logged in to OneDrive you will see a black page. This is normal. Look at the URL and copy the entire URL in the browser address bar to the console.

Back in the SSH console of your Ubuntu 22 server paste the entire URL and press enter. Your Ubuntu server will now be connected to your OneDrive account.

Configure Linux Backup Sync to OneDrive

In its default form, the OneDrive client for Linux will sync documents in the ~/OneDrive folder on your server to your OneDrive account. When using the OneDrive client for Linux backups you likely only want a one-way sync. At the moment, if you sync OneDrive with your Linux server all data in your OneDrive account will be downloaded to your server. That’s unnecessary if you simply want to store backups from your Linux server in OneDrive. So next, let’s set up a One-Way sync to OneDrive. Create the below file and copy the defaults for the OneDrive client to the file. You can download our file to your server with the below command or copy and paste the configuration.

cd /etc/onedrive && wget https://f2h.ha-cdn.com/Configs/onedrive/config
nano /etc/onedrive/config

# Default OneDrive Client Configuration

# Configuration for OneDrive Linux Client
# This file contains the list of supported configuration fields
# with their default values.
# All values need to be enclosed in quotes
# When changing a config option below, remove the '#' from the start of the line
# For explanations of all config options below see docs/USAGE.md or the man page.
#
sync_dir = "/backup/"
# skip_file = "~*|.~*|*.tmp"
# monitor_interval = "300"
# skip_dir = ""
# log_dir = "/var/log/onedrive/"
# drive_id = ""
# upload_only = "false"
# check_nomount = "false"
# check_nosync = "false"
# download_only = "false"
# disable_notifications = "false"
# disable_upload_validation = "false"
# enable_logging = "false"
# force_http_11 = "false"
# local_first = "false"
# no_remote_delete = "false"
# skip_symlinks = "false"
# debug_https = "false"
# skip_dotfiles = "false"
# skip_size = "1000"
# dry_run = "false"
# min_notify_changes = "5"
# monitor_log_frequency = "6"
# monitor_fullscan_frequency = "12"
# sync_root_files = "false"
classify_as_big_delete = "10000"
# user_agent = ""
# remove_source_files = "false"
# skip_dir_strict_match = "false"
# application_id = ""
# resync = "false"
# resync_auth = "false"
# bypass_data_preservation = "false"
# azure_ad_endpoint = ""
# azure_tenant_id = "common"
# sync_business_shared_folders = "false"
# sync_dir_permissions = "700"
# sync_file_permissions = "600"
# rate_limit = "131072"
# webhook_enabled = "false"
# webhook_public_url = ""
# webhook_listening_host = ""
# webhook_listening_port = "8888"
# webhook_expiration_interval = "86400"
# webhook_renewal_interval = "43200"
# space_reservation = "50"
# display_running_config = "false"
# read_only_auth_scope = "false"
# cleanup_local_files = "false"
# operation_timeout = "3600"
# dns_timeout = "60"
# connect_timeout = "10"
# data_timeout = "600"
# ip_protocol_version = "0"

Note that there are only two default configuration options we need to change

sync_dir = "/backup/"
classify_as_big_delete = "10000"

The sync_dir is the directory we are going to sync to OneDrive. Anything inside this folder will be transported to our OneDrive storage. The classify_as_big_delete option allows us to delete many files remotely. You may not need this option but you won’t be able to delete a large amount of files out of the sync_dir without raising the default limit. Set a sync directory and if needed edit the classify_as_big_delete option. Save and close the /etc/onedrive/config.

Once you have set the sync directory, create another folder inside the directory. This will be the folder we sync to OneDrive. Ours is /backup/dev.f2h.cloud. Everything in the dev.f2h.cloud folder will be synced.

Save Backups To Sync Location

We are using a simple bash script to copy our website files to the sync directory. This script runs on a cron job at 1 AM each day. This file simply zips our development websites files and places them in our sync directory inside a folder called “website”.

Save Website Files – cp_website_files.sh

#!/bin/bash
cd /backup/dev.f2h.cloud/website
zip -r website-files.zip /var/www/dev.f2h.cloud

Save Database

To save our database we have configured automysqlbackup to backup our databases to a folder called mysql that is located inside our sync directory. This runs on a cronjob at 2 AM each day. Please see the above link for details on how to configure automysqlbackup.

Sync To OneDrive – one_drive_sync.sh

And finally, we need to create the folder we created inside the sync directory in OneDrive. The OneDrive client cannot create folders. We created the dev.f2h.cloud folder in our OneDrive account and our database is located at /backups/dev.f2h.cloud/mysql and our website files at /backups/dev.f2h.cloud/website. To perform the sync we are using a simple bash script with the –upload-only flag.

#!/bin/bash
onedrive --synchronize --upload-only

Confirm OneDrive For Linux is working

When you run the onedrive –synchronize –upload-only –dry-run command this will show you what will happen. No files will be uploaded to OneDrive. Run the command with –dry-run first. Don’t forget to place some files inside your sync directory. If all looks good. Run the same command without –dry-run to backup your Linux files to OneDrive.

onedrive --synchronize --upload-only

And finally, inspect your OneDrive account and confirm the backups were uploaded successfully.

So, we now have our Linux backups uploaded from our Ubuntu 22 server to OneDrive. The entire /backup/dev/f2h/cloud folder is being synced to OneDrive daily at 3 AM. But whilst OneDrive isn’t exactly designed to work with Linux, it is an excellent tool to have to ensure data redundancy in a 3-2-1 backup strategy. You can go further! The OneDrive client for Linux is able to sync your entire OneDrive folder with your Linux server. See the links above for full details.

2 Comments

Leave a Reply

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

Back to top button