Welcome to Linux Forums! With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.
Find the answer to your Linux question:
New to Linux Forums? Register here for free!
    Linux Forums > GNU Linux Zone > Linux Newbie > Home directory backup script for newbies
 Linux Newbie   If you're new to the wonderful world of Linux, start here!

Site Navigation
Linux Articles
Linux Forums
Linux Downloads
Linux Hosting
Free Magazines
Job Board
IRC Chat
Linux Forum Topics
Linux Forums
Your Distro
Linux Resources
GNU Linux Zone
The Community
Reply
 
Thread Tools Display Modes
Old 04-11-2006   #1 (permalink)
Just Joined!
 
watchman's Avatar
 
Join Date: Apr 2006
Location: Maine, USA
Posts: 10
Lightbulb Home directory backup script for newbies

As a linux newbie I set up a samba server in my home (I have 7 kids and a wife who use computers too and we have a couple dozen of the dang things networked here). I wanted a good backup script to backup all the home directories, and not being much of a programmer I searched the web for a good way to do it. Unfortunately all the scripts I found did not quite suit my needs, specifically:

1. I wanted an email reporting the status of the backup job
2. I wanted to backup each users home directory into a seperate easily restored and compressed file. I didn't want to backup the whole /home directory as the archive size would be very large and unwieldly.
3. I wanted several days worth of backups
4. I wanted each archive file to include a date/time in the filename.
5. I didn't want to have to modify the script simply because a new user was added or one was removed

So I wrote the following script, made it executable and added it to cron. It may not be perfect, or perfect in every setup, but it works great for me. I hope this helps another newbie who is trying to come up with a decent "poor man's" backup script.

#!/bin/bash
# A Simple (Poor Man) disk based backup script
# Will backup all user home directories in seperate archives
# as a single /home backup file can be quite large and unwieldly.
# Does not need to be modified if users are added/deleted

# Step - 1 Create Timestamp
# BUDTSTAMP = Backup Date/Time Stamp

BUTDSTAMP=$(date +%Y%m%d)

# Step - 2 Start Email Message To Be Sent

# Remove mail message from previous backup
# I do this at the beginning of the script instead of the end
# in case the mail does not send for whatever reason or
# I need to debug it

rm mail.txt
echo System Backup $BUTDSTAMP >> mail.txt

# The email sends a user friendly note showing the start and
# end time/dates. This is important so you can compare logs
# and see if a backup ran ok.

echo Backup Began $(date) >> mail.txt

# Step - 3 Rotate Backups
# Simple three backupset rotation, keeps only last three
# Use directory /vol/backup as an example, make sure you change this path
# to fit your local settings. Have three directories in /vol/backup named daily,
# yesterday and daybefore.
# Make sure /vol/backup is on a different disk (preferably a different machine)
# than the files you are backing up

rm /vol/backup/daybefore/*
mv /vol/backup/yesterday/* /vol/backup/daybefore/
mv /vol/backup/daily/* /vol/backup/yesterday/

# Step - 4 Archive Home Directories
# Creates a seperate tar file for each directory in the home directory
# Remember that root's home does not usually reside in /home so add it
# and any other directories you want to back up, i.e.:
# tar -czvf /vol/backup/daily/root-$BUTDSTAMP.tar.gz /root
# tar -czvf /vol/backup/daily/itunesmusic-$BUTDTAMP.tar.gz /share/music

cd /home
for FOLDERNAME in *
do

# Archives are created in the format someuser-datetime.tar.gz

tar -czvf /vol/backup/daily/$FOLDERNAME-$BUTDSTAMP.tar.gz /home/$FOLDERNAME
done

# Step - 5 Finish Email Report and Send

echo Backup ended $(date) >> mail.txt

# df -h includes a human readable disk usage report of the media that /vol/backup
# is mounted on. Good to now if your backup disk is running out of space.
# Of course /dev/hdb1 is the device I use, modify it for your local settings

df -h /dev/hdb1 >> mail.txt
mail somebody@somedomain.com -s "Backup Job Report" -v < mail.txt

# Step - 6 All Done
watchman is offline  

Reply With Quote
Old 04-11-2006   #2 (permalink)
Linux Newbie
 
jpalfree's Avatar
 
Join Date: Jul 2005
Location: Montreal, CA
Posts: 198
nice idea. thanks for posting it.
I've tried to make your script a bit more general and safe (not that it was dangerous to begin with, i'm just a paranoid person)
Check it out. It now can backup multiple directories by simply adding them to a list.
Quote:
#!/bin/bash
# A Simple (Poor Man) disk based backup script
# Will backup all user home directories in seperate archives
# as a single /home backup file can be quite large and unwieldly.
# Does not need to be modified if users are added/deleted

# Step - 1 Create Timestamp and set up variables and functions
# BUDTSTAMP = Backup Date/Time Stamp
BUTDSTAMP=$(date +%Y%m%d)

# variable holding directories containing files to backup eg: BACKUPTHESE="/home /root /etc"
BACKUPTHESE="/home"

#directory containing today's backup
BKUPDIR="/vol/backup/daily"
#directory containing yesterday's backup
YDBKUPDIR="/vol/backup/yesterday"
#directory containing the day before yesterday's backup
DBBKUPDIR="/vol/backup/daybefore"

# log files
LOG="mail.txt"
ERRORLOG="ERROR.txt"

# tag for backup file name
TAG="homebackup"

# function that quits and logs on error
exiterror()
{
# use this function by supplying $LINENO as first arg
echo "Fatal error caused by line ${1} of ${0}" >> $ERRORLOG
mail somebody@somedomain.com -s "Backup Job ERROR" -v < $ERRORLOG
mail somebody@somedomain.com -s "Backup Job Report" -v < $LOG
exit 1
}

# Step - 2 Start Email Message To Be Sent

# Remove mail message from previous backup
# I do this at the beginning of the script instead of the end
# in case the mail does not send for whatever reason or
# I need to debug it

rm $LOG > /dev/null 2>&1
echo "System Backup $BUTDSTAMP" >> $LOG

# The email sends a user friendly note showing the start and
# end time/dates. This is important so you can compare logs
# and see if a backup ran ok.

echo "Backup Began $(date)" >> $LOG

# Step - 3 Rotate Backups
# Simple three backupset rotation, keeps only last three
# Use directory /vol/backup as an example, make sure you change this path
# to fit your local settings. Have three directories in /vol/backup named daily,
# yesterday and daybefore.
# Make sure /vol/backup is on a different disk (preferably a different machine)
# than the files you are backing up

#looks specifically for backup files in case other files are kept in these directories
for bkfile in $DBBKUPDIR/*; do
echo $bkfile | grep $TAG >/dev/null &&
(rm $bkfile || exiterror $LINENO )
done
for bkfile in $YDBKUPDIR/*; do
echo $bkfile | grep $TAG >/dev/null &&
(mv $bkfile $DBBKUPDIR/ || exiterror $LINENO )
done
for bkfile in $BKUPDIR/*; do
echo $bkfile | grep $TAG >/dev/null &&
(mv $bkfile $YDBKUPDIR/ || exiterror $LINENO )
done

# Step - 4 Archive Home Directories
# Creates a seperate tar file for each directory in the directories in $BACKUPTHESE


# change to dir to backup. Check in case cd failed for some reason.
cd $BACKUPTHIS || exiterror $LINENO
#backup all files listed in BACKUPTHESE
for DIRTOBACKUP in $BACKUPTHESE; do
for FOLDERNAME in $DIRTOBACKUP/*
do
# Archives are created in the format someuser-$TAG-datetime.tar.gz
# basename is used here so as not to include absolute paths
# -p preserves permissions
echo -e "-------------------\n>>>taring ${FOLDERNAME}\n\n" >> $LOG
tar -czvpf ${BKUPDIR}/$( basename $FOLDERNAME )-${TAG}-${BUTDSTAMP}.tar.gz ${FOLDERNAME} >> $LOG || exiterror $LINENO
done
done

# Step - 5 Finish Email Report and Send

echo "Backup ended $(date)" >> $LOG

# df -h includes a human readable disk usage report of the media that /vol/backup
# is mounted on. Good to now if your backup disk is running out of space.
# Of course /dev/hdb1 is the device I use, modify it for your local settings

df -h /dev/hdb1 >> $LOG
mail somebody@somedomain.com -s "Backup Job Report" -v < $LOG

# Step - 6 All Done
__________________
Avatar from xkcd.com, a hilarious computer related webcomic.
jpalfree is offline   Reply With Quote
Old 04-12-2006   #3 (permalink)
Linux User
 
stokes's Avatar
 
Join Date: Oct 2004
Location: UK
Posts: 260
Send a message via MSN to stokes
This is pretty useful, thanks for posting!
__________________
Registered Linux user #389109
My Linux Blog
stokes is offline   Reply With Quote
Old 04-13-2006   #4 (permalink)
Just Joined!
 
watchman's Avatar
 
Join Date: Apr 2006
Location: Maine, USA
Posts: 10
Quote:
Originally Posted by jpalfree
I've tried to make your script a bit more general and safe.
Thanks, though I will humbly admit you have quite a bit more scripting knowledge than I, I have copied your script and have learned quite a bit from it.

That's why a forum like this is great.
watchman is offline   Reply With Quote
Old 04-13-2006   #5 (permalink)
Linux Engineer
 
Join Date: Oct 2004
Location: Vancouver
Posts: 1,366
Send a message via AIM to genesus Send a message via Skype™ to genesus
wow, nice script watch, I've been going over the base script and mods now...looks like something I may implement!
__________________
Operating System: GNU Emacs
genesus is offline   Reply With Quote
Old 04-13-2006   #6 (permalink)
Linux Guru
 
antidrugue's Avatar
 
Join Date: Oct 2005
Location: Montreal, Canada
Posts: 3,213
Yes, very nice, thanks guys.

Why not tar it in bzip2 instead of gzip?
__________________
"To express yourself in freedom, you must die to everything of yesterday. From the 'old', you derive security; from the 'new', you gain the flow."

-Bruce Lee
antidrugue is offline   Reply With Quote
Old 04-13-2006   #7 (permalink)
Linux Newbie
 
jpalfree's Avatar
 
Join Date: Jul 2005
Location: Montreal, CA
Posts: 198
Quote:
Originally Posted by antidrugue
Yes, very nice, thanks guys.

Why not tar it in bzip2 instead of gzip?
you're probably right, antidrugue. bzip2 would be better in this case. actually another fix would be to renice the tar process so that it works nicely in the background, like so:
Quote:
...
nice --adjustment=20 tar -cjvpf ${BKUPDIR}/$( basename $FOLDERNAME )-${TAG}-${BUTDSTAMP}.tar.bz2
...
there might be a way to renice the whole script but I am unfamiliar with one.
__________________
Avatar from xkcd.com, a hilarious computer related webcomic.
jpalfree is offline   Reply With Quote
Old 09-28-2006   #8 (permalink)
Just Joined!
 
Join Date: Jul 2005
Posts: 6
Is this script written in Perl? And would I need to execute it from the cgi-bin? thanks.
simonsez is offline   Reply With Quote
Old 09-28-2006   #9 (permalink)
/etc/init.d/moderator
 
bigtomrodney's Avatar
 
Join Date: Nov 2004
Location: Sunny South-East of Ireland
Posts: 5,825
No this script is written in bash, as you can see in the shabang
Code:
#!/bin/bash
All you need to do is to save it to a file, and make that file executable
Code:
chmod +x mybackupscript.sh
__________________
Registered Linux user #378740
New members read here / Forum Rules
#linuxforums on irc.freenode.net
bigtomrodney is offline   Reply With Quote
Old 09-28-2006   #10 (permalink)
Linux Newbie
 
X.Cyclop's Avatar
 
Join Date: May 2006
Location: Brooklyn, New York
Posts: 142
Send a message via AIM to X.Cyclop Send a message via MSN to X.Cyclop Send a message via Skype™ to X.Cyclop
Quote:
Originally Posted by bigtomrodney
All you need to do is to save it to a file, and make that file executable
Code:
chmod +x mybackupscript.sh
Or just type sh script.sh.
__________________
"Don't think about the work, think about the benefit"

Leonardo Juszkiewicz
X.Cyclop is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Free Magazines
Free Network Mapping Tool for Microsoft® Office Visio® Professional 2007 Users
Don't map your network by hand – let LANsurveyor Express for Microsoft Visio Professional 2007 automatically create network diagrams for you.
subscribe
Free eBook:"Vulnerability Management for Dummies"
Get all the Facts and See How to Implement a Successful Vulnerability Management Program.
subscribe
Google vs The World: The Battle of the Message Security Vendors
With such a powerful name behind it, Google Message Security stands out in a sea of products that do exactly the same thing - or so they say.
subscribe

Safe, Secure Backup


All times are GMT. The time now is 12:21 PM.






© 2000 - 2009 - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.3.0 RC2