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