Score:1

(r)sync between Ubuntu virtual machine and windows host using a shared folder (after start and before restart/shutdown

cn flag

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

ar flag
See [this question and it's answers for possible solutions](https://askubuntu.com/questions/293312/execute-a-script-upon-logout-reboot-shutdown-in-ubuntu) how to run scripts at system start up and shutdown.
kanehekili avatar
zw flag
try `Before=umount.target` instead of `shutdown.target`
user3541631 avatar
cn flag
@kanehekili it work but only if user is root
kanehekili avatar
zw flag
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`.
Score:1
zw flag

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.

Score:0
sy flag

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.

user3541631 avatar
cn flag
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.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.