Backup
Scripts from UnixReview.com: Part II
Edited by Ed Schaefer
I still host the Shell Corner column, and each month a lucky reader
wins $100.00 when UnixReview.com publishes their submission (see
http://www.samag.com/documents/s=7033/sam0204d/ for Part
I). This month, I present three unpublished backup submissions:
backup_algo: Juan Vera (juan@coredump.com.ar) presents
a Bourne shell script that creates a compressed tar backup file
without overwriting the previous backup. The backup file name is
based on date and a sequential number.
qd2nfs.ss: Mark Foster (mdf@foster.cc) submits a Bourne
shell script that backs up essential directories to an NFS-mounted
/share directory. The script is meant to be executed before doing
upgrades. As the script executes, the admin interactively decides
whether to back up the directory.
tgzdir: Alan Eldridge (alane@geeksrus.net) submits a Korn/Bash
script that backs up individual directory objects to compressed
tar files. Alan's script includes options to control the number
of history files to keep, rename hidden object names, change the
backup target directory location, etc.
backup_algo
by Juan Vera
I administer Tomcat-based Web applications, web-apps. Tomcat encloses
web-apps in individual directories where config and resource files
reside. I require frozen versions of Tomcat-configured programs,
source trees, and Web pages. My Bourne shell script, backup_algo,
backs up directory objects without removing the previous backup
file. See Listing 1.
The structure of the backup file name is the basename(1)
of the directory to back up, year-month-day, the string "rel", and
an incremental number starting at zero. The script uses the backup
command tar, and compresses using compress(1). You
can use gzip(1) by changing the compress variable.
The execution syntax is:
backup_algo <object to backup> [directory location for backup]
1. Executing the script with no optional second argument:
backup_algo app
places the backup file in the same locaton as the "app" object:
app.2002-10-02-rel0.tar.Z
2. Executing with a second backup_algo app /tmp creates a backup
of the app directory, and creates a backup file in /tmp. The optional
directory must exist or an error will occur.
Successive calls with the same arguments will increment "release
number":
/tmp/app.2002-10-02-rel0.tar.Z
/tmp/app.2002-10-02-rel1.tar.Z
3. If the first argument is an absolute path, backup files are placed
in the same directory as the object being backed up:
backup_algo /etc/protocols
/etc/protocols.2002-10-02-rel0.tar.Z
4. Supply the optional second argument to place the backed up file
in a different location:
backup_algo /etc/protocols /tmp
/tmp/protocols.2002-10-02-rel0.tar.Z
5. To place the backup file in the present working directory:
backup_algo /etc 'pwd'
If the present working directory is /home/jvera, backup_algo creates
/home/jvera/etc.2002-10-02-rel0.tar.Z. Do not use the
"." operator as the second operator.
backup_algo /etc .
creates the backup file in the root directory (provided you have write
permissions):
./etc.2002-10-02-rel0.tar.Z
6. backup_algo supports spaces and other funky characters, but I don't
recommend using those:
mkdir "funky name"
cp /etc/services funky\ name/
Executing:
backup_algo funky\ name/
creates:
funky name.2002-10-02-rel0.tar.Z
qd2nfs.ss
by Mark Foster
I often run my Bourne shell quick-and-dirty, qd2nfs.ss, script
from our NFS-mounted /share directory before doing upgrades. See
Listing 2. As the script executes, the admin interactively decides
whether to back up each of the important directories. Here's what
the script does:
1. If the effective user isn't root, terminate the script.
2. Set the archive directory variable, typically an NFS share.
3. If the archive directory doesn't exist, create it.
4. Change to the root directory.
5. If no directories exist on the command line, default to "etc
/usr/local".
For each of the important directories from root, such as (etc,
usr/local:
1. Continue to loop if the directory does not exist.
2. Determine the size of the directory with the du command.
3. Display the directory name and size, and prompt the user whether
to back up the directory.
4. If the user answers yes, using tar, back up the directory to
the archive directory, or skip it.
tgzdir
by Alan Eldridge
I've been doing C/Unix/sh programming for 20 years, and I like
to fix hard-to-find distributed systems bugs. Over time, I've developed
this ksh/bash script, tgzdir, to back up my individual directory
objects (e.g., I check my system's /etc dir every hour and back
it up if it changed). See Listing 3.
In its most simple execution:
tgzdir /home/alane/sourcedir
the script, by default, creates a gzip-compressed tar file at the
parent directory of the directory being backed up:
/home/alane/sourcedir.20021207.0755.tar.gz
where a tarball of sourcedir is created December 7, 2002 at 0755 AM.
tgzdir supports the following options:
-k|--keep) -- Keep <n> backups. default is 10.
-d|--dest) -- Change the destination directory (e.g., tgzdir
-d /tmp /home/alane/sourcedir).
-r|--rename) -- Rename hidden tarball file name. Backing
up .sourcdir creates .sourcedir.20021207.0755.tar.gz by default.
Change the leading "." to "_".
-X|--delete) -- Delete directory after backing it up, only
if the backup was successful.
-x|--debug) -- Set the shell's debugging mode (shell set
-x command).
-D|--date-only) -- Remove the time from the tarball file
name (i.e., sourcedir.20021207.tar.gz).
Ed Schaefer is a frequent contributor to Sys Admin. He is a
software developer and DBA for Intel's Factory Integrated Information
Systems, FIIS, in Aloha, Oregon. Ed also hosts the UnixReview.com
monthly Shell Corner column. He can be reached at: olded@ix.netcom.com.
Juan Vera has been a UNIX sys admin for six years. He has experience
on ISPs, consulting firms, financial firms, and banks. He prefers
to work with OpenBSD.
Mark Foster works for VeriSign(eNIC) and lives in Seattle,
WA, tending to and tinkering with FreeBSD and Linux servers. Find
out more about Mark at: http://mark.foster.cc/.
Alan Eldridge is a UNIX software designer and developer, heavily
involved in the FreeBSD project. He will soon be leaving Wall St.
for Denver, in search of peace, tranquility, and a paycheck. He
can be reached at: alane@freebsd.org.
|