scripts

shell scripts
git clone git://git.bitsmanent.org/scripts
Log | Files | Refs | README

mkbkp (1295B)


      1 #!/bin/sh
      2 # Copy $NAMES files and directory, clone the $GITREPOS it repositories, and put
      3 # everything into $BKPDIR (default CWD).
      4 #
      5 # Usage: ./mkbkp [config file]
      6 # Using sudo: sudo -Eu $(id -un) ./mkbkp [config file]
      7 
      8 # enabled supports
      9 SUPPORT="
     10 bkp_names
     11 bkp_sunames
     12 bkp_gitrepos
     13 "
     14 
     15 OPWD="$(pwd)"
     16 NEWLINE='
     17 '
     18 
     19 bkp_names() {
     20 	setdir "names"
     21 	mexec "$NAMES" "cp -rf %s ."
     22 }
     23 
     24 bkp_sunames() {
     25 	setdir "sunames"
     26 	mexec "$SUNAMES" "sudo cp -rf %s ."
     27 }
     28 
     29 bkp_gitrepos() {
     30 	setdir "gitrepos"
     31 	mexec "$GITREPOS" "git clone %s"
     32 }
     33 
     34 die() {
     35 	echo "$@"
     36 	exit 1
     37 }
     38 
     39 setdir() {
     40 	dir="$1"
     41 	[ "$dir" != "$BKPDIR" ] && cd "${OPWD}/${BKPDIR}"
     42 	if [ ! -d "$dir" ]; then
     43 		mkdir -p "$dir"
     44 		rv=$?
     45 		[ $rv -ne 0 ] && die "${dir}: cannot create directory"
     46 	fi
     47 	cd "$dir"
     48 }
     49 
     50 mexec() {
     51 	list="$1"
     52 	cmd="$2"
     53 	[ -z "$list" -o -z "$cmd" ] && return
     54 	for i in $list; do
     55 		c="$(printf "$cmd" "$i")"
     56 		c="${c} 2>&1 > /dev/null"
     57 		eval "$c" 2>&1 > /dev/null
     58 	done
     59 }
     60 
     61 isfunc() {
     62 	type "$1" |grep -q function
     63 }
     64 
     65 main() {
     66 	cfg="$1"
     67 	[ -z "$cfg" ] && cfg=~/.mkbkprc
     68 	[ ! -e "$cfg" ] && die "${cfg}: file not found"
     69 	. "$cfg"
     70 
     71 	setdir "$BKPDIR"
     72 	echo -n "Backing up into ${BKPDIR}..."
     73 
     74 	IFS="$NEWLINE"
     75 	for s in $SUPPORT; do
     76 		$s
     77 	done
     78 	unset IFS
     79 
     80 	cd "$OPWD"
     81 	echo " done."
     82 
     83 	isfunc "onfinish" && onfinish "$BKPDIR"
     84 }
     85 
     86 main "$@"