My Backup Script needs a tweak :)

If none of the more specific forums is the right place to ask

My Backup Script needs a tweak :)

Postby RWIndiana » 2019-10-11 19:31

Okay guys, I'm far from pro, so I guess y'all can make fun of me if you want! But I've been working on a script today to back up drive 1 to drive 2, checking to make sure both disks are mount properly first:

Code: Select all
if mount | grep $(blkid -U 'UUID1') | grep '/mnt/disk1' > /dev/null 2>&1 ; then
 if mount | grep $(blkid -U 'UUID2') | grep '/mnt/disk2' > /dev/null 2>&1  ;

then

                 rsync -aAHXxv --delete /mnt/disk1/ /mnt/disk2/;

                 echo "sync complete"
        else

    echo "one of your filesystems is not mounted"

    exit

        fi

fi


It works fine, but doesn't display any message when "disk1" is offline. I'm not exactly sure what's happening.

Thanks for any pointers. :)
RWIndiana
 
Posts: 136
Joined: 2005-09-30 17:44

Re: My Backup Script needs a tweak :)

Postby Bloom » 2019-10-11 21:48

blkid is a root command, so you would need sudo to make it work.

Your logic is a bit flawed:

Code: Select all
if drive1 then
  if drive2 then
    sync drive1 and drive2
  else
   show error
  fi
fi

Your error message is only shown if drive2 is not there.

So I suggest this:
Code: Select all
Drive1="/mnt/disk1"
Drive2="/mnt/disk2"
Mount1=$(grep "$Drive1" /etc/mtab >/dev/null 2>&1)
Mount2=$(grep "$Drive2" /etc/mtab >/dev/null 2>&1)
if [ -n "$Mount1" ] && [ -n "$Mount2" ]; then
  rsync -aAHXxv --delete "$Drive1" "$Drive2"
  echo "sync complete"
else
  echo "one of your filesystems is not mounted"
fi
User avatar
Bloom
 
Posts: 193
Joined: 2017-11-11 12:23

Re: My Backup Script needs a tweak :)

Postby Dai_trying » 2019-10-11 22:01

I have a very similar solution.

Code: Select all
#!/bin/bash

mountpoint -q /mnt/disk1
disk1=$?
mountpoint -q /mnt/disk2
disk2=$?

if [[ $disk1 -eq 0 ]] && [[ $disk2 -eq 0 ]]; then
    rsync -aAHXxv --delete /mnt/disk1/ /mnt/disk2/
    echo "sync complete"
else
    echo "one of your filesystems is not mounted"
fi
Dai_trying
 
Posts: 800
Joined: 2016-01-07 12:25

Re: My Backup Script needs a tweak :)

Postby RWIndiana » 2019-10-12 02:16

Thanks very much guys! Got it working satisfactorily. More importantly, learned a bit more about Bash.
But for some reason I couldn't get the first suggestion working. Odd. Second one did work though.
RWIndiana
 
Posts: 136
Joined: 2005-09-30 17:44


Return to General Questions

Who is online

Users browsing this forum: No registered users and 6 guests

fashionable