WORLD'S WORST SOFTWARE
Software by jason, for jason.
Software by jason, for jason.
Home
About
Articles
Resume
Contact
Software
iPlaylist CopierJava Libraries
ItunesUtilitiesScripts
Bash Scripts
File Size File Size Test Free Space Line Counter List Copy MD5 Tool OS X Rotate Logs Rename It SnapshotPHP Scripts
Write Variable To File WWS MailerJavascript Scripts
WWS Background ScalerInteresting People
Admins
Phil GlocknerDesigners
Tonya Browning Catherine Chu Justin Cox Richard Georges Stephen Gray Jonathan Horak Justin Kiehl Andrea Spencer MeatheadDevelopers
Chris Austin Joost van Dongen Noah Figg Rick Hogge John Quarles Wayne Rittiman Joe Rivera Brent Schneeman David Scott Andy Skelton Pavan TumatiSnapshot
The snapshot bash script creates or deletes dated snapshot directories from the path you specify. Snapshot will create a dated snapshot directory for you and echo the directory's name to stdout, or snapshot will delete older snapshot directories. The snapshot deletion operation allows you to specify how many older snapshot directories to preserve. You can specify to clean out the newly created snapshot directory if the directory existed before the snapshot script was executed.
Dependencies
bash, echo, test, grep, find, rm, date, mkdir, xargs
Download
snapshot.sh [3.2 KB]
The Script
#!/bin/bash
########################################################################
#
# snapshot.sh creates and removes dated snapshot directories
# written by Jason Baker (http://www.worldsworstsoftware.com)
#
########################################################################
#
# UPDATES:
# 2007/1/27
# - fixed some issue with unquoted arguments containing spaces
# 2006/10/25
# - updated usage
# 2006/10/12
# - initial version
#
########################################################################
function print_usage()
{
echo "snapshot.sh creates and removes dated snapshot directories"
echo
echo "USAGE"
echo " snapshot.sh OPERATION PATH"
echo
echo "ARGUMENTS"
echo " OPERATION - the operation to perform"
echo " PATH - the path to create or deleted snapshot directores from"
echo
echo "VALID OPERATIONS"
echo " CREATE - Create snapshot directory for today,"
echo " and print the directory name to stdout"
echo
echo " CREATECLEAN - Same as CREATE, except clean out "
echo " contents if the dir already exists"
echo
echo " DELETE N - Delete snapshot directories, "
echo " but save latest N of them"
echo
echo
echo " ERROR: $1"
echo
exit 1
}
function test_directory_exists()
{
if test ! -d "$1"
then
print_usage "Directory Not Found: $1"
fi
}
function test_is_numeric()
{
NUM=`echo "$1" | grep '^[0-9]\+$'`
if test -z $NUM
then
print_usage "\"$1\" is not a number."
fi
}
function clear_snapshots()
{
# arg 1 = path to clear snapshots from
# arg 2 = directories to save
DIRECTORIES=`find "$1" -maxdepth 1 -mindepth 1 -type d | sort`
#make for's argument seperator newline only
IFS=$'\n'
DIRECTORIES_TO_SAVE=$2
COUNT=0
for DIRECTORY in $DIRECTORIES
do
COUNT=$(( $COUNT + 1 ))
done
COUNT=$(( $COUNT - $DIRECTORIES_TO_SAVE ))
for DIRECTORY in $DIRECTORIES
do
if test $COUNT -gt 0
then
echo "removing old snapshot \"$DIRECTORY\""
rm -Rf "$DIRECTORY"
COUNT=$(( $COUNT - 1 ))
else
break
fi
done
}
function create_snapshot()
{
# arg 1 = path name to create snapshot in
# arg 2 = "CLEAN" if snapshot should be cleaned out
TODAY=`date +"%Y%m%d"`
COMPLETED_PATH="${1%*/}/" #this will put a / on the end of the path if there isnt one already
NEW_SNAPSHOT_DIR="$COMPLETED_PATH$TODAY"
if test ! -d "$NEW_SNAPSHOT_DIR"
then
mkdir "$NEW_SNAPSHOT_DIR"
fi
if test "$2" = "CLEAN"
then
#
# thanks to the unixadmin livejournal community for help on this bit below
# http://community.livejournal.com/unixadmin/82573.html
#
find "$NEW_SNAPSHOT_DIR/" -maxdepth 1 -mindepth 1 -print0 | xargs -0 rm -rf
fi
echo "$NEW_SNAPSHOT_DIR"
}
if test -z $1
then
print_usage "No arguments specified."
fi
if test $1 = "CREATE"
then
test_directory_exists "$2"
create_snapshot "$2"
elif test $1 = "CREATECLEAN"
then
test_directory_exists "$2"
create_snapshot "$2" "CLEAN"
elif test $1 = "DELETE"
then
test_is_numeric "$2"
test_directory_exists "$3"
clear_snapshots "$3" "$2"
else
print_usage "Unsupported Operation: $1"
fi
exit 0
########################################################################
#
# snapshot.sh creates and removes dated snapshot directories
# written by Jason Baker (http://www.worldsworstsoftware.com)
#
########################################################################
#
# UPDATES:
# 2007/1/27
# - fixed some issue with unquoted arguments containing spaces
# 2006/10/25
# - updated usage
# 2006/10/12
# - initial version
#
########################################################################
function print_usage()
{
echo "snapshot.sh creates and removes dated snapshot directories"
echo
echo "USAGE"
echo " snapshot.sh OPERATION PATH"
echo
echo "ARGUMENTS"
echo " OPERATION - the operation to perform"
echo " PATH - the path to create or deleted snapshot directores from"
echo
echo "VALID OPERATIONS"
echo " CREATE - Create snapshot directory for today,"
echo " and print the directory name to stdout"
echo
echo " CREATECLEAN - Same as CREATE, except clean out "
echo " contents if the dir already exists"
echo
echo " DELETE N - Delete snapshot directories, "
echo " but save latest N of them"
echo
echo
echo " ERROR: $1"
echo
exit 1
}
function test_directory_exists()
{
if test ! -d "$1"
then
print_usage "Directory Not Found: $1"
fi
}
function test_is_numeric()
{
NUM=`echo "$1" | grep '^[0-9]\+$'`
if test -z $NUM
then
print_usage "\"$1\" is not a number."
fi
}
function clear_snapshots()
{
# arg 1 = path to clear snapshots from
# arg 2 = directories to save
DIRECTORIES=`find "$1" -maxdepth 1 -mindepth 1 -type d | sort`
#make for's argument seperator newline only
IFS=$'\n'
DIRECTORIES_TO_SAVE=$2
COUNT=0
for DIRECTORY in $DIRECTORIES
do
COUNT=$(( $COUNT + 1 ))
done
COUNT=$(( $COUNT - $DIRECTORIES_TO_SAVE ))
for DIRECTORY in $DIRECTORIES
do
if test $COUNT -gt 0
then
echo "removing old snapshot \"$DIRECTORY\""
rm -Rf "$DIRECTORY"
COUNT=$(( $COUNT - 1 ))
else
break
fi
done
}
function create_snapshot()
{
# arg 1 = path name to create snapshot in
# arg 2 = "CLEAN" if snapshot should be cleaned out
TODAY=`date +"%Y%m%d"`
COMPLETED_PATH="${1%*/}/" #this will put a / on the end of the path if there isnt one already
NEW_SNAPSHOT_DIR="$COMPLETED_PATH$TODAY"
if test ! -d "$NEW_SNAPSHOT_DIR"
then
mkdir "$NEW_SNAPSHOT_DIR"
fi
if test "$2" = "CLEAN"
then
#
# thanks to the unixadmin livejournal community for help on this bit below
# http://community.livejournal.com/unixadmin/82573.html
#
find "$NEW_SNAPSHOT_DIR/" -maxdepth 1 -mindepth 1 -print0 | xargs -0 rm -rf
fi
echo "$NEW_SNAPSHOT_DIR"
}
if test -z $1
then
print_usage "No arguments specified."
fi
if test $1 = "CREATE"
then
test_directory_exists "$2"
create_snapshot "$2"
elif test $1 = "CREATECLEAN"
then
test_directory_exists "$2"
create_snapshot "$2" "CLEAN"
elif test $1 = "DELETE"
then
test_is_numeric "$2"
test_directory_exists "$3"
clear_snapshots "$3" "$2"
else
print_usage "Unsupported Operation: $1"
fi
exit 0
© 2006, 2007 Jason Baker