Using Rclone to mount Google Drive for Plex

What is rclone?

Rclone is a command-line program to manage files on cloud storage. It is a feature-rich alternative to cloud vendors’ web storage interfaces. Over 40 cloud storage products support rclone including S3 object stores, business & consumer file storage services, as well as standard transfer protocols.

Users call rclone “The Swiss army knife of cloud storage”, and “Technology indistinguishable from magic”.

Source: rclone.com

Why use rclone?

Over time your Plex library can grow to extraordinary sizes, especially if you value a decent quality of your movies and series. The first intuitive solution would be to grab a decently sized storage VPS and mount it’s drive to our Plex server over NFS or perhaps SSHFS. The problem is that storage VPSs usually have a second, if not third-tier networking. With multiple users trying to fetch different movies you might also hit a CPU cap, especially if you encrypt the data stream (SSHFS or NFS over encrypted VPN).

At some point, you might hit the magic barrier of 10€/mo for storage at which point a storage server is no longer cost-effective, as unlimited space can be achieved under the threshold of 10€.

Google to the rescue!

Google offers unlimited Drive space in its GSuite Business service. In some regions it outright says unlimited, but in most regions the description states “or 1TB per user if fewer than 5 users”, however, it’s a well-known industry secret that Google has never enforced that restriction and probably won’t do that in the near future. If you are still unsure about your data safety - Google always downgrades (be it student plan or paid plan) accounts in a non-destructive fashion. You still have the access to all your files, you just can’t upload any more.

Rclone instalaltion

Rclone provides a one-line installation script:

curl https://rclone.org/install.sh | sudo bash

You can also go to rclone.org/downloads if you want to go the more traditional way and install the package manually.

Rclone configuration

The best practice is to obtain our own Google Drive API key as rclone’s default key is shared among its users. There is a global rate limit on the number of queries per second that each key can do set by Google. Rclone has a high quota and is working with Google to keep it high enough, however, we are power users here, aren’t we?

  1. Log into the Google API Console (the account doesn’t matter).
  2. Select a project or create a new one.
  3. Click ENABLE APIS AND SERVICES on the top, search for Drive and enable Google Drive API
  4. Now go to Credentials on your left-side panel
  5. Proceed to create an OAuth Client ID by pressing CREATE CREDENTIALS on top.
  6. If you haven’t previously created a consent screen go ahead and configure it now (choose external, you do not have to audit it). All you have to set is the name. After creating the consent screen try to create the OAuth credentials.
  7. For the Application Type choose TVs and Limited Input devices and enter any name you desire.
  8. Keep the Client ID and Secret around (you can always access it by pressing the pen icon next to the credential).

Creating the first remote in rclone

Start by executing rclone config:

No remotes found - make a new one
n) New remote
r) Rename remote
c) Copy remote
s) Set configuration password
q) Quit config

Enter n for new remote and give it a name (make it memorable)

n/r/c/s/q> n
name> GDriveMrPsycho

After entering the name you will be presented with a list of all available remote types, we are interested in drive

XX / Google Drive
\ "drive"
Storage> drive

Now it’s time to enter your client ID and secret.

Google Application Client Id
client_id> .......................
Google Application Client Secret
client_secret> .......................

Now choose a scope:

Scope that rclone should use when requesting access from drive.
Choose a number from below, or type in your own value
1 / Full access all files, excluding Application Data Folder.
\ "drive"
2 / Read-only access to file metadata and file contents.
\ "drive.readonly"
/ Access to files created by rclone only.
3 | These are visible in the drive website.
| File authorization is revoked when the user deauthorizes the app.
\ "drive.file"
/ Allows read and write access to the Application Data folder.
4 | This is not visible in the drive website.
\ "drive.appfolder"
/ Allows read-only access to file metadata but
5 | does not allow any access to read or download file content.
\ "drive.metadata.readonly"
scope> 1

If you want to restrict rclone to a specific folder inside your drive you can enter root_fodler_id in the next step. You can get the folder ID from URL when accessing Drive in the browser. For example from https://drive.google.com/drive/u/1/folders/1xDLZTo0x1kOJLXke9mUqe3l8EiUXXXXX, we obtain 1xDLZTo0x1kOJLXke9mUqe3l8EiUXXXXX. This also works with folders inside team drives.

For the sake of later convenience, I suggest creating a folder if you haven’t done it previously.

ID of the root folder - leave blank normally.  Fill in to access "Computers" folders. (see docs).
root_folder_id> 1xDLZTo0x1kOJLXke9mUqe3l8EiUXXXXX

Next, you will be asked for Service Account Credentials. These are useful for using rclone in unattended mode (i.e. not tied to a specific end-user Google account); …or when you want to bypass the 750GB daily upload limit. However service accounts are beyond the scope of this tutorial, so we leave the field empty.

…also, we are not interested in advanced config so skip that part too.

Service Account Credentials JSON file path - needed only if you want use SA instead of interactive login.
service_account_file>
Edit advanced config? (y/n)
y) Yes
n) No (default)
y/n> n

In the next step, we have to give rclone permission to use our Google Drive. If you are on a desktop machine enter y; if you are on a remote or headless machine enter n.

Depending on what have you chosen, either a browser will open and rclone will obtain the code automagically or you will be given a link to Google’s OAuth page - after you grant the access you will be given verification code, paste it in the console.

Remote config
Use auto config?
* Say Y if not sure
* Say N if you are working on a remote or headless machine
y) Yes (default)
n) No
y/n> n
Please go to the following link: https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=dsagfresfdg&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&response_type=code&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive&state=XJbwTiRbqGLtDAAOers1jg
Log in and authorize rclone for access
Enter verification code> ..................................

Now rclone will ask if you want to use your main drive or a team drive. The decision is up to you.

Configure this as a team drive?
y) Yes
n) No
y/n> n

The last step is to confirm if our configuration is OK

[GDriveMrPsycho]
client_id = .......................
client_secret = .......................
scope = drive
root_folder_id = 1xDLZTo0x1kOJLXke9mUqe3l8EiUXXXXX
service_account_file =
token = {"access_token":"XXX","token_type":"Bearer","refresh_token":"XXX","expiry":"2014-03-16T13:57:58.955387075Z"}
--------------------
y) Yes this is OK
e) Edit this remote
d) Delete this remote
y/e/d> y

…and we are done! We can technically now mount the drive, but since we are going to use the drive for storing movies for Plex let’s proceed with some tweaks.

Cache & Crypt

Since you are now familiar with the rclone configurator there is no need to go through it for another two remotes. We will edit the rclone.conf straight away to save some time. You should have no problems understanding the syntax.

Do not loose : after parent remote!

~/.config/rclone/rclone.conf

[GDriveMrPsycho]
type = drive
client_id = .......................
client_secret = .......................
scope = drive
root_folder_id = 1xDLZTo0x1kOJLXke9mUqe3l8EiUXXXXX
token = {"access_token":"XXX","token_type":"Bearer","refresh_token":"XXX","expiry":"2020-07-12T23:40:10.189116862+02:00"}
team_drive = 0AMxMkXCM8SHcUkXXXX

[GDriveMrPsychoCache]
type = cache
remote = GDriveMrPsycho:
chunk_size = 50M
info_age = 1h0m0s
chunk_total_size = 10G

[GDriveMrPsychoCrypt]
type = crypt
remote = GDriveMrPsychoCache:
filename_encryption = standard
directory_name_encryption = true

The only param we are now missing is obviously the encryption password and salt.

DO NOT SKIP THIS PART

To add the encryption password and salt we once again execute rclone config and we edit the crypt remote:

Current remotes:

Name Type
==== ====
GDriveMrPsycho drive
GDriveMrPsychoCache cache
GDriveMrPsychoCrypt crypt

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> e
Choose a number from below, or type in an existing value
1 > GDriveMrPsycho
2 > GDriveMrPsychoCache
3 > GDriveMrPsychoCrypt
remote> 3
--------------------
[GDriveMrPsychoCrypt]
type = crypt
remote = GDriveMrPsychoCache:
filename_encryption = standard
directory_name_encryption = true
--------------------
Edit remote
** See help for crypt backend at: https://rclone.org/crypt/ **

Value "remote" = "GDriveMrPsychoCache:"
Edit? (y/n)>
y) Yes
n) No (default)
y/n> n
Value "filename_encryption" = "standard"
Edit? (y/n)>
y) Yes
n) No (default)
y/n> n
Value "directory_name_encryption" = "true"
Edit? (y/n)>
y) Yes
n) No (default)
y/n> n
Value "password" = ""
Edit? (y/n)>
y) Yes
n) No (default)
y/n> y
Password or pass phrase for encryption.
y) Yes type in my own password
g) Generate random password
y/g> y
Enter the password:
password:
Confirm the password:
password:
Value "password2" = ""
Edit? (y/n)>
y) Yes
n) No (default)
y/n> y
Password or passphrase for salt. Optional but recommended.
Should be different to the previous password.
y) Yes type in my own password
g) Generate random password
n) No leave this optional password blank (default)
y/g/n> y
Enter the password:
password:
Confirm the password:
password:
--------------------
[GDriveMrPsychoCrypt]
type = crypt
remote = GDriveMrPsychoCache:
filename_encryption = standard
directory_name_encryption = true
password = *** ENCRYPTED ***
password2 = *** ENCRYPTED ***
--------------------
y) Yes this is OK (default)
e) Edit this remote
d) Delete this remote
y/e/d> y

Mounting the Drive

Manual

To check if our configuration is working we can execute rclone mount and try to access the mount in another terminal/SSH session. As with any mount, make the directory earlier. The following arguments for rclone are best suited for Plex and come from my own trial and error.

mkdir /mnt/GDriveMrPsycho
rclone mount GDriveMrPsychoCrypt: /mnt/GDriveMrPsycho --allow-other --cache-db-purge --fast-list --poll-interval 10m

On startup via systemd

If our configuration is working we can proceed and make a simple service. Remember about the --config - point it to your config.

/etc/systemd/system/GDriveMrPsycho.service

[Unit]
Description=rclone for GDriveMrPsycho
AssertPathIsDirectory=/mnt/GDriveMrPsycho
After=networking.service

[Service]
Type=simple
ExecStart=rclone mount --config=/home/jacob/.config/rclone/rclone.conf GDriveMrPsychoCrypt: /mnt/GDriveMrPsycho --allow-other --cache-db-purge --fast-list --poll-interval 10m
ExecStop=/bin/fusermount -u /mnt/GDriveMrPsycho
Restart=always
RestartSec=10

[Install]
WantedBy=default.target

That’s it!

You can now head to Plex and add your mount.

If you want to use the same mount on another machine and have your file decrypted all you have to do is to copy the password and password2 from rclone.conf to the new machine.

So you've read the whole article? How about signing up for more?

Email Address

No spam, I promise