-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackupRepositories.sh
More file actions
156 lines (144 loc) · 3.72 KB
/
backupRepositories.sh
File metadata and controls
156 lines (144 loc) · 3.72 KB
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
#!/bin/bash
helpFunction()
{
echo ""
echo "usage: $0 -r <backup | add | remove> [-v <repository-url>]"
echo " [-c <relative-file-path>]"
echo " [-d <relative-folder-path>]"
echo ""
echo "usage: $0 --run <backup | add | remove> [--value <repository-url>]"
echo " [--config-file <relative-file-path>]"
echo " [--target-directory <relative-folder-path>]"
exit 1
}
# default values
basePath="$(pwd)"
command=""
commandValue=""
configFile=""
backupPath=""
# read options
if options="$(getopt -o r:v:c:d:h -l run:,value:,config-file:,target-directory:,help -- "$@")"; then
eval set -- "$options"
while true
do
case "${1,,}" in
-r|--run)
if [ -z $command ]; then
command="$2"
else
echo "CONFLICTING OPTIONS FOR -r"
helpFunction
fi
shift # skip argument
;;
-v|--value)
if [ -z $commandValue ]; then
commandValue="$2"
else
echo "CONFLICTING OPTIONS FOR -v"
helpFunction
fi
shift # skip argument
;;
-c|--config-file)
if [ -z $configFile ]; then
configFile="$2"
else
echo "CONFLICTING OPTIONS FOR -c"
helpFunction
fi
shift # skip argument
;;
-d|--target-directory)
if [ -z $backupPath ]; then
backupPath="$2"
else
echo "CONFLICTING OPTIONS FOR -d"
helpFunction
fi
shift # skip argument
;;
-h|--help) helpFunction ;;
--)
shift
break
;;
*) helpFunction ;;
esac
shift
done
fi
# validate options
if [ -z "$command" ]; then
echo "MISSING COMMAND: backup | add | remove"
helpFunction
fi
if [[ ("$command" == "add" || "$command" == "remove") && -z "$commandValue" ]]; then
echo "MISSING VALUE: repository-url"
helpFunction
fi
if [ -z "$configFile" ]; then
configFile="./backupRepositories.config"
fi
if [ -z "$backupPath" ]; then
backupPath="./"
fi
if [ ! -f "$configFile" ]; then
echo "CONFIG FILE NOT FOUND: $configFile"
exit 1
fi
# run the script
echo "========== ARGUMENTS =========="
echo "base directory: $basePath"
echo "config file: $configFile"
echo "target directory: $backupPath"
if [ "$command" == "add" ]; then
if grep -q "$commandValue" "$configFile"; then
echo "$configFile already contains $commandValue"
echo "========== CONFIG =========="
cat "$configFile"
else
echo "ADDING $commandValue TO $configFile"
echo "$commandValue" >> "$configFile"
echo "========== NEW CONFIG =========="
cat "$configFile"
fi
fi
if [ "$command" == "remove" ]; then
if grep -q "$commandValue" "$configFile"; then
echo "REMOVING $commandValue FROM $configFile"
grep -v -xF -e "$commandValue" "$configFile" > "${configFile}.tmp" && mv "${configFile}.tmp" "$configFile"
echo "========== NEW CONFIG =========="
cat "$configFile"
else
echo "$configFile does not contain $commandValue"
echo "========== CONFIG =========="
cat "$configFile"
fi
fi
if [ "$command" == "backup" ]; then
echo "========== CONFIG =========="
cat "$configFile"
echo "========== RUNNING BACKUP =========="
while IFS= read -r line
do
mkdir -p "$backupPath"
cd "$backupPath"
repoDirectory=$(basename "$line")
if [ ! -d "$repoDirectory" ]; then
echo ""
echo "> CLONING $line to $(pwd)/$repoDirectory"
git clone "$line"
else
echo ""
echo "> PULLING $line to $(pwd)/$repoDirectory"
cd "$repoDirectory"
git pull
cd ..
fi
cd "$basePath"
done < "$configFile"
echo "========== BACKUP FINISHED =========="
fi
exit 0