Automatic Backup Script Help

2012-05-23 Thread Craig A. Adams

Hi,

I am trying to setup a basic backup script for a Debian 6.0.5 server and 
am wanting to validate the scripts I have so far.


Here is the scenario...

The server has 2 x 1Tb hard drives in RAID 1 config. A third 1Tb hard 
drive is used for scheduled rsync snapshots.


I also have 5 x USB 3.0 1Tb hard drives for taking backups off site.

The aim is to plug one of the external hard drives and rsync the 
snapshot backup to it automatically.


Using udev rules I now have the hard drives being recognised on plug in 
and udev firing off a test script.


As it is not a good idea to block up udev by running the backup script 
from the udev rule, I need a staging script to start the backup, but not 
immediately.


The backup script is intended to be run 1 minute after the external hard 
drive is plugged in, and this is what I have come up with:


Staging Script
==

/data/backups/scripts/start-disk-1.sh
#!/bin/bash
echo /data/backups/scripts/backup-disk-1.sh | at now + 1 minute

Is this the correct way to call the backup script, or is there a better way?

Backup Script
=

Now the backup script needs to do certain things.

1) Mount the hard drive and fail if necessary.
2) Carry out the rsync to the external hard drive.
3) Unmount the external hard drive.
4) Generate a log of the backup process.
4) Send an email with the log as the message body.

I have not figured out how to generate the log.
I am not sure of my script syntax including  vs ' vs ` delimiters.

The actual backup script I have come up with is as follows... (I hope 
email line wrapping doesn't ruin the script layout.)


/data/backups/scripts/backup-disk-1.sh
#!/bin/bash
# Backup to Disk 1

# Set Variables for Script

backup_description=Disk-1
backup_source='/data/snapshots/'
backup_target='/data/backups/disk-1'
start_date_time='date +%Y-%m-%d-%H:%M:%S'
from_addr='x...@abc.com'
to_addr='a...@xyz.com'
cc_addr='d...@uvw.com
subject='${backup_description} backup at ${current_date_time}'
smtp_server='smtp.somewhere.com'
user_name='x...@abc.com'
mail_pwd='password'
backup_log_file='/data/backup/logs/${backup_description}-${start_date_time}.log'

# Check if another backup is already in progress. If not, create a 
progress file, else fail.


if -a /data/backups/${backup_description}-in-progress; then
echo FAIL ${backup_description} backup already in progress.
exit 1
else touch /data/backups/${backup_description}-in-progress
fi

# Start disk mount process

echo ${backup_description} Automatic Backup Starting at ${start_date_time}

# Check and mount hard drive if not already mounted.

if ! mountpoint -q ${backup_target}/; then
echo Mounting the external hard drive.
echo External hard drive mounted at ${backup_target}
if ! mount ${backup_target}; then
echo FAILURE! An error was returned during mounting of hard 
drive.
rm /data/backups/${backup_description}-in-progress
exit 1
else echo External hard drive mounted successfully.;
fi
else echo ${backup_target} is already mounted, but should not be. Check 
logs!;

fi

# If hard drive is still not mounted, exit ungracefully

if ! mountpoint -q ${backup_target}/; then
echo FAILURE! Mounting of external hard drive failed!
rm /data/backups/${backup_description}-in-progress
exit 1
fi

# Start actual rsync backup

echo Start of rsync backup.
sudo rsync --archive --verbose --human-readable --itemize-changes 
--progress --delete ${backup_source} ${backup_target}/


# Remove backup in progress file

rm /data/backups/${backup_description}-in-progress

# Unmount external hard drive

umount ${backup_target}

# Set backup finish time

finish_date_time='date +%Y-%m-%d-%H:%M:%S'

# End of backup message

echo ${backup_description} Automatic Backup finished at 
${finish_date_time}.


# Send eMail Log of backup

sendemail -f ${from_addr} -t ${to_addr} -u ${subject} -m 
${backup_log_file} -s ${smtp_server} -cc ${cc_addr} -xu ${user_name} -xp 
${password}


Any help with this would be gratefully received.

Kindest Regards

Craig A. Adams


--
To UNSUBSCRIBE, email to debian-user-requ...@lists.debian.org 
with a subject of unsubscribe. Trouble? Contact listmas...@lists.debian.org

Archive: http://lists.debian.org/4fbc8405.3020...@iafrica.com



Re: Automatic Backup Script Help

2012-05-23 Thread Jon Dowland
On Wed, May 23, 2012 at 08:30:29AM +0200, Craig A. Adams wrote:
 /data/backups/scripts/start-disk-1.sh
 #!/bin/bash
 echo /data/backups/scripts/backup-disk-1.sh | at now + 1 minute
 
 Is this the correct way to call the backup script, or is there a better way?

It doesn't seem like a bad idea; especially since you get some extras from at
(e.g. mail error output to user) for free. Another approach would be

  { sleep 60; /data/backups/scripts/backup-disk-1.sh } 

But I don't see why you even need the sleep.

 start_date_time='date +%Y-%m-%d-%H:%M:%S'

should be backticks here so that date is evaluated e.g.

start_date_time=`date +%Y-%m-%d-%H:%M:%S`

 if -a /data/backups/${backup_description}-in-progress; then
   echo FAIL ${backup_description} backup already in progress.
   exit 1
 else touch /data/backups/${backup_description}-in-progress
 fi

There is a risk of race conditions here, if two instances of your script
are fired off at similar times. E.g., whilst instance #1 has performed
the check, instance #2 creates the file…  Better would be a proper locking
solution. (sorry I'm not offering one in this reply)

 if ! mountpoint -q ${backup_target}/; then

risk of races here

   if ! mount ${backup_target}; then

and here



-- 
Jon Dowland


-- 
To UNSUBSCRIBE, email to debian-user-requ...@lists.debian.org 
with a subject of unsubscribe. Trouble? Contact listmas...@lists.debian.org
Archive: http://lists.debian.org/20120523071200.GA28792@debian



Re: Automatic Backup Script Help

2012-05-23 Thread Craig A. Adams

Thank you for the feedback Jon. :-)

On 23/05/2012 09:12 AM, Jon Dowland wrote:

   { sleep 60; /data/backups/scripts/backup-disk-1.sh }

But I don't see why you even need the sleep.


I agree. I suspect using sleep will merely suspend udev operations as well.


should be backticks here so that date is evaluated e.g.

start_date_time=`date +%Y-%m-%d-%H:%M:%S`


Thank you. Not being an experiences bash scripter, I always seem to get 
lost on the proper delimeters.





if -a /data/backups/${backup_description}-in-progress; then
echo FAIL ${backup_description} backup already in progress.
exit 1
else touch /data/backups/${backup_description}-in-progress
fi


There is a risk of race conditions here, if two instances of your script
are fired off at similar times. E.g., whilst instance #1 has performed
the check, instance #2 creates the file…  Better would be a proper locking
solution. (sorry I'm not offering one in this reply)


That is what I am trying to achieve by the progress file. I have noticed 
that sometimes with usb, the device is discovered multiple times. So I 
want to prevent multiple instances of the script. I will obviously need 
to find a better locking mechanism.





if ! mountpoint -q ${backup_target}/; then


risk of races here


if ! mount ${backup_target}; then


and here



I am lost here. I thought that this would be a simple mountpoint check. 
Is there a better way to mount and check if the drive is mounted?


Kindest Regards

Craig A. Adams



--
To UNSUBSCRIBE, email to debian-user-requ...@lists.debian.org 
with a subject of unsubscribe. Trouble? Contact listmas...@lists.debian.org

Archive: http://lists.debian.org/4fbc92fd.9000...@iafrica.com



Re: Automatic Backup Script Help

2012-05-23 Thread Kushal Kumaran
On Wed, May 23, 2012 at 1:04 PM, Craig A. Adams crai...@iafrica.com wrote:
 Thank you for the feedback Jon. :-)


 On 23/05/2012 09:12 AM, Jon Dowland wrote:

   { sleep 60; /data/backups/scripts/backup-disk-1.sh }

 But I don't see why you even need the sleep.


 I agree. I suspect using sleep will merely suspend udev operations as well.


 should be backticks here so that date is evaluated e.g.

 start_date_time=`date +%Y-%m-%d-%H:%M:%S`


 Thank you. Not being an experiences bash scripter, I always seem to get lost
 on the proper delimeters.



 if -a /data/backups/${backup_description}-in-progress; then
        echo FAIL ${backup_description} backup already in progress.
        exit 1
 else touch /data/backups/${backup_description}-in-progress
 fi


 There is a risk of race conditions here, if two instances of your script
 are fired off at similar times. E.g., whilst instance #1 has performed
 the check, instance #2 creates the file…  Better would be a proper locking
 solution. (sorry I'm not offering one in this reply)


 That is what I am trying to achieve by the progress file. I have noticed
 that sometimes with usb, the device is discovered multiple times. So I
 want to prevent multiple instances of the script. I will obviously need to
 find a better locking mechanism.


flock from the util-linux package:

FLOCK(1)

NAME
   flock - manage locks from shell scripts

SYNOPSIS
   flock [-sxon] [-w timeout] lockfile [-c] command...

   flock [-sxon] [-w timeout] lockdir [-c] command...

   flock [-sxun] [-w timeout] fd



 if ! mountpoint -q ${backup_target}/; then


 risk of races here

        if ! mount ${backup_target}; then


 and here


 I am lost here. I thought that this would be a simple mountpoint check. Is
 there a better way to mount and check if the drive is mounted?



-- 
regards,
kushal


--
To UNSUBSCRIBE, email to debian-user-requ...@lists.debian.org
with a subject of unsubscribe. Trouble? Contact listmas...@lists.debian.org
Archive: 
http://lists.debian.org/CAH8GtdNnz5Lsh8LeLkvFY=ox55mhf6dr2v3_stea6+ed-rf...@mail.gmail.com