LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 04-07-2006, 06:11 AM   #1
lionslair
LQ Newbie
 
Registered: Sep 2005
Location: Australia
Distribution: Debian
Posts: 12

Rep: Reputation: 0
custom backup


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?
 
Old 04-07-2006, 08:55 AM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,454

Rep: Reputation: 448Reputation: 448Reputation: 448Reputation: 448Reputation: 448
Hi

Seems you have a file called "flag" on the disk. Why not simply check if that file is missing?

Something like:

Code:
if [ ! -f /mnt/backup/flag ]
then
  echo "Failed mounting backup disk - no backup done!"
  exit 1
fi
Best Regards,
Guttorm.
 
Old 04-07-2006, 06:10 PM   #3
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,908

Rep: Reputation: 356Reputation: 356Reputation: 356Reputation: 356
There is another possibility that might easily be incorporated into a bash script.

When bash executes a command successfully, the exit code is 0. Any failure results in an exit code other than 0.

Try this test. Run a command you know will succeed, such as copying a file. Immediately after the copy command, run 'echo $?'. The return code is 0. Run a command you know will fail. 'echo $?' return a code other than 0.

So, before starting the backup, try to mount the device that is to receive the backup. If mount fails (device is already mounted), the exit code is not 0. If $? = 0, then abort, else do backup.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
custom install cd base-config custom mithereal Debian 6 09-11-2005 03:48 PM
can I backup a root disk and boot from the new backup disk linuxbkp Linux - Enterprise 3 10-15-2004 06:42 PM
custom geforce video card--custom module? bandofmercy Linux - Hardware 3 10-14-2004 06:52 PM
Selective backup to CDRW, or other backup methods. trekk Linux - Software 1 11-03-2003 02:46 PM
backup and update the backup file doris Linux - General 4 08-24-2002 07:26 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 04:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration