#!/bin/bash MYVERSION=0.20080420 # If you improve this script, please read the comments at the # end of this file. Thanks. #usage/help message usage_message() { cat >&2 <&2 exit $E_OSTYPE } #parse the arguments while getopts "hp:qt:vV" ARGS 2>/dev/null; do case $ARGS in \?) printf "$ERRFMT" "cannot parse given parameters" >&2 usage_message exit $E_ARGS ;; h) usage_message; exit $E_HELP; ;; p) PROFNAME=$OPTARG; ;; q) TARVERB=""; ;; t) TAG=-$OPTARG; ;; v) TARVERB=--verbose; ;; V) printf "$NFOFMT" "version $MYVERSION" >&2; exit $E_HELP; ;; esac done # move on to non-option parameters shift $((OPTIND - 1)) # the only parameter (if any) should be the output directory, # so figure it out and do a little error checking [[ $# -gt 1 ]] && { printf "$ERRFMT" "too many arguments given" >&2 usage_message exit $E_ARGS } [[ -n $1 ]] && OUTDIR=$1 # if under cygwin, change paths to POSIXness if [[ $OSTYPE == *cygwin* ]]; then FXSETDIR=$(cygpath "$FXSETDIR") && OUTDIR=$(cygpath "$OUTDIR") || { printf "$ERRFMT" \ "error using cygpath under cygwin" \ "please make sure cygpath is installed" >&2 exit $E_CYG } fi # strip trailing / if present OUTDIR=$(echo "$OUTDIR" | sed 's~\(.\+\)/$~\1~') [[ -d $OUTDIR ]] || { printf "$ERRFMT" "$OUTDIR is not a directory" >&2 usage_message exit $E_OUTDIR } [[ -w $OUTDIR ]] || { printf "$ERRFMT" "no permission to write to $OUTDIR" >&2 usage_message exit $E_OUTDIR } # use the profilename to find its directory by examining profiles.ini PROFDESC=$(grep -B 1 -A 2 "^Name=$PROFNAME[[:cntrl:]]\?$" "$FXSETDIR/profiles.ini") [[ -z $PROFDESC ]] && { printf "$ERRFMT" \ "cannot find profile '$PROFNAME'" \ "to verify the name, run firefox -ProfileManager" >&2 usage_message exit $E_PROF } [[ $(echo "$PROFDESC" | wc -l) -ne 4 ]] && { printf "$ERRFMT" \ "cannot determine profile directory to back up" \ " please debug me now " >&2 exit $E_PROF } RELFLAG=$(echo "$PROFDESC" | sed -n '/^IsRelative=\([01]\)/{s~^IsRelative=\([01]\)[[:cntrl:]]\?$~\1~; p}') [[ $RELFLAG -eq 1 ]] && PROFDIR=$FXSETDIR/$(echo "$PROFDESC" | sed -n '\~^Path=~{s~Path=~~; p}') || PROFDIR=$(echo "$PROFDESC" | sed -n '\~^Path=~{s~Path=~~; p}') # if under cygwin, change path to POSIXness if [[ $OSTYPE == *cygwin* ]]; then PROFDIR=$(cygpath "$PROFDIR") || { printf "$ERRFMT" "error using cygpath under cygwin" \ "please make sure cygpath is installed" >&2 exit $E_CYG } fi [[ ! -d $PROFDIR ]] && { printf "$ERRFMT" \ "cannot determine profile to back up" \ " please debug me now " >&2 exit $E_PROF } [[ -h $PROFDIR/lock ]] || [[ -e $PROFDIR/lock ]] || [[ -e $PROFDIR/parent.lock ]] && { printf "$ERRFMT" \ "profile '$PROFNAME' is locked" \ "try again after killing any firefoxen" >&2 exit $E_PROF } # generate name for backup file BACKUPCOUNT=1 BACKUPFILE=$OUTDIR/${PROFDIR##*/}.$TODAY$TAG.tar.gz while [[ -e $BACKUPFILE ]]; do BACKUPFILE=$OUTDIR/${PROFDIR##*/}.$TODAY$TAG-$BACKUPCOUNT.tar.gz BACKUPCOUNT=$((BACKUPCOUNT+1)) done # make the tar.gz, check tar's exit code, and be done cd "${PROFDIR%/*}" || { printf "$ERRFMT" "cannot enter ${PROFDIR%/*}" >&2 exit $E_PROF } tar $TARVERB --gzip --preserve --create --file="$BACKUPFILE" --exclude=Cache* "${PROFDIR##*/}" [[ $OSTYPE == *cygwin* ]] && BACKUPFILE=$(cygpath -m "$BACKUPFILE") printf "$NFOFMT" \ "The Firefox profile '$PROFNAME' has been backed up to" \ "$BACKUPFILE" exit 0 ################################################################## ## ## ## This script is provided as-is, with no warranty. Use it ## ## however you want. No rights are reserved. ## ## ## ## If you improve the script, please mail your changes to ## ## boxcars at gmx dot net. ## ## ## ##################################################################