Synology HyperBackup - backup to OneDrive by rclone
===
```mermaid
graph TD;
HyperBackup -- WebDAV protocol -->rclone_ext[container exposed port, :10080]
rclone_int[rclone as WebDAV server, :8080] -- OneDrive API --> OneDrive
rclone_ext-- internal routing [10080:8080] --> rclone_int
subgraph rclone_conatiner[rclone container]
rclone_int
end
subgraph Synology NAS
HyperBackup
rclone_conatiner
rclone_ext
end
```
### Overview
- Create a `rclone.conf` file
- Authorize rclone an access token from browser to let it operate your OneDrive API when backup
- Move `rclone.conf` into your NAS, will mount the file to rclone container later
- Create a rclone container on NAS
- Run rclone as WebDAV serve mode
- Run rclone with `rclone.conf` that we created at first step
- Configure WebDAV by setting container's environment variables
- Create a HyperBackup task
- Choose WebDAV
- Set destination to the rclone container
- LET'S GO!!!
### Setup rclone config
- In this step, we'll create a `rclone.conf` file which contains an access token for rclone to operate OneDrive API
- We'll mount `rclone.conf` to rclone container later, so the rclone in the container can have access to OneDrive
- In this article, I'll create `rclone.conf` on my macOS because it's much easier to authorize rclone in a browser than typing a lot of commands in a terminal
:::danger
Be aware of not to upload the config file or the token to the public or anyone.
`rclone.conf` contains an access token to give rclone permission to operate OneDrive API. Anyone have the file or the token may have access to your account.
:::
#### Download rclone
- Download official rclone binary from https://rclone.org/downloads/
- If you run rclone on macOS, it may be blocked by the OS since the executable is not signed by a valid certificate.
- Go to `System > Privacy & Security`, find rclone at the bottom and choose "Open Anyawy"
#### Make a rclone config for OneDrive remote
- Basically follows https://rclone.org/onedrive/ , I'll go through the guide in the following--
- Configure rclone config in terminal:
```shell=
## `--config`: generate config file to a specific location
> ./rclone config --config ./rclone.conf
No remotes found, make a new one?
n) New remote
s) Set configuration password
q) Quit config
n/s/q> n
## This "remote name" will be used later when we setup rclone container in NAS
Enter name for new remote.
name> onedrive
## Find the number of Microsoft OneDrive
Option Storage.
Type of storage to configure.
Choose a number from below, or type in your own value.
1 / 1Fichier
\ (fichier)
2 / Akamai NetStorage
\ (netstorage)
3 / Alias for an existing remote
\ (alias)
...
35 / Microsoft OneDrive
\ (onedrive)
...
Storage> 35
## Just leave blank on client_id and client_secret
Option client_id.
OAuth Client Id.
Leave blank normally.
Enter a value. Press Enter to leave empty.
client_id>
Option client_secret.
OAuth Client Secret.
Leave blank normally.
Enter a value. Press Enter to leave empty.
client_secret>
Option region.
Choose national cloud region for OneDrive.
Choose a number from below, or type in your own value of type string.
Press Enter for the default (global).
1 / Microsoft Cloud Global
\ (global)
2 / Microsoft Cloud for US Government
\ (us)
3 / Microsoft Cloud Germany
\ (de)
4 / Azure and Office 365 operated by Vnet Group in China
\ (cn)
region> 1
Edit advanced config?
y) Yes
n) No (default)
y/n> n
Use web browser to automatically authenticate rclone with remote?
* Say Y if the machine running rclone has a web browser you can use
* Say N if running rclone on a (remote) machine without web browser access
If not sure try Y. If Y failed, try N.
y) Yes (default)
n) No
y/n> y
```
It will launch your browser and lead to a Microsoft Oauth page asking for accessing to your account. rclone will save the access token for operating the OneDrive API.
Once you finish the login process in your browser, it will redirect to this success page, then we can move back to the terminal for the next step.

```shell=
2024/11/11 17:06:36 NOTICE: If your browser doesn't open automatically go to the following link: http://127.0.0.1:XXXX/auth?state=XXXXXXXXXXXXXX
2024/11/11 17:06:36 NOTICE: Log in and authorize rclone for access
2024/11/11 17:06:36 NOTICE: Waiting for code...
2024/11/11 17:07:12 NOTICE: Got code
Option config_type.
Type of connection
Choose a number from below, or type in an existing value of type string.
Press Enter for the default (onedrive).
1 / OneDrive Personal or Business
\ (onedrive)
2 / Root Sharepoint site
\ (sharepoint)
/ Sharepoint site name or URL
3 | E.g. mysite or https://contoso.sharepoint.com/sites/mysite
\ (url)
4 / Search for a Sharepoint site
\ (search)
5 / Type in driveID (advanced)
\ (driveid)
6 / Type in SiteID (advanced)
\ (siteid)
/ Sharepoint server-relative path (advanced)
7 | E.g. /teams/hr
\ (path)
config_type> 1
Option config_driveid.
Select drive you want to use
Choose a number from below, or type in your own value of type string.
Press Enter for the default (XXXXXXXXXX).
1 / OneDrive (personal)
\ (XXXXXXXXXX)
config_driveid> 1
Drive OK?
Found drive "root" of type "personal"
URL: https://onedrive.live.com?cid=XXXXXXXXX&id=XXXXXXXXX
y) Yes (default)
n) No
y/n> y
Configuration complete.
Options:
- type: onedrive
- token: {"access_token":"XXXXXXX","expiry":"XXXXXXX"}
- drive_id: XXXXXXXXX
- drive_type: personal
Keep this "onedrive" remote?
y) Yes this is OK (default)
e) Edit this remote
d) Delete this remote
y/e/d> y
Current remotes:
Name Type
==== ====
onedrive onedrive
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> q
```
It should create a `rclone.conf` alongside with the rclone executable.
For now, we've completed the connection between rclone and OneDrive. In the next step, we'll start setting up the connection between HyperBackup and rclone.
### Setup rclone on Synology NAS
#### Move `rclone.conf` to NAS
- Create a folder somewhere in the NAS named `rclone`, and move `rclone.conf` into that folder

#### Create rclone container
- Pull latest official rclone image from `Container Manager > Registry > rclone/rclone > latest`

- Create a container from `rclone/rclone` image

##### Port Settings

- 10080 port will be the port that HyperBackup connect to.
- 8080 port will be the port that rclone listen for WebDAV connections inside the container
- In the example, HyperBackup will send requests to host's :10080 port, and the request will be redirected to the :8080 port inside rclone container which rclone is listening for WebDAV requests.
##### Volume Settings

- Choose "Add Folder"
- Choose the rclone folder [we just created on the NAS](#Move-rcloneconf-to-NAS). It must has a `rclone.conf` under it.
- Mount location: `/config/rclone`
##### Environment

- `RCLONE_ADDR`: address that rclone runs as WebDAV server
- `RCLONE_USER`: username when you connect to rclone by WebDAV
- `RCLONE_PASS`: password when you connect to rclone by WebDAV
- (optional) `RCLONE_VFS_CACHE_MODE`: `full`
- Cache everything inside the container, so it will use twice space at most when you first time backup in NAS. Your backup will first cache in the container, and upload to OneDrive continuously
- (optional) `RCLONE_CACHE_DIR`: `/temp`
- Where to put the cache
##### Execution Command

- Command: `serve webdav onedrive:`
- `serve webdav` - Let rclone serve as WebDAV server
- `onedrive:` - the remote name in the `rclone.conf`
### Setup HyperBackup Task
- Create task and choose `WebDAV`

- Setup Destination Settings
We'll fill the setting with the environment variables that we set for rclone container

- Server address: `http://localhost:[expose port that binds to RCLONE_ADDR]`
- Username: `[RCLONE_USER]`
- Password: `[RCLONE_PASS]`
- Folder: Folder on OneDrive that you want to put your backup file. If you can list the folder in the option, it means your setting of the rclone and OneDrive works.
- Directory: HyperBackup will create a new folder for backup under the `Folder` you choosed in the last column.
### That's it! Let's Backup!
