VectorLinux
July 27, 2014, 10:01:44 pm *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Visit our home page for VL info. To search the old message board go to http://vectorlinux.com/forum1. The first VL forum is temporarily offline until we can find a host for it. Thanks for your patience.
 
Now powered by KnowledgeDex.
   Home   Help Search Login Register  
Please support VectorLinux!
Pages: [1]
  Print  
Author Topic: Backup script  (Read 1490 times)
no2thesame
Packager
Vectorite
****
Posts: 136


« on: April 25, 2009, 11:11:03 pm »

I'm a beginner with shell scripts. A friend I'm helping start on Linux, wants a simple way to to backup (synch actually) several Linux machines to an external USB hard drive. (he would physically move the external drive to the different machines, I don't know if he has them networked) I thought I would go with a shell script with a desktop link. I also want to introduce him to scripting.

Here is the script I came up with. I would appreciate someone with more experience to check it out. There must be better ways to do things, specifically:
- Is there a better way to find where the hard disk is mounted? Automatic would be even better! I had to do quite a bit of scratching around to find where HAL put the USB stick that I'm using for testing.
- Is there a better way than cd to the drive to test that the hard drive is mounted?
- rsync throws up chgrp errors when I use the -a option, it seems to work fine with -r though. Am I using rsync correctly?
- Returning to the home directory doesn't seem to have any effect, I end up where I started the script, even though I assume I changed to the drive directory earlier in the script.

Thanks
Code:
#!/bin/bash
# Name: syncext.sh
# Calls: rsync, zenity
# Purpose: Syncs preselected directory's in Dave's home directory
# with an external drive.
# Find where your external hard drive is mounted and replace the
# "/media/disk-1" below with what it is on your system.
EXT_HD="/media/disk-1"

# Setting the target directory on the external hard drive
TARGET="$EXT_HD/$HOSTNAME"

# Please fill in the directories to back up. Separate with a space.
# SOURCES="$HOME/Pictures $HOME/Documents $HOME/Music $HOME/Downloads"
SOURCES="$HOME/davetest"

# Check that the external hard drive is connected and mounted,
# and that the back up directory exists
cd $EXT_HD
EARLY_EXIT=$?
if [ $EARLY_EXIT != 0 ]; then
echo $(zenity --error --text="Can't find external hard drive!\n\nEnsure it is connected and mounted" --title="Error")
exit 1
fi

# If no target directory, makes one - for more than one machine
if [ ! -d $EXT_HD/$HOSTNAME ]; then
mkdir $EXT_HD/$HOSTNAME
fi

# Copy the differences between the directories to the external hard drive
# maintaining permissions and recursively
rsync -r $SOURCES $TARGET | zenity --progress --pulsate --auto-close --title="Synching" --text="Copying files to external hard drive...\n\nPlease wait for the operation to finish"

# Returning to prior location
cd $HOME

# Finished copying. Goodbye.
zenity --info --title "Copying finished" --text "$SOURCES are now synched."
fi
Logged
toothandnail
Tester
Vectorian
****
Posts: 2527


« Reply #1 on: April 26, 2009, 09:41:04 am »

My own backups are done in a similar fashion. I also have a couple of clients where I use an external NAS device as a backup medium. Not sure if its a better way of detecting that the device is mounted, but this is what I use to backup to the NAS:

Code:
~!/bin/bash
    /sbin/e-smith/signal-event pre-backup
mount -t cifs -o user=synback%password-123 //pandoar/gback /mnt/smb
sleep 10
if [ -d /mnt/smb/files ]; then
        /usr/bin/synbak -s goldie -m tar -M gz
sleep 20
        /sbin/e-smith/signal-event post-backup
        umount /mnt/smb; exit
else
        echo "`date +%m-%d-%H:%M` Mount failed, backup aborted" >> /var/log/synbak.log
fi

Detecting the mount that way depends on their being a directory with a known name on the mounted device. Since I want to put the backups into that directory, it should be there...

I found I needed the sleep statements because mount took some time to settle, and if I let the script proceed too fast, it would fail. I also added the 'else' statement to log any attempts that fail because the mount has failed (because some $%^$%#  in the office decided to be green and turned the NAS off...). The 'signal-event' commands are specific to SME server, first doing a dump of the MySQL databases, then cleaning up at the end of the backup.

I'm also lazy - I use synbak to do the backup itself, and I create the backup as a compressed tar file, which makes sure all file permissions are correctly stored, even though the backup is going to a file system that does not understand Linux permissions and owner/group attributes.

As to the errors you are getting from rsync, what file system are you using on the USB drive? That could have a lot to do with that sort of error.

Not sure about the failure to return to return to the user's home directory. As an alternative, cd entered without arguments should have the same effect. Have you tried that?

paul.
Logged
bigpaws
Vectorian
****
Posts: 1843


« Reply #2 on: April 26, 2009, 05:48:15 pm »

Quote
- Is there a better way to find where the hard disk is mounted? Automatic would be even better! I had to do quite a bit of scratching around to find where HAL put the USB stick that I'm using for testing.

There are couple of ways. You could set udev to name the drive since udev is dynamic. You could also use mount to
mount the drive where you want, I do understand that the disk has to be in the same position on every machine.

Quote
- Is there a better way than cd to the drive to test that the hard drive is mounted?

The variable from the mount commend could be used.
or

libsmount="/some/potential/mount/point/to/check";

mountpoint $libsmount
if [ $? -eq 0 ] ; then
echo "already mounted"
else
echo "not mounted do it now"
fi

libsmount="/some/potential/mount/point/to/check";

mountpoint $libsmount
if [ $? -eq 0 ] ; then
echo "already mounted"
else
echo "not mounted do it now"
fi

Quote
- rsync throws up chgrp errors when I use the -a option, it seems to work fine with -r though. Am I using rsync correctly?

I have never had a problem, I use rsync -avz on my backups.

HTH

Bigpaws



Logged
no2thesame
Packager
Vectorite
****
Posts: 136


« Reply #3 on: April 26, 2009, 11:20:48 pm »

Thank you , I will try your solutions out when I get time.
Logged
no2thesame
Packager
Vectorite
****
Posts: 136


« Reply #4 on: May 14, 2009, 03:26:07 pm »

FWiW here is what we ended up with:
Code:
#!/bin/bash
# Name: syncher
# Calls: rsync, zenity
# Purpose: Syncs preselected directory's in Dave's home directory
# with an external drive.
# Find where your external hard drive is mounted and replace the
# "/media/disk-1" below with what it is on your system.
EXT_HD="/media/disk-1"

# Setting the target directory on the external hard drive
TARGET="$EXT_HD/$HOSTNAME"

# Please fill in the directories to back up. Separate with a space.
# SOURCES="$HOME/Pictures $HOME/Documents $HOME/Music $HOME/Downloads"
SOURCES="$HOME/work"

# Check that the external hard drive is connected and mounted,
# and that the back up directory exists
cd $EXT_HD
EARLY_EXIT=$?
if [ $EARLY_EXIT != 0 ]; then
echo $(zenity --error --text="Can't find external hard drive!\n\nEnsure it is connected and mounted" --title="Error")
exit 1
fi

# If no target directory, makes one - for more than one machine
if [ ! -d $EXT_HD/$HOSTNAME ]; then
mkdir $EXT_HD/$HOSTNAME
fi


# Copy the differences between the directories to the external hard drive
# maintaining permissions and recursively
rsync -r $SOURCES $TARGET | zenity --progress --pulsate --auto-close --title="Synching" --text="Copying files to external hard drive...\n\nPlease wait for the operation to finish"

# Returning to prior location
cd $HOME

# Finished copying. Goodbye.
zenity --info --title "Copying finished" --text "$SOURCES are now synched."

We found the kernel device node for the USB hard drive by plugging it in and mount it then typing:

df

this outputs:
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/sda6             10329440   5368468   4960972  52% /
/dev/sda7             30626948   4742408  25884540  16% /home
tmpfs                   250736         0    250736   0% /dev/shm
/dev/sdb1              9950528      5280   9945248   1% /media/disk-1

and copying the  "/media/disk-1" (it was much more specific on Dave's machine something like "media/usbHD")

Editing 'syncher'
EXT_HD="media/usbHD"

SOURCES="$HOME/Documents"

chmod 777 syncher
su
cp syncher /usr/bin

Then making a desktop launcher with the command: syncher

References:
http://reactivated.net/writing_udev_rules.html
http://archive.atomicmpc.com.au/forums.asp?s=2&c=16&t=4775

All seemed to work.

Bruce
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2013, Simple Machines Valid XHTML 1.0! Valid CSS!