LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General > LinuxQuestions.org Member Success Stories
User Name
Password
LinuxQuestions.org Member Success Stories Just spent four hours configuring your favorite program? Just figured out a Linux problem that has been stumping you for months?
Post your Linux Success Stories here.

Notices


Reply
  Search this Thread
Old 09-27-2006, 07:07 AM   #1
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
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.
 
Old 09-27-2006, 07:22 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
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
 
Old 09-28-2006, 10:20 AM   #3
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Original Poster
Rep: Reputation: 128Reputation: 128
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
 
Old 09-28-2006, 10:40 AM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
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.

Last edited by unSpawn; 09-28-2006 at 10:42 AM.
 
Old 09-28-2006, 10:55 AM   #5
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Original Poster
Rep: Reputation: 128Reputation: 128
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.
 
Old 09-29-2006, 08:11 AM   #6
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
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.
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Using CP in my backup script. Echo Kilo Programming 6 06-01-2005 08:43 AM
backup script nitaish Linux - General 1 04-22-2005 11:31 AM
What backup script do you use? buldir Linux - Software 1 04-12-2005 02:57 PM
not that anyone cares but... here's a free decompression script i wrote versaulis Linux - Software 8 11-23-2003 02:21 PM
backup script lhoff Programming 2 05-28-2003 11:37 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General > LinuxQuestions.org Member Success Stories

All times are GMT -5. The time now is 10:40 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