September 11, 2020

Using OneDrive on Mac or Linux for Backups

Recently I discovered that I have access to Office 365 through my university. I am not interested in Office 365 at all, but my subscription includes 5 Terra (yes!) Bytes of OneDrive storage. This has aroused my full interest.

But: the official OneDrive client is not really what I want, because I don't want to synchronize stuff from a folder on my hard drive with OneDrive. I would like to “mount” OneDrive and use a tool, preferably BorgBackup or something similar, to transfer encrypted snapshots to OneDrive. However, you cannot use WebDAV, sshfs or the other tools you normally use for this purpose as OneDrive is Microsoft.

But there is a wonderful tool called rclone that I have never heard of before. rclone can connect to many online storage solutions (called “remotes” in rclone jargon), including OneDrive, Mega, … Backblaze, etc. Connecting rclone to OneDrive is a little surprising, since you are not using your username and password from OneDrive. Instead, you have to go through an automated setup process which is described [here] (https://rclone.org/onedrive/). At the end you will get a credential from OneDrive which is automatically fed into rclone, and then you can do things like rclone copy /path/to/source code onedrive:/path/to/destination, rclone tree --level 2 onedrive:, or rclone mount onedrive: /path/to/mount point`. Practical…

However, it turns out that BorgBackup does not work well with rclone. At least that's what I found in some bug reports and other online discussions. In one of these discussions someone mentioned another wonderful tool called [restic] (https://restic.net/), which I already knew. It is an alternative to BorgBackup, i.e. it offers an encrypted backup service with snapshots. Unlike BorgBackup, restic can connect to some online storage solutions like Backblaze on its own. However, it cannot connect directly to OneDrive. But it can use any “remote” that is configured in rclone!

So after you have configured OneDrive in rclone, you can initialize a folder in OneDrive as your backup destination:

#init.sh
#!/bin/bash
export RESTIC_REPOSITORY="rclone:onedrive:backups/macpro"
export RESTIC_PASSWORD_FILE="pass"
restic init

Note: you need a file pass which contains a strong password that you can generate with something like openssl rand -base64 32 > pass.

Backups are also simple to achieve:

#backup.sh
#!/bin/bash
export RESTIC_REPOSITORY="rclone:onedrive:backups/macpro"
export RESTIC_PASSWORD_FILE="pass"
restic backup --verbose --exclude-file excludes --exclude-caches /path/to/folder/1 /path/to/folder/2
restic snapshots

The file excludes contains patterns of files you don't want to be included in the backup. In my case this would be something like:

#excludes
._.DS_Store
.DS_Store
._*
*.bak
...

Restoring the latest snapshot would work as follows:

#restore.sh
#!/bin/bash
export RESTIC_REPOSITORY="rclone:onedrive:backup/macpro"
export RESTIC_PASSWORD_FILE="pass"
restic restore latest --target /path/to/restore

Neato!

© holger 2015 - 2020 |