0

I hope you are all having a great day. I have been tearing my hair out trying to do something I initially thought would be simple, run rclone to sync my files to Google at shutdown. I know most people suggest automatically running an rclone script with cron, but I do not work any normal hours or have my computer on all day, so a scheduled job would not work well. I have rclone running perfectly well, I can manually run backup scripts.

I am trying to do this with systemd following these instructions How to Run a Script Before Shutdown Under Systemd

After solving a few issues I am now stuck because systemd cannot find the rclone.conf file. My rclone.conf is located here:

/home/barry/.config/rclone/rclone.conf

but systemd is looking for rclone.conf under /root, so it fails. I guess I need to put some instructions in the script or unit file, but I am not sure what to do. Simple script for testing:

#!/bin/bash 
rclone sync -v --create-empty-src-dirs /$HOME/Documents Google:Documents

Unit File:

[Unit]
Description=Execute custom script before system poweroff
DefaultDependencies=no
Before=shutdown.target

[Service] Type=oneshot ExecStart=/usr/lib/systemd/system-shutdown/backup.sh TimeoutStartSec=0

[Install] WantedBy=shutdown.target

Systemctl status with error "NOTICE: Config file "/root/.config/rclone/rclone.conf" not found - using defaults"

arry@barryubuntu:~$ systemctl status execute-before-shutdown.service
× execute-before-shutdown.service - Execute custom script before system poweroff
     Loaded: loaded (/etc/systemd/system/execute-before-shutdown.service; enabled; preset: enabled)
     Active: failed (Result: exit-code) since Fri 2025-02-14 18:48:01 -05; 2min 22s ago
    Process: 10732 ExecStart=/usr/lib/systemd/system-shutdown/backup.sh (code=exited, status=1/FAILURE)
   Main PID: 10732 (code=exited, status=1/FAILURE)
        CPU: 70ms

Feb 14 18:48:01 barryubuntu systemd[1]: Starting execute-before-shutdown.service - Execute custom script before system poweroff... Feb 14 18:48:01 barryubuntu backup.sh[10733]: NOTICE: Config file "/root/.config/rclone/rclone.conf" not found - using defaults Feb 14 18:48:01 barryubuntu backup.sh[10733]: CRITICAL: Failed to create file system for "Google:/Documents": didn't find section in config file Feb 14 18:48:01 barryubuntu systemd[1]: execute-before-shutdown.service: Main process exited, code=exited, status=1/FAILURE Feb 14 18:48:01 barryubuntu systemd[1]: execute-before-shutdown.service: Failed with result 'exit-code'. Feb 14 18:48:01 barryubuntu systemd[1]: Failed to start execute-before-shutdown.service - Execute custom script before system poweroff.

Ok, if there is a better way to do this than with systemd, I will be happy to follow it! Thanks

2 Answers2

0

The file, which could be /usr/local/bin/rclone_shutdown.sh, might contain something like:

#!/bin/bash
/usr/bin/rclone sync /path/local remote:/path/remote 

The one who had to be given execution permissions

sudo chmod +x /usr/local/bin/rclone_shutdown.sh

Now it is necessary to create a service file for systemd, /etc/systemd/system/rclone-shutdown.service, that has a content similar to:

[Unit]
Description=Sync files to Google Drive before shutdown
DefaultDependencies=no
Before=poweroff.target halt.target reboot.target

[Service] Type=oneshot ExecStart=/usr/local/bin/rclone_shutdown.sh RemainAfterExit=true

[Install] WantedBy=halt.target reboot.target poweroff.target

And the service would have to be enabled.

sudo systemctl daemon-reload
sudo systemctl enable rclone-shutdown.service
kyodake
  • 17,808
0

I made two changes. I found the rclone flag for designating where the rclone.conf is, since under systemd it was not finding it. I added --config path to the script.

and then in the script changed the path from $HOME/Documents to the full path /home/myname/Documents

Now its working.