0

I have a bash script to backup my raspberry pi, and after running the backup command my pc is supposed to powerdown. This works when I just run the script via the command line. But when cronie executes it, it immediately jumps to the shutdown command instead of waiting for the backup to finish.

What can I do about that?

Shell script:

#!/bin/bash

ssh root@192.168.x.x dd if=/dev/mmvblk0 bs=1M | gzip -c > /path/to/backup/$(date -I) && systemctl poweroff
8
  • 3
    Are you usually authenticating your SSH connections against an SSH Agent? This would not work in cron or cronie.
    – Kusalananda
    Apr 15, 2018 at 13:16
  • 2
    How confident are you that you actually get useful backups? ISTM that you're copying an image from a live system, so there's no guarantee whatsoever that your backup is valid.
    – roaima
    Apr 15, 2018 at 13:32
  • @Kusalananda I'm not yet using an ssh-agent, did not know that this won't work. What about using cron to start a systemd service linked to my bash script? Apr 15, 2018 at 13:40
  • @roaima I was not aware of that, maybe I should look into other methods. Apr 15, 2018 at 13:40
  • rsnapshot is one method I use - at work and at home. duplicity is another.
    – roaima
    Apr 15, 2018 at 14:04

1 Answer 1

0

It just seemed that ssh was running in the background. Actually ssh could not connect and exited at once which made gzip exit immediately, too. But because the exit code of a pipeline is the exit code of the last command (unless set -o pipefail is used before) the shutdown command was executed.

If you want to use an SSH key which is not in the default files for the respective user then you need to tell ssh about the key file with the -i option:

ssh -i /home/user/.ssh/id_rsa root@192.168.x.x dd if=/dev/mmvblk0 bs=1M
2
  • I think I'm going to use rsync as it deals better with a live system. This way I can backup my PC and my Pi. Apr 15, 2018 at 18:11
  • @TheDutchman Same problem with rsync as it uses ssh. However, as it seems that your problem has been solved you should consider accepting my answer so that this question does not appear as unanswered any more. Apr 15, 2018 at 18:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.