-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup.sh
executable file
·157 lines (128 loc) · 3.34 KB
/
backup.sh
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/bin/bash
ARGS=`getopt -o "ifvhucd" --long "debug,create,interactive,force,version,verbose,help,usage,profile:,target-device:,target-device-path:" -n "$0" -- "$@"`
ARGSERR=$?
if [ $ARGSERR -ne "0" ]; then
echo "Error"
exit 1;
fi
eval set -- "$ARGS"
#Default options values
INTERACTIVE=false
FORCE=false
VERBOSE=false
PROFILE=""
CREATE=false
DEBUG=false
TARGET_DEVICE=""
TARGET_DEVICE_PATH=""
#Load config.
SCRIPT_DIR=`dirname $0`
SCRIPT_DIR=`cd $SCRIPT_DIR && pwd`
. $SCRIPT_DIR/config.conf
#Helper funxtions
function usage() {
echo "Usage: $0 [-ifvhuc] `hostname`"
}
function show_help() {
usage;
echo "HELP TEXT"
}
function check_profile() {
if [ $PROFILE ] && [ -d "$SCRIPT_DIR/$PROFILE" ] && [ -f "$SCRIPT_DIR/$PROFILE/config.conf" ]; then
echo "Config found";
else
echo "$SCRIPT_DIR/$PROFILE/config.conf not found"
unset -v PROFILE
fi
}
function set_profile() {
check_profile;
while [ ! $PROFILE ] ; do
if $INTERACTIVE ; then
if $CREATE ; then
echo create profile
else
echo "Use \"`hostname`\"? [y/n]"
while true ; do
read ANSW;
case $ANSW in
"y") PROFILE="`hostname`"; break ;;
"n") break; ;;
*) echo "Write \"y\" or \"n\"" ;;
esac
done
check_profile;
if [ ! $PROFILE ] ; then
echo Ask for existing profile
echo "Press CTRL+C to cancel"
read PROFILE;
check_profile;
fi
fi #End create
else #Not interactive
echo "Error, no profile selected"
exit 1;
fi
done #End
if [ ! $PROFILE ] ; then
exit 1;
fi
}
function check_target_device_path() {
if [ -h $TARGET_DEVICE ]; then
TARGET_DEVICE=`readlink -f "$TARGETDEVICE"`
fi
TARGET_DEVICE_MOUNT=$(awk -v d=$DEVICE '$1 ~ d { print $2 }' < /proc/mounts)
if [ ! $TARGER_DEVICE_MOUNT ] ; then
return 1;
else
echo "Device mounted to $TARGET_DEVICE_MOUNT"
fi
}
while true ; do
case "$1" in
-i|--interactive) INTERACTIVE=true ; shift ;;
-f|--force) FORCE=true ; shift ;;
-v|--verbose) VERBOSE=true ; shift ;;
-h|--help) show_help; exit 0; shift ;;
-u|--usage) usage; exit 0; shift ;;
-c|--create) CREATE=true ; shift ;;
-d|--debug) DEBUG=true ; shift ;;
--profile) PROFILE=$2 ; shift 2;;
--target-device) TARGET_DEVICE=$2 ; shift 2;;
--target-device-path) TARGET_DEVICE_PATH=$2; shift 2;;
--) shift ; break ;;
*) echo "Error!" ; exit 1 ;;
esac
done
#Validtion
if [ $1 ] ; then PROFILE=$1; shift; fi
if $DEBUG ; then
echo "Remaining arguments:";
for arg do echo '--> '"\`$arg'" ; done
fi
set_profile;
echo "Using $PROFILE profile."
if [ $? != 0 ] ; then echo "Error"; exit 1 ; fi
if $DEBUG ; then exit 1; fi
. $SCRIPT_DIR/$PROFILE/config.conf
if [ ! -d "$BUDESTDIR" ]; then
echo "Dest dir "$BUDESTDIR" not exist"
exit 1;
fi
EXCLUDES="$SCRIPT_DIR/$PROFILE/exclude.list"
BUDEST="$BUDESTDIR/$PROFILE"
BUDIR=`date +%Y-%m-%d`
DEST="$BUDEST/$BUDIR"
BUOPTS="--force --ignore-errors --delete-excluded --exclude-from=$EXCLUDES
--delete --backup --backup-dir=$DEST -a -R -v"
while read source
do
SOURCE="$source"
CURRENT="$BUDEST/current"
echo "$SOURCE > $DEST"
if ( ! $DEBUG ); then
#Sudo required for system files backups
rsync $BUOPTS $SOURCE $CURRENT
fi
done < $SCRIPT_DIR/$PROFILE/sources.list