Linux - GeneralThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
#!/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
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 11:08 PM.
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
#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 05:55 AM.
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")
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.