Issue
What I'm trying to do - In the end, I want a script that checks for 3 devices, an SD card, Backup1 & Backup2. They've all been set to auto-mount at their respective mountpoints. The script should check for SD card first, if this fails, then a warning should be sent, and nothing more. If SD is okay, but only one backup is mounted, then ask for confirmation to go ahead with rsync to mounted backup. If all devices are mounted, then rsync from SD card to both backups.
Currently I'm just trying to get the device check nailed, using echo commands. Here's what I have (after multiple attempts) -
if ! mount | grep /media/card >/dev/null
then
echo "ERROR: SD card not mounted, aborting"
else
if ! mount | grep /media/backup >/dev/null
then
if ! mount | grep /media/backup2 >/dev/null
then
echo "ERROR: No backup devices"
else
echo "CAUTION: Backup_2 missing, Backup_1 OKAY"
fi
else
if ! mount | grep /media/backup2 /dev/null
then
echo "CAUTION: Backup_1 missing, Backup_2 OKAY"
else
echo "SUCCESS: All devices OKAY"
fi
fi
fi
Which isn't working. I'm getting confused by all the nested 'if's and surely there's a simpler way? Perhaps something that checks each device independently, then returns values that equate to the various combinations (0=no devices, 1=sd only, 2=sd & backup1, 3=sd & backup 2, 4 = all okay) which then is read and decides next part of script to run? If not, where has this way gone wrong?
Any help is appreciated
Solution
Solved! Part of the problem was using mount | grep /media/backup > /dev/null
as there's an issue with similar names, so /media/backup2 being mounted meant it was counting both devices as mounted.
Using grep -q /media/backup2\ /proc/mounts
works in a similar way, or at least produces the same outcome, just without the naming issue.
I've also changed the way the if statements work, it now checks for SD card, if that fails, then it aborts. It then checks for which devices are mounted with a fairly simple flow.
Here's the code:
GNU nano 2.2.6 File: backupSD2.sh Modified
#!/bin/bash
#Check for SD
if ! grep -q /media/card\ /proc/mounts
then
echo "ERROR: NO SD CARD"
exit 0
else
echo "SD OKAY..."
fi
#Check for Backup Devices
if (( grep -q /media/backup\ /proc/mounts ) && ( grep -q /media/backup2\ /proc/mounts ))
then
echo "Both Backups mounted"
elif grep -q /media/backup\ /proc/mounts
then
echo "CAUTION: Backup_2 missing, Backup_1 OKAY"
elif grep -q /media/backup2\ /proc/mounts
then
echo "CAUTION: Backup_1 missing, Backup_2 OKAY"
else
echo "ERROR: NO BACKUP DEVICES"
exit 0
fi
After the 'echo' commands is where I'll be putting the rsync commands.
The final exit 0
is probably unnecessary, but I don't think it's doing any harm and reassures that it's not going to try any more commands.
Answered By - Airgiraffe Answer Checked By - Cary Denson (WPSolving Admin)