freebsd backup script

Script? How about a oneliner?

# tar -C /some/dir -zcvf backup.tgz files

This will create a gzipped tar file called backup.tgz of the directory /some/dir/files/.
 
i need a script that can be used to configure which directories to backup and use those directories as arguments to the tar utility creating an archive file... anyone can help.. please..
 
This is what I use on my system:

Code:
#!/bin/sh
## **********************************************************************
## Copyright (c) 2008-2010, Aaron J. Graves
## All rights reserved.
##
## Redistribution and use in source and binary forms, with or without 
## modification, are permitted provided that the following conditions are met:
##
## 1. Redistributions of source code must retain the above copyright notice,
##    this list of conditions and the following disclaimer.
## 2. Redistributions in binary form must reproduce the above copyright notice,
##    this list of conditions and the following disclaimer in the documentation
##    and/or other materials provided with the distribution.
##
## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
## AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
## IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
## ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
## LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
## CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
## SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
## INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
## CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
## ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
## POSSIBILITY OF SUCH DAMAGE.
## **********************************************************************

# Read configuration
installed="/usr/local/"
etcd="etc/"

# If an arg is passed, assume alternative conf file.
if [ "$1" = "" ]; then
  conffile="${installed}${etcd}backup.conf"
else
  conffile="$1"
fi

# wrt is the location where the backups will be written
wrt="/usr/backups"
# dtbu = dirs to back up
dtbu=`/bin/cat $conffile | /usr/bin/grep -v "^#"`
# dte is a specially formatted date that will be appended to the file name.
dte=`/bin/date +%Y%m%d%H%M`

# Unfortunately we must run as root (to make sure we can back up directories
#  that someone may have set "go= " on. As we all know, r00t over-rides all.
if [ `/usr/bin/id -un` != "root" ]; then
        /bin/echo 1>&2 ERROR: This command must be run by root...
        exit 1
fi

#set -x

# Make sure an error log exists
if [ ! -f $wrt/bkerror.log ]; then
        /usr/bin/touch $wrt/bkerror.log; /usr/sbin/chown backups:backups $wrt/bkerror.log
        /bin/chmod 660 $wrt/bkerror.log
fi

# Place today's date in the log
echo "*** `date` ***" >> $wrt/bkerror.log

printf "Running backups...\n"
for i in $dtbu
do
 prf="/"
 printf "\tGrabbing /$i... "
 ti=`/bin/echo $i | /usr/bin/sed "s,/,_,g"`
 #/usr/bin/tar -cfz $wrt/$ti-$dte.tar.gz /$i >> $wrt/bkerror.log 2>&1
 /usr/bin/tar -cf $wrt/$ti-$dte.tar $prf$i >> $wrt/bkerror.log 2>&1
 /usr/bin/gzip $wrt/$ti-$dte.tar
 /bin/chmod 660 $wrt/$ti-$dte.tar.gz
 /usr/sbin/chown backups:backups $wrt/$ti-$dte.tar.gz
 /bin/echo "Done."
done
printf "Compressing the error log... "
/bin/mv $wrt/bkerror.log $wrt/bkerror-$dte.log
/usr/bin/gzip $wrt/bkerror-$dte.log
/bin/chmod 660 $wrt/bkerror-$dte.log.gz
/usr/sbin/chown backups:backups $wrt/bkerror-$dte.log.gz
printf "Done.\n"
printf "\nBackups complete!\n\n"

Configuration file is stored as /usr/local/etc/backup.conf. Here's an example of the format of that file:
Code:
# Conf file for backup. List a directory (one per line) that should be backed up
# Do not use a leading "/"! This will break things!
etc
usr/local/etc
var/log
usr/com
var/com/muhboard
usr/vmail
var/mail
# Only enable the below lines for a FULL system backup!
#usr/home
#usr/local
#var
 
krkbryant08 said:
nice! i will try to use this code...

Use it and abuse it. The script is run via the system crontab (/etc/crontab) every night at midnight on my system.

Also I forgot to mention... the 'chown backups:backups' may need to be modified, unless you want to add a user and group called 'backups' to your system. Otherwise, you can chown to any user/group combo you want.

Edit: Forgot to mention this as well. There is a sed line that modifies the '/' to a '_' in the directory path. So for example, if you're backing up /usr/local/etc, the filename will be usr_local_etc and then appended with the date.
 
Back
Top