#!/bin/bash ###################################################### #+ Simple archive script with multiple backends #+ Rev. 3 - 2/07/09 - optimum.reflex@gmail.com ###################################################### hlp () { # Help function echo "usage: $0 [--help] [archive] [sources]" echo "[sources] is the files or folder you want added to the archive." echo echo "[archive] is the name of the archive generated, including extension." echo "Extensions are: .7z .zip .bz2 .gz .lzma .tar.*" exit 0 } if [ $# -lt 2 ]; then # Need atleast an archive and one source hlp fi tarfunc () { # function that does the work MSG="packing [$SRCD] to [$DIR/$TAR]... " case "$TAR" in *.7z ) echo -n $MSG /usr/bin/7z a -mx=9 $DIR/$TAR $SRC ;; *.tar ) echo -n $MSG /bin/tar -acf $DIR/$TAR $SRC ;; *.t* ) echo -n $MSG /bin/tar -acZf $DIR/$TAR $SRC ;; *.bz2 ) echo -n $MSG /bin/bzip2 -cq9 $SRC > $DIR/$TAR ;; *.gz ) echo -n $MSG /bin/gzip -cq9 $SRC > $DIR/$TAR ;; *.zip ) echo -n $MSG /usr/bin/zip -qr9 $DIR/$TAR $SRC ;; *.lzma ) echo -n $MSG /usr/bin/lzma -cq9 $SRC > $DIR/$TAR ;; *) hlp;; esac } tdir () { # Determin target directory for archive if [ -d $1 ]; then if [ "$1" == "." ]; then DIR="$PWD" else DIR="$1" fi else DIR="$PWD" fi } case "$@" in *--help*) hlp;; esac TAR="`basename $1`" tdir `dirname $1` && shift if [ $# -gt 1 ]; then # If more than one source SRCD="$@" # all sources to $SRCD i=0 # counter while [ "$1" ]; do if [ $i -eq 0 ]; then # only if the first source SRC="$1" && shift if [ ! -r "$SRC" ]; then echo "Location [$SRC] does not exist or is unreadable." exit 1 fi ((i++)) # increment else # if sources after the first if [ ! -r "$1" ]; then echo "Location [$1] does not exist or is unreadable." exit 1 fi SRC="$SRC $1" && shift # copy current $SRC and append next source fi done tarfunc # do the work else # else if there is only one source SRC="$1" SRCD="$SRC" # copy $SRC if [ ! -r "$SRC" ]; then echo "Location [$SRC] does not exist or is unreadable."; exit 1 elif [ -d "$SRC" ]; then # if source is a directory cd `dirname $SRC` # goto the directory one up from $SRC SRC="`basename $SRC`/" # change $SRC for correct directory packing structure fi tarfunc # do the work fi if [ $? -ne 0 ]; then # if last command failed cd $DIR rm -f $TAR && echo "failure detected, archive deleted." exit 1 else echo "success!"; exit 0 fi