linux - Rsync script does not continue after sync

Problem: I have a simple custom backup script that is set to run whenever my backup drive is detected, this is done via udev. All is well until about halfway down through the script it seems to hang after the rsync command. My code is below:

#!/bin/bash

#Mount the Backup Drive
wall "backup is starting"
mount -U f91b8373-6349-4de3-86e1-6a2557f2c3f7 /media/backupdrive

#Get updated package-list
mv /media/backupdrive/package-selections /media/backupdrive/package-selections.old
dpkg --get-selections >/media/backupdrive/package-selections
wall "pacakge list updated"

#Run Backup
mv /home/user/backup/rsync.log /home/user/backup/rsync.log.old
rsync --log-file=/media/backupdrive/backup/rsync.log -ravzX --delete --exclude /var/tmp --exclude /var/lock --exclude /var/run /home /etc /var /usr /media/backupdrive/backup
wall "rsync complete"

#Sync changes to disk and unmount
sync
cp /media/backupdrive/backup/rsync.log /home/user/backup/rsync.log
umount /media/backupdrive
wall "Backup is complete, the logfile can be viewed at /home/user/backup/rsync.log"

Question: What am I doing wrong here, why is the script not continuing after the rsync?

PS - The wall commands are not important to the script I placed them in at various points to troubleshoot, yes I'm new to this :)

Edit - I have tried removing the "z" option as was mentioned on a similar question, however this has made no difference

1 Answer

  1. Leo- Reply

    2019-11-14

    It's like a timeout of RUN command in udev. Instead of running backup script (which normally takes long time to complete) directly from udev, you can run it from separate process, activated by udev.

    For example you can use at command:

    ACTION=="add", KERNEL=="sd*", ENV{ID_FS_UUID_ENC}=="f91b8373-6349-4de3-86e1-6a2557f2c3f7", RUN+="/home/steve/backup/backup_at.sh"
    

    backup_at.sh:

    #!/bin/sh
    echo /home/steve/backup/backup.sh | at now
    

    Or you can try to run it in background:

    ACTION=="add", KERNEL=="sd*", ENV{ID_FS_UUID_ENC}=="f91b8373-6349-4de3-86e1-6a2557f2c3f7", RUN+="/home/steve/backup/backup.sh &"
    

    but I don't check this method.

    From http://lists.freedesktop.org/archives/systemd-devel/2012-November/007390.html:

    It's completely wrong to launch any long running task from a udev rule and you should expect that it will be killed. If you need to launch a process from a udev rule, use ENV{SYSTEMD_WANTS} to activate a service.

Leave a Reply

Your email address will not be published. Required fields are marked *

You can use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>