Finally got UnionFS, RClone, and the bandwidths limit right for Google Drive Sync

First create the plexdrive:

https://github.com/Admin9705/The-Awesome-Plex-Server/blob/Version-3/02%20-%20PlexDrive4

From there, these instructions will enable rclone and unionfs to sync to google drive. The good thing is that the read only portion bounces ONLY against plexdrive. Basically, you’ll never trigger API bans.

https://github.com/Admin9705/The-Awesome-Plex-Server/blob/Version-3/03%20-%20RClone%20-%20UnionFS%20Sync%20%26%20Move

All inspired by @aj1252

1 Like

One tip regarding the cron script.

Either add a check to see if already running:
For example create a pid file at start, delete at end. Exit script if already excists.

Or even better just loop forever:
Change cron to start on boot and just use a while true loop to run forever.

Im using:

#!/bin/bash
while true
do
rclone *****
sleep 300
done

Thanks @aj1252, That’s even smarter. So basically when the sync is done, the way you have it above is that it starts in 6 minutes… I’m getting at? I had it set to every 59 minutes because I noticed that if a certain show was running and if the script kicked in again, it would try to upload the same show.

You only run it once so there will never be any duplicate uploads like you are experiencing.

Instead of:
*/30 * * * * /bin/bash /opt/rclone-move.sh >/dev/null 2>&1
use
@reboot /bin/bash /opt/rclone-move.sh >/dev/null 2>&1

Then rclone-move.sh =

#!/bin/bash
while true
do
    rclone move --bwlimit 8M --tpslimit 8 --max-size 99G --log-level INFO --stats 15s local:/mnt/rclone-move gdrive:/
    sleep 300
done

It will run rclone
Wait 5 minutes, incase there is nothing to do so to prevent spaming the command.
Run rclone again
Wait 5
Run rclone
etc…

Actually, I’m testing this now. I’m running as a service instead of cronjob. Should work, got the green light.

################# Move Service ################# START

Create Script ### Inspired by aj1252 - rclone forum

Create the SCRIPT the CMD

sudo nano /opt/rclone-move.sh

######## Copy ####### START
#!/bin/bash

while true
do
sleep 30
rclone move --bwlimit 10M --tpslimit 4 --max-size 99G --log-level INFO --stats 15s local:/mnt/rclone-move gdrive:/
sleep 270
done

######## Copy ####### END

Press CTRL+X and ENTER; creating a CRONJOB as ROOT

Script permissions

sudo chmod 755 /opt/rclone-move.sh

Create UnionFS Service

sudo nano /etc/systemd/system/move.service

START COPY

[Unit]
Description=Move Service Daemon
After=multi-user.target

[Service]
Type=simple
User=root
Group=root
ExecStart=/bin/bash /opt/rclone-move.sh
TimeoutStopSec=20
KillMode=process
Restart=always

[Install]
WantedBy=multi-user.target

END COPY

Press CTRL+X and then Yes to save

Start and enable the service

sudo systemctl daemon-reload
sudo systemctl enable move.service
sudo systemctl start move.service
sudo systemctl status move.service

Press CTRL + C to exit the status message

################# RClone Move Service ################# END

1 Like

So here is the updated crazyiness that works :smiley:

## Making the directories and setting the permissions
mkdir /mnt/rclone-union 1>&2
mkdir /mnt/rclone-move 1>&2
chmod 755 /mnt/rclone-move
chmod 755 /mnt/rclone-union

## Installing rclone
cd /tmp
curl -O https://downloads.rclone.org/rclone-current-linux-amd64.zip
unzip rclone-current-linux-amd64.zip
cd rclone-*-linux-amd64
cp rclone /usr/bin/
chown root:root /usr/bin/rclone
chmod 755 /usr/bin/rclone
mkdir -p /usr/local/share/man/man1
cp rclone.1 /usr/local/share/man/man1/
mandb
cd .. && sudo rm -r rclone*

## Making rclone directory
mkdir /mnt/rclone 2>/dev/null
chmod 755 /mnt/rclone
chown root /mnt/rclone

## Replace Fuse by removing the # from user_allow_other
rm -r /etc/fuse.conf
tee "/etc/fuse.conf" > /dev/null <<EOF
# /etc/fuse.conf - Configuration file for Filesystem in Userspace (FUSE)

# Set the maximum number of FUSE mounts allowed to non-root users.
# The default is 1000.
#mount_max = 1000

# Allow non-root users to specify the allow_other or allow_root mount options.
user_allow_other
EOF

## Create the RClone Service
tee "/etc/systemd/system/rclone.service" > /dev/null <<EOF
[Unit]
Description=RClone Daemon
After=multi-user.target

[Service]
Type=simple
User=root
Group=root
ExecStart=/usr/bin/rclone --allow-non-empty --allow-other mount gdrive: /mnt/rclone --bwlimit 8650k --size-only
TimeoutStopSec=20
KillMode=process
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target
EOF

## Enable RClone Service
sudo systemctl daemon-reload

## Create the UnionFS Service
tee "/etc/systemd/system/unionfs.service" > /dev/null <<EOF
[Unit]
Description=UnionFS Daemon
After=multi-user.target

[Service]
Type=simple
User=root
Group=root
ExecStart=/usr/bin/unionfs -o cow,allow_other,nonempty /mnt/rclone-move=RW:/mnt/plexdrive4=RO /mnt/rclone-union
TimeoutStopSec=20
KillMode=process
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target
EOF

## Enable UnionFS Service
sudo systemctl daemon-reload

## Create the Move Script
tee "/opt/rclone-move.sh" > /dev/null <<EOF
#!/bin/bash
sleep 60
while true
do
# Purpose of sleep starting is so rclone has time to startup and kick in (1HR, you can change)
# Anything above 9M will result in a google ban if uploading above 9M for 24 hours
rclone move --bwlimit 9M --tpslimit 4 --max-size 99G --log-level INFO --stats 15s local:/mnt/rclone-move gdrive:/
sleep 900
done
EOF
chmod 755 /opt/rclone-move.sh

## Create the Move Service
tee "/etc/systemd/system/move.service" > /dev/null <<EOF
[Unit]
Description=Move Service Daemon
After=multi-user.target

[Service]
Type=simple
User=root
Group=root
ExecStart=/bin/bash /opt/rclone-move.sh
TimeoutStopSec=20
KillMode=process
RemainAfterExit=yes
Restart=always

[Install]
WantedBy=multi-user.target
EOF

sudo systemctl daemon-reload

####################################### Encrypted Service
## Create the RClone Service
tee "/etc/systemd/system/rclone-en.service" > /dev/null <<EOF
[Unit]
Description=RClone Daemon
After=multi-user.target

[Service]
Type=simple
User=root
Group=root
ExecStart=/usr/bin/rclone --allow-non-empty --allow-other mount crypt: /mnt/rclone --bwlimit 8650k --size-only
TimeoutStopSec=20
KillMode=process
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target
EOF

## Enable RClone Service
sudo systemctl daemon-reload

## Create the UnionFS Service
tee "/etc/systemd/system/unionfs.service" > /dev/null <<EOF
[Unit]
Description=UnionFS Daemon
After=multi-user.target
[Service]
Type=simple
User=root
Group=root
ExecStart=/usr/bin/unionfs -o cow,allow_other,nonempty /mnt/rclone-move=RW:/mnt/rclone=RO /mnt/rclone-union
TimeoutStopSec=20
KillMode=process
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
EOF

## Enable UnionFS Service
systemctl daemon-reload
systemctl enable unionfs
systemctl start unionfs

## Create the Move Script
tee "/opt/rclone-move-en.sh" > /dev/null <<EOF
#!/bin/bash
sleep t0
while true
do
rclone move --bwlimit 9M --tpslimit 4 --max-size 99G --log-level INFO --stats 15s local:/mnt/rclone-move gcrypt:/
sleep 900
done
EOF
chmod 755 /opt/rclone-move-en.sh

## Create the Move Service
tee "/etc/systemd/system/move-en.service" > /dev/null <<EOF
[Unit]
Description=Move Service Daemon
After=multi-user.target

[Service]
Type=simple
User=root
Group=root
ExecStart=/bin/bash /opt/rclone-move-en.sh
TimeoutStopSec=20
KillMode=process
RemainAfterExit=yes
Restart=always

[Install]
WantedBy=multi-user.target
EOF

sudo systemctl daemon-reload

For the unencrypted setup,

#!/bin/bash

clear
cat << EOF
Directory 1 (For Google Drive)
WARNING: Write this down and follow the order

N < For New remote
gdrive < for the name
9 < For Google Drive (double check the number select incase)
Enter Your Google ID
Enter Your Google Secret

Y < for GUI Interface (much easier if using a Graphical Interface)
N < for headless machine (if using only Terminal)

Enter Your Verification Code

Windows Users: Use CTRL+Insert (for copy) and Shift+Insert (for Paste)
Do anything else, you will mess it up

N < Configure this as a team drive?
Y < If asking all is ok?

EOF
bash /opt/plexguide/scripts/docker-no/continue.sh

cat << EOF
Directory 2 (Local Drive)
WARNING: Write this down and follow the order

N < For New remote
local < for the name
11 < For a Local Drive

Ignore this part about ... long file names, UNC, and selecting [1])
>>> Just type this exactly: /mnt/rclone-move and then press [ENTER]

Y < Is asking all is ok?
Q < to quit

EOF

bash /opt/plexguide/scripts/docker-no/continue.sh

rclone config

# disable the encrypted services to prevent a clash
systemctl disable rclone-en
systemctl disable move-en
systemctl stop rclone-en
systemctl stop move-en

# stop current services
systemctl stop unionfs
systemctl stop rclone
systemctl stop move

# ensure that the unencrypted services are on
systemctl enable rclone
systemctl enable move

# turn services back on
systemctl start unionfs
systemctl start rclone
systemctl start move

####################################################### REPEAT 2 WORK
# disable the encrypted services to prevent a clash
systemctl disable rclone-en
systemctl disable move-en
systemctl stop rclone-en
systemctl stop move-en

# stop current services
systemctl stop unionfs
systemctl stop rclone
systemctl stop move

# copy rclone config from sudo user to root, which is the target
cp ~/.config/rclone/rclone.conf /root/.config/rclone/

# ensure that the unencrypted services are on
systemctl enable rclone
systemctl enable move

# turn services back on
systemctl start unionfs
systemctl stop rclone
systemctl start rclone
systemctl start move

clear
cat << EOF
NOTE: You installed the unencrypted version for the RClone data transport!
If you messed anything up, select [2] and run through again.  Also check:
http://unrclone.plexguide.com and or post on http://reddit.plexguide.com

HOW TO CHECK: In order to check if everything is working, have 1 item at least
in your google Drive

1. Type: /mnt/rclone (and then you should see some item from your g-drive there)
2. Type: /mnt/rclone-union (and you should see the same g-drive stuff there)

Verifying that 1 and 2 are important due to this is how your data will sync!

To make it easy, you can also use the CHECKING TOOLS built in!

EOF
bash /opt/plexguide/scripts/docker-no/continue.sh

For the encrypted setup:

#!/bin/bash

## For Google Drive
clear
cat << EOF
Maintained By - Deiteq
Directory 1 (For Google Drive)
WARNING: Write this down and follow the order or http://enrclone.plexguide.com

N < For New remote
gdrive < for the name
9 < For Google Drive (double check the number select incase)
Enter Your Google ID
Enter Your Google Secret

Y < for GUI Interface (much easier if using a Graphical Interface)
N < for headless machine (if using only Terminal)

Enter Your Verification Code

Windows Users: Use CTRL+Insert (for copy) and Shift+Insert (for Paste)
Do anything else, you will mess it up

N < Configure this as a team drive?
Y < If asking all is ok?

EOF
bash /opt/plexguide/scripts/docker-no/continue.sh

####### For Encryption Part 1
cat << EOF
Maintained By - Deiteq
Part I Encryption
WARNING: Write this down and follow the order or http://enrclone.plexguide.com

N < For New remote
gcrypt < for the name
6 < For Encrypt/Decrypt (double check the number select incase)
gdrive:/encrypt (encrypt being the rclone encrypted folder within your gdrive)
2 < Encrypt standard
Y < type your own password (write it, secure it and do not lose it)
Y < type your own salt password (write it, secure it, make different from before)
Should see something like this:

[gcrypt]
remote = gdrive:/encrypt
filename_encryption = standard
password = *** ENCRYPTED ***
password2 = *** ENCRYPTED ***

Y < Is asking all is ok?

EOF

bash /opt/plexguide/scripts/docker-no/continue.sh

####### For Encryption Part 2
Maintained By - Deiteq
cat << EOF
Part II Encryption
WARNING: Write this down and follow the order or http://enrclone.plexguide.com

N < For New remote
crypt < for the name
6 < For Encrypt/Decrypt (double check the number select incase)
/mnt/plexdrive4/encrypt
2 < Encrypt standard
Y < type your own password (use same as before for gcrypt)
Y < type your own salt password (use same as before for gcrypt salt)
Should see something like this:

[crypt]
remote = /mnt/plexdrive4/encrypt
filename_encryption = standard
password = *** ENCRYPTED ***
password2 = *** ENCRYPTED ***

Y < Is asking all is ok?

EOF

bash /opt/plexguide/scripts/docker-no/continue.sh

##### For Encryption Part II
cat << EOF
Maintained By - Deiteq
Loca Drive
WARNING: Write this down and follow the order or http://enrclone.plexguide.com

N < For New remote
local < for the name
11 < For a Local Drive

Ignore this part about ... long file names, UNC, and selecting [1])
>>> Just type this exactly: /mnt/rclone-move and then press [ENTER]

Y < Is asking all is ok?
Q < to quit

EOF

bash /opt/plexguide/scripts/docker-no/continue.sh

rclone config

# disable the unencrypted services to prevent a clash
systemctl disable rclone
systemctl disable move
systemctl stop rclone
systemctl stop move

# stop current services
systemctl stop unionfs
systemctl stop rclone-en
systemctl stop move-en

# copy rclone config from sudo user to root, which is the target
cp ~/.config/rclone/rclone.conf /root/.config/rclone/

# ensure that the encrypted services are on
systemctl enable rclone-en
systemctl enable move-en

# turn services back on
systemctl restart unionfs
systemctl restart rclone-en
systemctl restart move-en

######################### REPEATS TO MAKE IT WORK
# disable the unencrypted services to prevent a clash
systemctl disable rclone
systemctl disable move
systemctl stop rclone
systemctl stop move

# stop current services
systemctl stop unionfs
systemctl stop rclone-en
systemctl stop move-en

# copy rclone config from sudo user to root, which is the target
cp ~/.config/rclone/rclone.conf /root/.config/rclone/

# ensure that the encrypted services are on
systemctl enable rclone-en
systemctl enable move-en

# turn services back on
systemctl restart unionfs
systemctl restart rclone-en
systemctl restart move-en

clear
cat << EOF
NOTE: You installed the encrypted version for the RClone data transport! If you
messed anything up, select [2] and run through again.  Also check:
http://enrclone.plexguide.com and or post on http://reddit.plexguide.com

HOW TO CHECK: In order to check if everything is working, have 1 item at least
in your google Drive

1. Type: /mnt/rclone (and then you should see some item from your g-drive there)
2. Type: /mnt/rclone-union (and you should see the same g-drive stuff there)

Verifying that 1 and 2 are important due to this is how your data will sync!

To make this easy, you can also use the checking tools built in!

EOF
bash /opt/plexguide/scripts/docker-no/continue.sh

You can also use my script as template: https://github.com/ajkis/scripts/blob/master/rclone/rclone-upload.sh

Basically you add it to cron tab to run every minute, script will automatically exit if already running OR there is nothing for upload.

p.s. Additionally it will start upload only if files are already 15 minutes + OLD, this is to prevent moving partial files eg that are still being copied by OS ( assuming you are not instantly moving them from download client to upload )