1

The host machine is windows. I have an Ubuntu virtual machine(vmware). There is a shared folder between Ubuntu and Windows. The shared folder is mounted in /etc/fstab:

vmhgfs-fuse                  /shared    fuse    defaults,allow_other,nofail

When Ubuntu virtual machine start(after)/restart(before)/shutdown(before), if shared is mounted I want to sync(rsync) certain directories from Ubuntu in the shared folder.

Example:

/work/alpha to be rsync in /shared/alpha

I tried the code below and variations but is not working:

file: rsync_before_shutdown.sh

#!/bin/bash
rsync -r /workspace/projects/ /shared
[Unit]
Description=Run before shutdown
DefaultDependencies=no
Before=shutdown.target reboot.target halt.target

[Service]
Type=oneshot
RemainAfterExit=true 
ExecStart=/bin/true
ExecStop=/bin/bash /workspace/projects/rsync_before_shutdown.sh/rsync_before_shutdown.sh
TimeoutStartSec=0

[Install]
WantedBy=multi-user.target 

If I'm checking journalctl after reboot:

run_before_shutdown.service: Failed with result 'exit-code'.
Failed to start Run before shutdown.

I presume that the reason can be the directory is unmounted when the script tries to run.

So, I changed the target from shutdown.target to unmount.target. ?It is working only if the reboot is done by root. The permission on the service is 644 and on the script rwxr-xr-x

4
  • See this question and it's answers for possible solutions how to run scripts at system start up and shutdown.
    – user68186
    Dec 25, 2022 at 7:32
  • 1
    try Before=umount.target instead of shutdown.target
    – kanehekili
    Dec 25, 2022 at 19:49
  • @kanehekili it work but only if user is root Dec 27, 2022 at 9:24
  • Yes, that is to be expected. The services run as root. On a GUI you could shut down via Menu, so how do you shutDown? So your script could be either moved to root or you change user & group: chown root:root /path/to/your/rsync_before_shutdown.sh.
    – kanehekili
    Dec 28, 2022 at 18:22

2 Answers 2

1
+50

I tried the following in a VBox with Ubuntu 22.04 running.

Create a systemd service in /etc/systemd/system/actionPreShutDown.service:

[Unit]
Description=Run before shutdown
#DefaultDependencies=no
After=local-fs.target

[Service]
Type=oneshot
RemainAfterExit=true 
ExecStart=/bin/true
ExecStop=/bin/bash /usr/local/bin/sync_before_shutdown.sh
TimeoutStartSec=0

[Install]
WantedBy=multi-user.target

Create the testfile /usr/local/bin/sync_before_shutdown.sh

Mine looked like this:

#!/bin/bash
timestamp=$(date '+%n---%a %b %e %H:%M:%S %Z %Y---%n')
SOURCE=`dirname -- "$0"`;
echo $SOURCE
echo "Shutting down now: '$timestamp'" >>$SOURCE/log.txt

so it basically wrote a log (into the still mounted filesystem) on "poweroff".

The "Poweroff" was triggered via the gnome menu and via command line.

Why /usr/local/bin?

The code to be executed must belong to the system, not the user. Since I consider "backups" an admin task and the Linux Filesystem Hierarchy Standard writes:

The /usr/local hierarchy is for use by the system administrator when installing software locally. It needs to be safe from being overwritten when the system software is updated.

The bash will be in your path (i.e. you can call it from the console), but can only be executed as root.

But my final answer is, that it might not work stable. Poettering himself could not answer the question...

The main problem seems to be that there is no way to execute code before umount, since systemd runs parallel and shutdown and umount are triggered independently of each other. So I focused on available mounts and targets.

The line After=local-fs.target enables the service after that target is bound, and therefore will end it before that target will be removed/stopped.

As an alternative you might setup your mount points from where you want to backup and proceed like this. Since I spend a lot of time on this issue I'd appreciate an update if you found another solution.

0

I just recently used Rsync to backup files from my old 16.04 PC to another PC with 22.04 using rsync -av -e ssh old_user@old_laptop:/home/old_user/ /home/new_user but you need to have openssh-server installed on the PC your coming from.

To use Rsync on Windows you may need the Windows Subsystem for Linux or WSL for short installed or Git for Windows which will let you call Rsync with git bash.

Frankly I think this is all unnecessary as there are programs out there already that allow you to setup real-time sync changing files from source directory to target directory in real time.

Perhaps look at Syncthing and SyncTrayzor.

1
  • it is a virtual machine an there is mounted a shared folder between the Ubuntu vm and Windows host, so the Ubuntu sees as a normal folder in Linux. No ssh needed or WSL. I don't need to be in real time but when the vm is shutdown/restarted. Dec 27, 2022 at 8:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .