LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 09-28-2006, 08:24 PM   #16
dfwcomputer
Member
 
Registered: Mar 2006
Posts: 47

Original Poster
Rep: Reputation: 15

thanks very much.


Anyone got any ideas on restoring the backups

e.g. tar xfv *.tar.gz (maybe)
 
Old 09-28-2006, 08:33 PM   #17
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
here's a file restoration script

Code:
#!/bin/bash

# restore.sh (c) 2006 konsolebox
# license: GPLv2
#

usage() {
	message "usage: restore.sh [-q] file [file ...] [rootdir]\n"
	exit 1
}

# set to 1 for quiet mode by default
QUIET=
# default rootdir, can be set to a different default root directory
ROOTDIR=/
# default tar output
TAROUTPUT=

# logging functions
log() {
	[ "$QUIET" ] && return
	echo -e -n "$@"
}
message() { log "$@"; }
warning() { log "warning: ""$@"; read; }
error() { log "error: ""$@"; exit 1; }

# when doing 'restores.sh * restoredir', directories won't be included from the list
# if the last argument is a file and not a directory, restoration will default to /
[ $# -eq 0 ] && usage
until [ $# -eq 1 ]; do
	case $1 in
	-q|--quiet)
		QUIET=1
		;;
	-h|--help)
		usage
		;;
	*)
		if [ -f $1 ]; then
			FILES="$FILES $1"
		elif [ ! -d $1 ]; then
			warning "possibly invalid parameter no $#: $1\n"
		fi
		;;
	esac
	shift
done
if [ -d $1 ]; then
	ROOTDIR=$1
else
	FILES="$FILES $1"
fi

# extract files
if [ "$QUIET" ]; then
	TAROUTPUT=">/dev/null 2>&1"
fi
for a in ${FILES}; do
	message "extracting $a to $ROOTDIR\n"
	eval "tar -C $ROOTDIR -vzxf $a $TAROUTPUT"
	message "\n"
done
 
Old 09-28-2006, 08:51 PM   #18
dfwcomputer
Member
 
Registered: Mar 2006
Posts: 47

Original Poster
Rep: Reputation: 15
Thumbs up

Excellent thanks again for that, much appreciated

Dean
 
Old 09-28-2006, 10:07 PM   #19
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
You're welcome. Btw I thought doing 'restore.sh *' can cause errors if the last paramater in * is a directory. Here's a more stable version:
Code:
#!/bin/bash

# restore.sh (c) 2006 konsolebox
# backup restoration script for dfwcomputer
# version 1.0
# license: GPLv2

usage() {
	message "usage: restore.sh [-q] [-r rootdir] file [file ...]\n"
	exit 1
}

# set to 1 for quiet mode by default
QUIET=
# default rootdir, can be set to a different default root directory
ROOTDIR=/
# default tar output
TAROUTPUT=

# logging functions
log() {
	[ "$QUIET" ] && return
	echo -e -n "$@"
}
message() { log "$@"; }
warning() { log "warning: ""$@"; read; }
error() { log "error: ""$@"; exit 1; }

message "restore.sh 1.0\n\n"

# when doing 'restores.sh * restoredir', directories won't be included from the list
# if the last argument is a file and not a directory, restoration will default to /
[ $# -eq 0 ] && usage
while [ $# -gt 0 ]; do
	case $1 in
	-q|--quiet)
		QUIET=1
		;;
	-h|--help)
		usage
		;;
	-r|--rootdir)
		if [ -d $2 ]; then
			ROOTDIR=$2
		else
			error "invalid argument to $1: $2\n"
		fi
		shift
		;;
	*)
		if [ -f $1 ]; then
			FILES="$FILES $1"
		elif [ -d $1 ]; then
			message "ignoring directory $1\n"
		else
			warning "possible invalid parameter found: $1\n"
		fi
		;;
	esac
	shift
done

# extract files
if [ "$QUIET" ]; then
	TAROUTPUT=">/dev/null 2>&1"
fi
for a in ${FILES}; do
	message "extracting $a to $ROOTDIR\n"
	eval "tar -C $ROOTDIR -vzxf $a $TAROUTPUT"
	message "\n"
done

Last edited by konsolebox; 09-28-2006 at 10:08 PM.
 
Old 09-28-2006, 11:21 PM   #20
dfwcomputer
Member
 
Registered: Mar 2006
Posts: 47

Original Poster
Rep: Reputation: 15
Thanks, i will give it a try

do i have to but the file in the directory with the backups or can it run from anywhere.
 
Old 09-29-2006, 12:35 AM   #21
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
i guess it can run from anywhere.
 
Old 10-09-2009, 02:49 AM   #22
dfwcomputer
Member
 
Registered: Mar 2006
Posts: 47

Original Poster
Rep: Reputation: 15
I know this thread is old but I still use this excellent script :)

I recently decided to update my server from ubuntu drapper to ubuntu jaunty (I know it took me a while lol) and I seem to be getting errors in my script now :(

my current script is

Code:
#!/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

# Some variables
# Set to 1 for verbose mode by default (might not work with cron).
VERBOSE=

# Some messaging functions
log() {
    [ "$VERBOSE" ] && echo -e "$@"
    echo -e "$@" >> $LOG
}

log_error() {
    [ "$VERBOSE" ] && echo -e "$@"
    echo -e "$@" >> $LOG
}

# Check args
for a in $*; do
    case $a in
    -v)
        VERBOSE=1
        ;;
    *)
        ;;
    esac
done

# 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 with tags
# format: "DIR:TAG ..."
# eg: BACKUPPAIRS="/homehome-backup /root:root-backup /etc:etc-backup"
BACKUPPAIRS="/home:home-backup /etc:etc-backup"

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

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

# function that quits and logs on error
exiterror()
{
    # use this function by supplying $LINENO as first arg
    log_error "Fatal error caused by line ${1} of ${0}"
    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
log "System Backup $BUTDSTAMP"

# 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.

log "Backup Began $(date)"

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

# create the backup directories if they don't exist
[ ! -d $DBBKUPDIR ] && ( mkdir -p $DBBKUPDIR || exiterror $LINENO )
[ ! -d $YDBKUPDIR ] && ( mkdir -p $YDBKUPDIR || exiterror $LINENO )
[ ! -d $BKUPDIR ] && ( mkdir -p $BKUPDIR || exiterror $LINENO )

#looks specifically for backup files in case other files are kept in these directories
for PAIR in $BACKUPPAIRS; do
    TAG=${PAIR/*:}
    [ -d $DBBKUPDIR/$TAG ] && \
        ( rm -fr $DBBKUPDIR/$TAG || exiterror $LINENO )
    [ -d $YDBKUPDIR/$TAG ] && \
        ( mv $YDBKUPDIR/$TAG $DBBKUPDIR/ || exiterror $LINENO )
    [ -d $BKUPDIR/$TAG ] && \
        ( mv $BKUPDIR/$TAG $YDBKUPDIR/ || exiterror $LINENO )
done

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

# change to dir to backup. Check in case cd failed for some reason.
# cd $BACKUPPAIRS || exiterror $LINENO
# check verbosity
if [ "$VERBOSE" ]; then
    TAROUTPUT="| tee -a $LOG"
else
    TAROUTPUT=">> $LOG 2>/dev/null"
fi
# backup all files listed in BACKUPPAIRS
for PAIR in $BACKUPPAIRS; do
    DIRTOBACKUP=${PAIR/:*}
    TAG=${PAIR/*:}
    log "Doing backups for $DIRTOBACKUP using TAG $TAG"
    [ -d $DIRTOBACKUP ] || exiterror $LINENO
    # if we don't want to include DIRTOBACKUP from the archive, we should
    # note the annoying 'removing trailing lines' tar messages so i included 2>/dev/null
    # cd $DIRTOBACKUP || exiterror $LINENO
    # for FOLDERNAME in *
    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
        log "-------------------\n>>> tarring ${FOLDERNAME}"
        mkdir -p ${BKUPDIR}/${TAG} || exiterror $LINENO
        # tar -czvpf ${BKUPDIR}/${TAG}/$( basename $FOLDERNAME )-${TAG}-${BUTDSTAMP}.tar.gz ${FOLDERNAME} >> $LOG 2>/dev/null || exiterror $LINENO
        eval "tar -czvpf ${BKUPDIR}/${TAG}/$( basename $FOLDERNAME )-${TAG}-${BUTDSTAMP}.tar.gz ${FOLDERNAME} ${TAROUTPUT} || exiterror $LINENO"
        log
    done
done

# Step - 5 Finish Email Report and Send

log "Backup ended $(date)"

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

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

# Step - 6 All Done
and the response I get is

Code:
root@server:/home/deano# sh backup.sh
backup.sh: 102: Bad substitution
any ideas on what the problem is.I think it could related to this section

Code:
#looks specifically for backup files in case other files are kept in these directories
for PAIR in $BACKUPPAIRS; do
    TAG=${PAIR/*:}
    [ -d $DBBKUPDIR/$TAG ] && \
        ( rm -fr $DBBKUPDIR/$TAG || exiterror $LINENO )
    [ -d $YDBKUPDIR/$TAG ] && \
        ( mv $YDBKUPDIR/$TAG $DBBKUPDIR/ || exiterror $LINENO )
    [ -d $BKUPDIR/$TAG ] && \
        ( mv $BKUPDIR/$TAG $YDBKUPDIR/ || exiterror $LINENO )
done
 
Old 10-09-2009, 04:40 AM   #23
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
wow it's been 3 years. how have you been

regarding:

Code:
for PAIR in $BACKUPPAIRS; do
    TAG=${PAIR/*:}
    [ -d $DBBKUPDIR/$TAG ] && \
        ( rm -fr $DBBKUPDIR/$TAG || exiterror $LINENO )
    [ -d $YDBKUPDIR/$TAG ] && \
        ( mv $YDBKUPDIR/$TAG $DBBKUPDIR/ || exiterror $LINENO )
    [ -d $BKUPDIR/$TAG ] && \
        ( mv $BKUPDIR/$TAG $YDBKUPDIR/ || exiterror $LINENO )
done
try to change it to something like this:

Code:
#looks specifically for backup files in case other files are kept in these directories
for PAIR in $BACKUPPAIRS; do
    TAG=${PAIR/*:}
    test -d "$DBBKUPDIR/$TAG" && \
        ( rm -fr $DBBKUPDIR/$TAG || exiterror $LINENO )
    test -d "$YDBKUPDIR/$TAG" && \
        ( mv "$YDBKUPDIR/$TAG" "$DBBKUPDIR"/ || exiterror $LINENO )
    test -d "$BKUPDIR/$TAG" && \
        ( mv "$BKUPDIR/$TAG" "$YDBKUPDIR/" || exiterror $LINENO )
done
i made a recommendation about always using the [..] pairs but you can also use test.. i prefer to use it now.. well except in bash where sometimes i prefer to use [[

edit: You should do that in other variables as well (the ones that are parsed as arguments). Also, most of the time it's not needed but only in assignment statements (var=$assignment).

Last edited by konsolebox; 10-09-2009 at 04:55 AM.
 
Old 10-09-2009, 06:13 AM   #24
dfwcomputer
Member
 
Registered: Mar 2006
Posts: 47

Original Poster
Rep: Reputation: 15
Thumbs down

yer good m8, your little script has been a tool i couldn't live with out

I've now got this after an hour lol

Code:
#!/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

# Some variables
# Set to 1 for verbose mode by default (might not work with cron).
VERBOSE=1

# Some messaging functions
log() {
    [ "$VERBOSE" ] && echo -e "$@"
    echo -e "$@" >> $LOG
}

log_error() {
    [ "$VERBOSE" ] && echo -e "$@"
    echo -e "$@" >> $LOG
}

# Check args
for a in $*; do
    case $a in
    -v)
        VERBOSE=1
        ;;
    *)
        ;;
    esac
done

# 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 with tags
# format: "DIR:TAG ..."
# eg: BACKUPPAIRS="/homehome-backup /root:root-backup /etc:etc-backup"
BACKUPPAIRS="/home:home-backup /etc:etc-backup"

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

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

# function that quits and logs on error
exiterror()
{
    # use this function by supplying $LINENO as first arg
    log_error "Fatal error caused by line ${1} of ${0}"
    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
log "System Backup $BUTDSTAMP"

# 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.

log "Backup Began $(date)"

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

# create the backup directories if they don't exist
test -d $DBBKUPDIR && \
    ( mkdir -p $DBBKUPDIR || exiterror $LINENO )
test -d $YDBKUPDIR && \
    ( mkdir -p $YDBKUPDIR || exiterror $LINENO )
test -d $BKUPDIR && \
    ( mkdir -p $BKUPDIR || exiterror $LINENO )

#looks specifically for backup files in case other files are kept in these directories
#looks specifically for backup files in case other files are kept in these directories
for PAIR in $BACKUPPAIRS; do
    TAG=${PAIR/*:}
    test -d "$DBBKUPDIR/$TAG" && \
        ( rm -fr $DBBKUPDIR/$TAG || exiterror $LINENO )
    test -d "$YDBKUPDIR/$TAG" && \
        ( mv "$YDBKUPDIR/$TAG" "$DBBKUPDIR"/ || exiterror $LINENO )
    test -d "$BKUPDIR/$TAG" && \
        ( mv "$BKUPDIR/$TAG" "$YDBKUPDIR/" || exiterror $LINENO )
done

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

# change to dir to backup. Check in case cd failed for some reason.
# cd $BACKUPPAIRS || exiterror $LINENO
# check verbosity
if [ "$VERBOSE" ]; then
    TAROUTPUT="| tee -a $LOG"
else
    TAROUTPUT=">> $LOG 2>/dev/null"
fi
# backup all files listed in BACKUPPAIRS
for PAIR in $BACKUPPAIRS; do
    DIRTOBACKUP=${PAIR/:*}
    TAG=${PAIR/*:}
    log "Doing backups for $DIRTOBACKUP using TAG $TAG"
    test -d $DIRTOBACKUP || exiterror $LINENO
    # if we don't want to include DIRTOBACKUP from the archive, we should
    # note the annoying 'removing trailing lines' tar messages so i included 2>/dev/null
    # cd $DIRTOBACKUP || exiterror $LINENO
    # for FOLDERNAME in *
    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
        log "-------------------\n>>> tarring ${FOLDERNAME}"
        mkdir -p ${BKUPDIR}/${TAG} || exiterror $LINENO
        # tar -czvpf ${BKUPDIR}/${TAG}/$( basename $FOLDERNAME )-${TAG}-${BUTDSTAMP}.tar.gz ${FOLDERNAME} >> $LOG 2>/dev/null || exiterror $LINENO
        eval "tar -czvpf ${BKUPDIR}/${TAG}/$( basename $FOLDERNAME )-${TAG}-${BUTDSTAMP}.tar.gz ${FOLDERNAME} ${TAROUTPUT} || exiterror $LINENO"
        log
    done
done

# Step - 5 Finish Email Report and Send

log "Backup ended $(date)"

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

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

Code:
testy@server:~$ sudo sh backup.sh
: not found2: 
: not found7: 
: not found11: 
-e 
: not found16: }
: not found17: 
-e 
: not found21: }
: not found22: 
backup.sh: 24: Syntax error: word unexpected (expecting "do")
the logfile just had this in it

Code:
-e
-e
-e
-e
if that helps lol
 
Old 10-09-2009, 10:45 AM   #25
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
Quote:
Originally Posted by dfwcomputer View Post
yer good m8, your little script has been a tool i couldn't live with out
i'm glad to hear that
Quote:
Code:
testy@server:~$ sudo sh backup.sh
: not found2: 
: not found7: 
: not found11: 
-e 
: not found16: }
: not found17: 
-e 
: not found21: }
: not found22: 
backup.sh: 24: Syntax error: word unexpected (expecting "do")
some things seem to refer to invisible commands (spaces). maybe you should check if the file has been saved in DOS format and not in UNIX format?

you can also try to debug the script and know what part it causes an error:
Code:
bash -x backup.sh
btw, is sh referring to bash?.. also if you use sh, sometimes bash runs in compatibility mode so some commands supported only bash might not work.. so maybe you should also run it as
Code:
sudo bash backup.sh
 
Old 10-10-2009, 11:23 PM   #26
dfwcomputer
Member
 
Registered: Mar 2006
Posts: 47

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by konsolebox View Post
Code:
sudo bash backup.sh
That did the trick.Not sure why but it did lol

once again thankyou for all your help.
 
  


Reply

Tags
backup, script, ubuntu


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
backup script tommytomato Linux - General 6 09-22-2006 02:41 AM
Need backup script. gtrawoger Linux - Software 2 07-17-2006 07:31 AM
startup script modifying my XF86Config bads Linux - Newbie 5 09-07-2005 09:29 AM
Using CP in my backup script. Echo Kilo Programming 6 06-01-2005 08:43 AM
Modifying this iptables script for non router use. slewis1972 Linux - Networking 6 09-21-2003 08:46 AM


All times are GMT -5. The time now is 07:05 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration