Linux to OneDrive via Synology


I run Home Assistant and a number of other things from a small Linux PC that acts as my server. It’s not storing much critical data but it would be a pain if I lost all of the configuration.

I have my critical documents backed up to OneDrive, so it made sense to do the same for critical files on my server. Luckily I have a Synology NAS and it provides an easy solution. One of the applications available for the Synology is Cloud Sync, which allows you to synchronise a directory on the NAS with cloud storage from a wide variety of providers.

This allows me to make the Synology NAS a gateway between the Linux server and OneDrive by mounting the NAS directory on the Linux server via NFS. Anything copied to the NAS mount point on the Linux server will automatically be synchronised with OneDrive via Cloud Sync.

You can choose to sync your entire OneDrive or pick a subdirectory to sync. I’ve opted to sync a single directory under OneDrive that I’ve created for the purpose of the backups.

I don’t need my entire OneDrive on the NAS, and it insulates the rest of my files from the Linux server. That way, if I do something really stupid, like run a recursive delete on the Linux server, it will only affect the backups and not the rest of my documents.

Configuring the NFS on the Synology is quite simple. First I had to enable NFS within File Services:

And then set the NFS permissions for the shared folder:

Mounting the folder on the Linux side just required a directory for the mount point and an entry in the /etc/fstab file:

IP_ADDRESS_OF_NAS:/volume1/onedrive  /mnt/onedrive nfs defaults 0 0

I can now copy files onto OneDrive from the Linux server. There are any number of ways of using the mount point to backup the critical files on the server.

I could simply store the files directly on the NFS mount and access them from there, rather than use the internal drive, but I don’t want to create a dependency on the NAS. The mountpoint will be used only for backups.

I only need certain files backed up, so I’ve chosen to set up a two step process in a backup script.

Step one is to gather files into my home directory, under a “backups” subdirectory. I use /opt/local for everything that isn’t part of a base OS install, and /data for, well, data, so those are the directories I’m most interested in. I also make sure I have an up to date copy of my Apache configuration, as it was quite fiddly to get right.

Step two is to copy my home directory into the OneDrive mount point.

I’m using rsync to ensure that only the files that have changed get copied, and with the delete option to ensure that the target directories get cleaned up when files are deleted in the source directories.

#!/bin/bash

# Step 1: Copy application files into home directory:

rsync -av --delete /etc/httpd/conf.d/*.conf /home/sean/backups/configs/apache/
rsync -av --delete --exclude-from "/home/sean/backups/exclude.txt" /data/ /home/sean/backups/data/
rsync -av --delete --exclude-from "/home/sean/backups/exclude.txt" /opt/local/ /home/sean/backups/opt/local/

# Step 2: Copy home directory to NAS:
rsync  -av --delete --exclude-from "/home/sean/backups/exclude.txt" /home/sean/ /mnt/onedrive/home/

This is then run daily from cron:

0 3 * * * /home/sean/bin/backup.sh > /home/sean/backups/backup.log 2>&1

The exclusion list includes things like docker images, Python virtual environments, temporary files and a mysql database that lives under /data. The actual MySQL database itself gets backed up separately using the mysqldump command to export the database into a file in my home directory, from where it will be copied to OneDrive by the main backup script:

mysqldump --all-databases -u backup -p<password> | gzip > /home/sean/backups/mysql/mysql.bak.gz

In short, if you have a Synology NAS you can use it for more than just local storage. With Cloud Sync and a bit of Linux configuration, you can use it to ensure that you have offsite backups for your Linux machines.

in Home Automation

Add a Comment

Your email address will not be published. All comments will be reviewed.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts