1

I'm running a raspi to backup some data to a usb flash drive. Some of the input data is located on a nas and mounted as smb-drive.

After electrical power failure (which happens rarely), I'm facing following issue:

  • Both, raspi and NAS, start directly after power-on
  • NAS needs some time (1-2 Minutes) to be reachable in the network
  • raspi is much faster and doesn't mount the NAS drive on power on

Consequently, the mountpoint remains empty and the calls to rnaspshot are saving empty data. That's not a big problem, but: As soon as I manually mount the nas by means of sudo mount -a rsnapshot saves the nas data from scratch without re-using the existing data and that eats up the memory on my pen drive.

My basic idea is to wrap the cron-calls of rsnapshot (at least the hourly ones) into a bash script which checks whether the nas has been mounted. If not, it would try to mount if mounted, it would call rsnapshot.

Is this a proper approach to my issue or could you suggest a much cleaner solution?

Thanks

8
  • You say in your question, "I manually mount the nas by means of sudo mount -a ". Does that mean that you have not created an entry in /etc/fstab to mount your NAS at boot time?
    – Seamus
    Apr 22, 2020 at 13:21
  • There is actually an entry in /etc/fstab but since the NAS is not ready in network at boot time, this doesn't mount the drive. Consequently, I've to manually do it afterwards.
    – amw
    Apr 22, 2020 at 13:50
  • 1
    Oh that shouldn't be! Could you add your /etc/fstab entry to your question? This Q&A may have the answer to your problem.
    – Seamus
    Apr 22, 2020 at 13:54
  • That's one line out of the /etc/fstab //192.168.21.22/raspi /mnt/wdmycloud/raspi cifs username=raspi,password=*****,rw,uid=1000,gid=1000,file_mode=0744,dir_mode=0744
    – amw
    Apr 22, 2020 at 14:08
  • Thanks for the link. I call rsnapshot by means of /etc/crontab with root rights, it's not a service as mentioned in the link. Do I still need to wrap the call to rsnapshot in order to wait for the mount?
    – amw
    Apr 22, 2020 at 14:12

1 Answer 1

2

As I understand your situation:

  1. You have a NAS drive mounted on your RPi. Let's assume that mount point is:

    /mnt/wdmycloud/raspi

  2. You also mount a thumb drive on your RPi. Let's assume that mount point is:

    /mnt/thumbdrv

  3. You have a script called rsnapshot that you have scheduled to run at regular intervals in a cron job. rsnapshot copies or backs up some data from the NAS drive to the Thumb drive. You wish to avoid performing copy/backup operations in rsnapshot when the NAS is not mounted as this causes issues. (I assume these issues are due to how your script is written, but that is speculation)

  4. You occasionally have power outages that affect both the RPi and the NAS. The NAS has a longer boot time, and for reasons that aren't clear to me, the NAS is not being re-mounted by systemd once it has booted; this failure requires you manually mount the NAS following a power outage.

  5. In summary then, it sounds as if you have two issues:

    • Issue 1: failure to mount the NAS after a power outage
    • Issue 2: dysfunction of rsnapshot due to NAS not being mounted

Issue 1:

The following may work to resolve Issue 1:

Schedule a re-mount of the NAS in root's crontab:

sudo crontab -e           # use root's crontab instead of running sudo in yours 

When the editor opens, add this line to root's crontab:

@reboot (sleep 120; mount -a -v >> /home/pi/yourlogfile.txt 2>&1)

This will cause cron to wait 2 minutes after it's started during the RPi boot cycle, and then mount everything in /etc/fstab. You can experiment with the sleep value if 2 minutes isn't quite right. This cron job will also send all stdout and stderr output from the mount command to the log file. If the NAS drive is not being mounted, you will see that in the stdout output in this log file. If that mount fails, you will also (hopefully) see some useful stderr output that gives clues as to the cause of failure.

Issue 2:

The following may work to resolve Issue 2:

As you have suggested in your question, it is possible to determine whether or not the NAS is properly mounted prior to copy/backup operations. One way to do that is to use findmnt in your rsnapshot script. Without seeing the rsnapshot script, something like this may work:

#!/bin/bash
...
# snippet for checking if NAS is mounted
if [[ $(findmnt -M "/mnt/wdmycloud/raspi") ]]
then
    echo "NAS Drive Mounted at /mnt/wdmycloud/raspi"
    # proceed with copy/backup operations
else
    echo "NAS Drive NOT mounted, Try Again Later"
    # wait, resolve, exit ??
fi
...

Refer to man findmnt for details on this command & its options.

As you think through this, there are several options available. For example, you could use findmnt in root's crontab (although elevated privileges are not required to use findmnt), and loop through sleep sessions until it became TRUE. Similarly, you may also wish to verify that your Thumb drive is mounted before going further in rsnapshot.

Let us know if this doesn't work & we'll try to help.

3
  • Does that sleep also block further crontab actions, in particular the hourly call of rsnapshot? If not, I'm afraid that the first rsnapshot call would happen exactly during the sleep time.
    – amw
    Apr 22, 2020 at 19:22
  • No - it doesn't block other jobs. But I think I've finally understood something in your question that I was missing earlier. I'm revising my answer now - give me a few minutes.
    – Seamus
    Apr 22, 2020 at 19:27
  • Works well bith both sultions. Thanks.
    – amw
    Apr 23, 2020 at 13:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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