LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   LinuxQuestions.org Member Success Stories (https://www.linuxquestions.org/questions/linuxquestions-org-member-success-stories-23/)
-   -   Just a little backup script I wrote which does what I want it to (https://www.linuxquestions.org/questions/linuxquestions-org-member-success-stories-23/just-a-little-backup-script-i-wrote-which-does-what-i-want-it-to-487355/)

pwc101 09-27-2006 07:07 AM

Just a little backup script I wrote which does what I want it to
 
I bought an external USB hard drive, and thought it was about time I started backing up my data. So, with that in mind, I set about writing a little script to backup my home directory to a folder on the external disk. Here's what I got:
Code:

#! /bin/sh

# backup script to save my home directory to my external usb drive.

# set a variable of today's date
DATE=`date "+%y-%m-%d"`

# add the directories as variables, including logs.
HOME_DIR=/home/$USER
#ETC_DIR=/etc
TO_DIR=/mnt/usb/linux/current
PART_DIR=/mnt/usb/linux/partials
LOG_DIR=/mnt/usb/linux/logs
LOG_FILE=backup_$DATE.log

# check if the usb drive's mounted, and if it's not, then mount it.
if mount | grep sda>/dev/null
then echo "1">mount.info
else mount /mnt/usb>/dev/null
fi

# either copy the directory as is, no compression or tarring or anything. Only copies newer files.
#cp -aRu $HOME_DIR $TO_DIR

# or tar the whole directory and shunt that over to the usb drive
#tar cfj $TO_DIR/$DATE.tar.bz2 $HOME_DIR/* ETC_DIR/*

# or use rsync to copy only files which are newer than those on the backup
# -a is archive, -v is verbose, and -r is recursively copy files.
/usr/bin/rsync -avr --partial --partial-dir=$PART_DIR $HOME_DIR $TO_DIR > $LOG_DIR/$LOG_FILE

# then, if the mount was already mounted, leave it be, otherwise, unmount it:
# check mount.info is present, and if so, then don't unmount, if it isn't, then do.
if ls | grep mount.info>/dev/null
then echo "Mounted!">/dev/null
else umount /mnt/usb>/dev/null
fi

# then remove the mount.info file
rm -f ./mount.info

There's bit I've taken from other scripts (notably the mounting part - the unmounting bit, though inelegant, I wrote myself :)). As you can see, I tried a series of ways of copying the data over, finally settling on rsync.

I've still to set it up as a cron job, and I'd like to add a few other directories (/etc, /usr, /boot etc.), but I was quite chuffed it worked at all! Other than scripts for data processing for my thesis, this is the first useful script I've written :)

Any suggestions are welcome, but just thought I'd put it out there.

unSpawn 09-27-2006 07:22 PM

Cool.
Any suggestions are welcome
Not really a suggestion, just regard it as feedback ;-p
Code:

#! /bin/sh
# set error flag
set -e
DATE=`date "+%y-%m-%d"`
HOST=$(hostname -s)
# Dirs to back up
DIRS="/home/$USER /etc /var/something"
# Backup root
BASE=/mnt/usb/linux
TO_DIR=$BASE/current
PART_DIR=$BASE/partials
LOG_FILE=$BASE/logs/backup_$DATE.log
MOUNTED=1
# Check if mounted, do and set flag.
mount | grep -q sda. && { MOUNTED=0; mount /mnt/usb > /dev/null; }
# Handle each dir separate
for DIR in $DIRS; do
 # If the dir doesn't exist continue next one
 [ ! -d $DIR ] && { echo "No dir \"$DIR\""; break; }
 case "$1" in
  c|cp|--copy) cp -aRu $DIR $TO_DIR/${HOST}/${DIR//\//};;
  t|tar|--tar) tar cfj $TO_DIR/$HOST_$DATE_${DIR//\//}.tar.bz2 $DIR;;
  r|--rsync)  rsync -avr --partial --partial-dir=$PART_DIR $DIR \
              $TO_DIR > $LOG_FILE;;
  *) echo "${0//*\/}: [cp|tar|rsync]"; exit 1;;
 esac
done
# Umount if mounted
[ $MOUNTED -eq 0 ] && umount /mnt/usb > /dev/null

exit 0


pwc101 09-28-2006 10:20 AM

Ah, elegance! That's what I'm looking for...

I've got a few questions about what you've done, if you don't mind?

Does set -e mean that if something goes wrong, the script will cease?

Code:

case "$1" in
  c|cp|--copy) cp -aRu $DIR $TO_DIR/${HOST}/${DIR//\//};;
  t|tar|--tar) tar cfj $TO_DIR/$HOST_$DATE_${DIR//\//}.tar.bz2 $DIR;;
  r|--rsync)  rsync -avr --partial --partial-dir=$PART_DIR $DIR \
              $TO_DIR > $LOG_FILE;;
  *) echo "${0//*\/}: [cp|tar|rsync]"; exit 1;;
 esac

This bit I don't really understand. Does this run either cp, tar or rsync? If so, what's it dependent on? i.e. when does it run cp over tar, for example?

Setting a flag from the output of mount was what I'd wanted to do in the first place, but I had no idea how to do it; I suspect that will come in handy in future! Thanks :)

Might go home and have a play with new ideas...

I'm still struggling with a rename-to-lowercase script too, but that's a FAT32 thing.

Thanks again :)

unSpawn 09-28-2006 10:40 AM

Ah, elegance! That's what I'm looking for...
No. That's not what you're looking for. You're looking for something that just works. (I mean, I would)

Does set -e mean that if something goes wrong, the script will cease?
Indeed it does. For more see "man bash", under "set".

This bit I don't really understand.
Me neither. I just find I've written the scripts when I've woken up. It's rather embarassing.

Does this run either cp, tar or rsync?
Either.

If so, what's it dependent on?
When the first commandline arg matches.

when does it run cp over tar, for example?
When? Never. It *either* copies, tars or rsyncs.

I'm still struggling with a rename-to-lowercase script too, but that's a FAT32 thing.
There's Bash for POS too.
You don't need to install Cygwin for it.

pwc101 09-28-2006 10:55 AM

Quote:

Originally Posted by unSpawn
When the first commandline arg matches.

So if I run the script as ./backup_script.sh c, then it'll use cp?

Quote:

Originally Posted by pwc101
I'm still struggling with a rename-to-lowercase script too, but that's a FAT32 thing.

Sorry, should have been more clear. I'm trying to rename all the files on my external hard drive (FAT32) to lowercase from Linux (hitting shift all the time is doing my nut!), but it doesn't like it one iota. This thread's where I'm up to so far: http://www.linuxquestions.org/questi...d.php?t=487415

Thanks for the clarifications.

unSpawn 09-29-2006 08:11 AM

So if I run the script as ./backup_script.sh c, then it'll use cp?
It sez so on the package, so it should.


All times are GMT -5. The time now is 09:21 PM.