2

I have a small raspberry Pi that I use as a NAS, mount with fstab, and back up to my desktop with rsync.

But, sometimes the raspberry pi doesn't mount, and rsync deletes the whole backup folder.

Is there any way to tell rsync (or grsync) not to start the backup if the raspberry is not mounted?

alebal
  • 453
  • 2
  • 9
  • 20
  • You can check with `mountpoint /path/to/mountpoint/` in a shellscript and let it either try again to mount the backup partition or stop with a message to you, that you must mount it. This shellscript can be the same one that is doing the backup. – sudodus Jun 17 '22 at 18:26

1 Answers1

4

Mounted filesytems should be visible to the mount command, and you can use that to test for the existence of the mount before running rsync:

mount | awk '{if ($3 == "/path/to/mount") { exit 0}} ENDFILE {exit -1}' && rsync ...

mount runs the mount command, which will display any mounted filesystems. That output is then piped (|) through to awk which looks for the third ($3) argument to be equal to the path you're looking for (/path/to/mount in this example). If the path matches then the command returns success (exit 0) otherwise it'll exit with failure (exit -1). The && will require that the mount command returns successfully before executing the part after. Any other return code is considered a failure, so exit 10, for example, would also be interpreted as a failure.

Fnyar
  • 41
  • 2