I have written this bash script which runs every night to do a full backup of my system
Code:
#!/bin/sh
## BACKUP RZEPECKI-04 SYSTEM ##
## THIS SCRIPT PERFORMS A BACKUP OF THE LISTED DIRECTORIES TOO THE EXTERNAL USB HARD DRIVE
## PROCESS IS
## MOUNT THE EXTERNAL DRIVE (USB AT THE MOMENT)
## DO THE COPY OF THE DIRECTORIES
## UNMOUNT THE DRIVE
## MOUNT THE DRIVE
mount -t ext3 /dev/sda1 /mnt/backup
sleep 5
flag=$(cat /mnt/backup/flag)
echo $flag
echo
#do an if
if [ "$flag" = "A" ]; then
flag=B
echo THIS BACKUP WILL BE $flag
echo
else
#if [ "$flag" = "B" ]; then
flag=A
echo THIS BACKUP WILL BE AN $flag
echo
fi
## below list the directories to backup
echo "BACKUP /home/* TO THE EXTERNAL BACKUP"
echo
#mkdir /mnt/backup/home
#cp -auv /home/* /mnt/backup/home/
#tar -mvx /home /mnt/backup
tar -czvf /mnt/backup/home-$flag.tgz /home
#rsync -a -delete /home/* /mnt/backup/home-$flag/
echo "FOLDERS: /home BACKED UP"
echo "###############################################"
echo
## NEXT DIR ##
echo "BACKUP /etc/*"
echo
#mkdir /mnt/backup/etc
#cp -auv /etc/* /mnt/backup/etc/
tar -czvf /mnt/backup/etc-$flag.tgz /etc
#rsync -a -delete /etc/* /mnt/backup/etc-$flag
echo "FOLDERS /etc BACKED UP"
echo "###############################################"
echo
## NEXT DIR ##
echo "BACKUP /usr/local/cvsroot/*"
echo
#mkdir /mnt/backup/usr/local/cvsroot
#cp -auv /usr/local/cvsroot/* /mnt/backup/usr/local/cvsroot/
tar -czvf /mnt/backup/cvsroot-$flag.tgz /usr/local/cvsroot
#rsync -a -delete /usr/local/cvsroot/* /mnt/backup/cvsroot-$flag
echo "FOLDERS /usr/local/cvsroot BACKED UP"
echo "###############################################"
echo
## NEXT DIR ##
echo "BACKUP /public/*"
echo
#mkdir /mnt/backup/public
#cp -auv /public/* /mnt/backup/public/
tar -czvf /mnt/backup/public-$flag.tgz /public
#rsync -a -delete /public/* /mnt/backup/public-$flag
echo "FOLDERS /public/* BACKED UP"
echo "###############################################"
echo
## UPDATE THE FLAG FILE ON THE DRIVE
#echo $flag >> /mnt/backup/flag # this line will append the flag var to the file
# The below line will hopefully overwrite the contents of the flag file.
echo $flag > /mnt/backup/flag
sync
sleep 5
## Unmount the back drive
umount /mnt/backup
sleep 5
echo "BACKUP DRIVE UNMOUNTED"
echo "BACKUP OF SYSTEM COMPLETE"
echo
My question is how can I implement a method into this script that will detect or check that the external drive is actually connected.
What happens is when the power fails the backup writes to the local disk and can fill the disk. This happens on occasions that I go away and do not have access to the system.
What I would like to the is have the script automatically check the external drive is connected to the system and if something happens during the running of this backup to terminate the backup.
How do I go about implementing this into my simple backup script?