franta-hg@0
|
1 |
#!/bin/bash
|
franta-hg@0
|
2 |
|
franta-hg@0
|
3 |
# VCS Backup
|
franta-hg@0
|
4 |
# Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info)
|
franta-hg@0
|
5 |
#
|
franta-hg@0
|
6 |
# This program is free software: you can redistribute it and/or modify
|
franta-hg@0
|
7 |
# it under the terms of the GNU General Public License as published by
|
franta-hg@0
|
8 |
# the Free Software Foundation, either version 3 of the License, or
|
franta-hg@0
|
9 |
# (at your option) any later version.
|
franta-hg@0
|
10 |
#
|
franta-hg@0
|
11 |
# This program is distributed in the hope that it will be useful,
|
franta-hg@0
|
12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
franta-hg@0
|
13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
franta-hg@0
|
14 |
# GNU General Public License for more details.
|
franta-hg@0
|
15 |
#
|
franta-hg@0
|
16 |
# You should have received a copy of the GNU General Public License
|
franta-hg@0
|
17 |
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
franta-hg@0
|
18 |
|
franta-hg@0
|
19 |
|
franta-hg@0
|
20 |
# Server-side configuration:
|
franta-hg@0
|
21 |
VCS_BACKUP_DATA_DIR="/mnt/data";
|
franta-hg@0
|
22 |
VCS_BACKUP_CURRENT_DIR="$VCS_BACKUP_DATA_DIR/current";
|
franta-hg@2
|
23 |
VCS_BACKUP_PUBLIC_DIR="$VCS_BACKUP_DATA_DIR/public";
|
franta-hg@0
|
24 |
VCS_BACKUP_CONFIG_DIR="$VCS_BACKUP_DATA_DIR/config";
|
franta-hg@0
|
25 |
VCS_BACKUP_SNAPSHOT_DIR="$VCS_BACKUP_DATA_DIR/snapshot";
|
franta-hg@0
|
26 |
VCS_BACKUP_SUBVOLUME_SOCKET="/run/vcs-backup-subvolume";
|
franta-hg@7
|
27 |
VCS_BACKUP_CLONE_SOCKET="/run/vcs-backup-clone/socket"; # the directory will be writable by ${VCS_BACKUP_USER}
|
franta-hg@3
|
28 |
VCS_BACKUP_CLONE_CALLBACK_SOCKET="clone-callback";
|
franta-hg@0
|
29 |
VCS_BACKUP_USER="vcs-backup";
|
franta-hg@0
|
30 |
VCS_BACKUP_MANAGER="vcs-backup-manager";
|
franta-hg@0
|
31 |
|
franta-hg@0
|
32 |
# Installation – check and do it by hand:
|
franta-hg@0
|
33 |
# There should be already mounted Btrfs at $VCS_BACKUP_DATA_DIR
|
franta-hg@0
|
34 |
installInstructions() {
|
franta-hg@0
|
35 |
cp vcs-backup.sh /usr/local/bin/
|
franta-hg@0
|
36 |
adduser --disabled-password "$VCS_BACKUP"
|
franta-hg@0
|
37 |
adduser --disabled-password "$VCS_BACKUP_MANAGER"
|
franta-hg@0
|
38 |
|
franta-hg@0
|
39 |
mkdir "$VCS_BACKUP_CURRENT_DIR";
|
franta-hg@0
|
40 |
mkdir "$VCS_BACKUP_CONFIG_DIR";
|
franta-hg@0
|
41 |
mkdir "$VCS_BACKUP_SNAPSHOT_DIR";
|
franta-hg@5
|
42 |
mkdir "$(dirname VCS_BACKUP_CLONE_SOCKET)"
|
franta-hg@0
|
43 |
|
franta-hg@5
|
44 |
chown "${VCS_BACKUP_USER}:${VCS_BACKUP_USER}" "$(dirname VCS_BACKUP_CLONE_SOCKET)"
|
franta-hg@0
|
45 |
chown "${VCS_BACKUP_MANAGER}:${VCS_BACKUP_MANAGER}" "$VCS_BACKUP_CONFIG_DIR"
|
franta-hg@0
|
46 |
}
|
franta-hg@0
|
47 |
|
franta-hg@0
|
48 |
|
franta-hg@0
|
49 |
# --- Private functions: ---------------------------------------------------------------------------
|
franta-hg@0
|
50 |
|
franta-hg@0
|
51 |
# Environment: all
|
franta-hg@0
|
52 |
# $1 = VCS type: hg, git
|
franta-hg@0
|
53 |
# $2 = URL
|
franta-hg@0
|
54 |
isValidTypeAndURL() { ([[ "$1" == "hg" || "$1" == "git" ]]) && [[ $(echo "$2" | wc -l) == 1 ]] && [[ $(echo "$2" | grep -E '^(http|https|ssh)://([a-zA-Z0-9_-][a-zA-Z0-9_-.]*/?)+$' | wc -l) == 1 ]]; }
|
franta-hg@0
|
55 |
|
franta-hg@0
|
56 |
# Environment: all
|
franta-hg@0
|
57 |
# $1 = path to the config file
|
franta-hg@5
|
58 |
loadConfigFile() { if [ -f "$1" ]; then . "$1"; fi }
|
franta-hg@0
|
59 |
|
franta-hg@0
|
60 |
# Environment: server
|
franta-hg@0
|
61 |
# $1 = URL
|
franta-hg@0
|
62 |
urlToRelativeDirectoryPath() {
|
franta-hg@0
|
63 |
echo "$1" | sed -E 's@^[^:]+://@@g';
|
franta-hg@0
|
64 |
}
|
franta-hg@0
|
65 |
|
franta-hg@9
|
66 |
# Environment: all
|
franta-hg@9
|
67 |
# $1 = optional value (if missing, reads STDIN)
|
franta-hg@9
|
68 |
escapeRecfileValue() { if [[ $# = 0 ]]; then awk '{ if (NR > 1) { printf "+ " } print $_ }'; else echo "${@}" | ${FUNCNAME[0]}; fi }
|
franta-hg@9
|
69 |
|
franta-hg@9
|
70 |
# Environment: all
|
franta-hg@9
|
71 |
# $1 = key
|
franta-hg@9
|
72 |
# $2 = value
|
franta-hg@9
|
73 |
printRecfileKeyValue() { echo -n "$1: "; escapeRecfileValue "$2"; }
|
franta-hg@9
|
74 |
|
franta-hg@0
|
75 |
# --- Public interface functions: ------------------------------------------------------------------
|
franta-hg@0
|
76 |
|
franta-hg@0
|
77 |
# Environment: client
|
franta-hg@0
|
78 |
# $1 = VCS type: hg, git
|
franta-hg@0
|
79 |
# $2 = URL
|
franta-hg@2
|
80 |
# $3 = "public" or "private" (default), whether the repository should be available through the public web interface
|
franta-hg@10
|
81 |
# $4 = "clone" (optional), if present, will also clone the backup locally
|
franta-hg@0
|
82 |
vcs_backup_public_clientSubmitBackupRequest() {
|
franta-hg@0
|
83 |
if isValidTypeAndURL "$1" "$2"; then
|
franta-hg@0
|
84 |
loadConfigFile ~/.config/vcs-backup/client.cfg
|
franta-hg@3
|
85 |
${VCS_BACKUP_SSH_COMMAND[@]} vcs-backup.sh serverSubmitBackupRequest "$1" "$2" "$3" "$4"
|
franta-hg@3
|
86 |
if [[ "$4" == "clone" ]]; then
|
franta-hg@10
|
87 |
if [[ "$1" == "hg" ]]; then hg clone "ssh://${VCS_BACKUP_SERVER}/$VCS_BACKUP_CURRENT_DIR/$1/$(urlToRelativeDirectoryPath $2)";
|
franta-hg@10
|
88 |
elif [[ "$1" == "git" ]]; then git clone "ssh://${VCS_BACKUP_SERVER}/$VCS_BACKUP_CURRENT_DIR/$1/$(urlToRelativeDirectoryPath $2)";
|
franta-hg@3
|
89 |
fi
|
franta-hg@3
|
90 |
fi
|
franta-hg@0
|
91 |
else
|
franta-hg@0
|
92 |
echo "Unsupported VCS type: '$1' or URL: '$2'" >&2;
|
franta-hg@0
|
93 |
fi
|
franta-hg@0
|
94 |
}
|
franta-hg@0
|
95 |
|
franta-hg@0
|
96 |
# Environment: server
|
franta-hg@5
|
97 |
# User: $VCS_BACKUP_MANAGER
|
franta-hg@2
|
98 |
# has same parameters as clientSubmitBackupRequest (see above)
|
franta-hg@0
|
99 |
vcs_backup_public_serverSubmitBackupRequest() {
|
franta-hg@0
|
100 |
if isValidTypeAndURL "$1" "$2"; then
|
franta-hg@0
|
101 |
loadConfigFile "/etc/vcs-backup/server.cfg";
|
franta-hg@0
|
102 |
relativePath=$1/$(urlToRelativeDirectoryPath "$2");
|
franta-hg@0
|
103 |
absolutePath="$VCS_BACKUP_CONFIG_DIR/$relativePath";
|
franta-hg@0
|
104 |
mkdir -p "$absolutePath";
|
franta-hg@0
|
105 |
echo "$2" > "$absolutePath/url.txt"
|
franta-hg@0
|
106 |
echo "submited" > "$absolutePath/state.txt"
|
franta-hg@0
|
107 |
setfacl -m u:${VCS_BACKUP_USER}:r "$absolutePath/url.txt"
|
franta-hg@0
|
108 |
setfacl -m u:${VCS_BACKUP_USER}:rw "$absolutePath/state.txt"
|
franta-hg@0
|
109 |
echo "$relativePath" | socat -u - unix-send:${VCS_BACKUP_SUBVOLUME_SOCKET};
|
franta-hg@3
|
110 |
|
franta-hg@2
|
111 |
if [[ "$3" == "public" ]]; then
|
franta-hg@2
|
112 |
cd "$VCS_BACKUP_PUBLIC_DIR";
|
franta-hg@2
|
113 |
mkdir -p "$(dirname $relativePath)";
|
franta-hg@2
|
114 |
ln -rs "../current/$relativePath" "$(dirname $relativePath)";
|
franta-hg@2
|
115 |
fi
|
franta-hg@3
|
116 |
|
franta-hg@3
|
117 |
if [[ "$4" == "clone" ]]; then
|
franta-hg@3
|
118 |
socat -u "unix-recvfrom:$absolutePath/${VCS_BACKUP_CLONE_CALLBACK_SOCKET},mode=777" - | while read m; do # TODO: ,group=${VCS_BACKUP_USER} and no 777
|
franta-hg@3
|
119 |
echo "Message from the clone service: $m";
|
franta-hg@3
|
120 |
done;
|
franta-hg@3
|
121 |
fi
|
franta-hg@0
|
122 |
else
|
franta-hg@0
|
123 |
echo "Unsupported VCS type: '$1' or URL: '$2'" >&2;
|
franta-hg@0
|
124 |
fi
|
franta-hg@0
|
125 |
}
|
franta-hg@0
|
126 |
|
franta-hg@0
|
127 |
# Environment: server
|
franta-hg@5
|
128 |
# User: root
|
franta-hg@0
|
129 |
# Should be started as a systemd/init service.
|
franta-hg@0
|
130 |
# - reads messages from from the subvolume socket – message contains the relative directory path
|
franta-hg@0
|
131 |
# - creates a subvolume for given repository + necesary parent directories
|
franta-hg@0
|
132 |
# - sends a message to the clone service → start cloning into the created subvolume
|
franta-hg@0
|
133 |
vcs_backup_public_serverStartSubvolumeService() {
|
franta-hg@0
|
134 |
socat -u "unix-recv:${VCS_BACKUP_SUBVOLUME_SOCKET},group=${VCS_BACKUP_MANAGER},mode=770" - | while read d; do
|
franta-hg@0
|
135 |
mkdir -p $(dirname "$VCS_BACKUP_CURRENT_DIR/$d");
|
franta-hg@0
|
136 |
btrfs subvolume create "$VCS_BACKUP_CURRENT_DIR/$d" && \
|
franta-hg@0
|
137 |
echo "subvolumeCreated" > "$VCS_BACKUP_CONFIG_DIR/$d/state.txt" && \
|
franta-hg@0
|
138 |
chown "${VCS_BACKUP_USER}:${VCS_BACKUP_USER}" "$VCS_BACKUP_CURRENT_DIR/$d" && \
|
franta-hg@0
|
139 |
echo "$d" | socat -u - unix-send:${VCS_BACKUP_CLONE_SOCKET};
|
franta-hg@0
|
140 |
done
|
franta-hg@0
|
141 |
}
|
franta-hg@0
|
142 |
|
franta-hg@0
|
143 |
# Environment: server
|
franta-hg@5
|
144 |
# User: $VCS_BACKUP_USER
|
franta-hg@0
|
145 |
# should be started as a systemd/init service
|
franta-hg@0
|
146 |
vcs_backup_public_serverStartCloneService() {
|
franta-hg@0
|
147 |
socat -u "unix-recv:${VCS_BACKUP_CLONE_SOCKET},mode=700" - | while read d; do
|
franta-hg@2
|
148 |
vcsType=$(echo "$d" | sed 's@/.*@@g');
|
franta-hg@0
|
149 |
url=$(cat "$VCS_BACKUP_CONFIG_DIR/$d/url.txt");
|
franta-hg@3
|
150 |
|
franta-hg@0
|
151 |
if isValidTypeAndURL "$vcsType" "$url"; then
|
franta-hg@8
|
152 |
if [[ "$vcsType" == "hg" ]]; then hg clone -U "$url" "$VCS_BACKUP_CURRENT_DIR/$d";
|
franta-hg@8
|
153 |
elif [[ "$vcsType" == "git" ]]; then git clone --mirror "$url" "$VCS_BACKUP_CURRENT_DIR/$d";
|
franta-hg@0
|
154 |
fi && echo "cloned" > "$VCS_BACKUP_CONFIG_DIR/$d/state.txt";
|
franta-hg@0
|
155 |
else
|
franta-hg@0
|
156 |
echo "Unsupported VCS type: '$vcsType' or URL: '$url'" >&2;
|
franta-hg@0
|
157 |
fi
|
franta-hg@3
|
158 |
|
franta-hg@3
|
159 |
callBackSocket="$VCS_BACKUP_CONFIG_DIR/$d/$VCS_BACKUP_CLONE_CALLBACK_SOCKET";
|
franta-hg@3
|
160 |
if [[ -e "$callBackSocket" ]]; then
|
franta-hg@3
|
161 |
echo "done" | socat -u - unix-send:"$callBackSocket";
|
franta-hg@3
|
162 |
fi
|
franta-hg@0
|
163 |
done
|
franta-hg@0
|
164 |
}
|
franta-hg@0
|
165 |
|
franta-hg@7
|
166 |
# Environment: client
|
franta-hg@7
|
167 |
# prints list of repositories in recfile format
|
franta-hg@7
|
168 |
# usage example: vcs-backup.sh clientListRepositories | relpipe-in-recfile | relpipe-out-tabular
|
franta-hg@7
|
169 |
vcs_backup_public_clientListRepositories() {
|
franta-hg@7
|
170 |
loadConfigFile ~/.config/vcs-backup/client.cfg;
|
franta-hg@7
|
171 |
${VCS_BACKUP_SSH_COMMAND[@]} vcs-backup.sh serverListRepositories;
|
franta-hg@7
|
172 |
}
|
franta-hg@7
|
173 |
|
franta-hg@7
|
174 |
# Environment: server
|
franta-hg@7
|
175 |
# User: $VCS_BACKUP_MANAGER
|
franta-hg@7
|
176 |
vcs_backup_public_serverListRepositories() {
|
franta-hg@7
|
177 |
echo "%rec: repositories";
|
franta-hg@7
|
178 |
echo "%type: bytes int";
|
franta-hg@7
|
179 |
echo "%type: public bool";
|
franta-hg@7
|
180 |
echo;
|
franta-hg@7
|
181 |
|
franta-hg@7
|
182 |
find "$VCS_BACKUP_CONFIG_DIR" -name url.txt -printf '%P\n' | sort | xargs dirname | while read d; do
|
franta-hg@7
|
183 |
url=$(cat "$VCS_BACKUP_CONFIG_DIR/$d/url.txt");
|
franta-hg@7
|
184 |
state=$(cat "$VCS_BACKUP_CONFIG_DIR/$d/state.txt");
|
franta-hg@7
|
185 |
vcsType=$(echo "$d" | sed 's@/.*@@g');
|
franta-hg@7
|
186 |
sizeBytes=$(du -sb "$VCS_BACKUP_CURRENT_DIR/$d" | cut -f1);
|
franta-hg@7
|
187 |
[[ -e "$VCS_BACKUP_PUBLIC_DIR/$d" ]] && public="true" || public="false";
|
franta-hg@7
|
188 |
|
franta-hg@7
|
189 |
if [[ "$vcsType" == "hg" ]]; then lastCommit=$(hg log --limit 1 --template '{date|isodatesec}' -R "$VCS_BACKUP_CURRENT_DIR/$d" 2>/dev/null);
|
franta-hg@7
|
190 |
elif [[ "$vcsType" == "git" ]]; then lastCommit=$(git -C "$VCS_BACKUP_CURRENT_DIR/$d" log --max-count=1 --pretty="%ai");
|
franta-hg@7
|
191 |
else lastCommit=""; fi
|
franta-hg@7
|
192 |
|
franta-hg@9
|
193 |
printRecfileKeyValue "type" "$vcsType";
|
franta-hg@9
|
194 |
printRecfileKeyValue "url" "$url";
|
franta-hg@9
|
195 |
printRecfileKeyValue "state" "$state";
|
franta-hg@9
|
196 |
printRecfileKeyValue "public" "$public";
|
franta-hg@9
|
197 |
printRecfileKeyValue "serverPath" "$VCS_BACKUP_CURRENT_DIR/$d";
|
franta-hg@9
|
198 |
printRecfileKeyValue "size" "$sizeBytes";
|
franta-hg@9
|
199 |
printRecfileKeyValue "lastCommit" "$lastCommit";
|
franta-hg@7
|
200 |
echo;
|
franta-hg@7
|
201 |
done
|
franta-hg@7
|
202 |
}
|
franta-hg@7
|
203 |
|
franta-hg@6
|
204 |
# Environment: server
|
franta-hg@6
|
205 |
# User: $VCS_BACKUP_USER
|
franta-hg@6
|
206 |
# should be called from cron (usually every day)
|
franta-hg@6
|
207 |
vcs_backup_public_serverPullCronTask() {
|
franta-hg@11
|
208 |
echo "%rec: pull";
|
franta-hg@11
|
209 |
echo "%type: started date";
|
franta-hg@11
|
210 |
echo "%type: finished date";
|
franta-hg@11
|
211 |
echo "%type: duration int";
|
franta-hg@11
|
212 |
echo "%type: resultCode int";
|
franta-hg@11
|
213 |
|
franta-hg@11
|
214 |
find "$VCS_BACKUP_CONFIG_DIR" -name url.txt -printf '%P\n' | sort | xargs dirname | while read d; do
|
franta-hg@11
|
215 |
state=$(cat "$VCS_BACKUP_CONFIG_DIR/$d/state.txt");
|
franta-hg@11
|
216 |
vcsType=$(echo "$d" | sed 's@/.*@@g');
|
franta-hg@11
|
217 |
absolutePath="$VCS_BACKUP_CURRENT_DIR/$d";
|
franta-hg@11
|
218 |
|
franta-hg@11
|
219 |
|
franta-hg@11
|
220 |
pullStarted=$(date --iso-8601=s);
|
franta-hg@11
|
221 |
pullStartedMiliseconds=$(($(date +%s%N)/1000000));
|
franta-hg@11
|
222 |
pullFinished="";
|
franta-hg@11
|
223 |
pullDuration="";
|
franta-hg@11
|
224 |
pullResult="";
|
franta-hg@11
|
225 |
pullResultCode="";
|
franta-hg@11
|
226 |
if [[ "$state" == "cloned" ]]; then
|
franta-hg@11
|
227 |
if [[ "$vcsType" == "hg" ]]; then pullResult=$(hg pull --force --repository "$absolutePath" 2>&1); pullResultCode=$?;
|
franta-hg@11
|
228 |
elif [[ "$vcsType" == "git" ]]; then pullResult=$(git -C "$absolutePath" fetch 2>&1); pullResultCode=$?;
|
franta-hg@11
|
229 |
fi
|
franta-hg@11
|
230 |
pullFinished=$(date --iso-8601=s);
|
franta-hg@11
|
231 |
pullFinishedMiliseconds=$(($(date +%s%N)/1000000));
|
franta-hg@11
|
232 |
pullDuration=$(( $pullFinishedMiliseconds - $pullStartedMiliseconds ));
|
franta-hg@11
|
233 |
fi
|
franta-hg@11
|
234 |
|
franta-hg@11
|
235 |
printRecfileKeyValue "serverPath" "$absolutePath";
|
franta-hg@11
|
236 |
printRecfileKeyValue "type" "$vcsType";
|
franta-hg@11
|
237 |
printRecfileKeyValue "state" "$state";
|
franta-hg@11
|
238 |
printRecfileKeyValue "started" "$pullStarted";
|
franta-hg@11
|
239 |
printRecfileKeyValue "finished" "$pullFinished";
|
franta-hg@11
|
240 |
printRecfileKeyValue "duration" "$pullDuration";
|
franta-hg@11
|
241 |
printRecfileKeyValue "resultCode" "$pullResultCode";
|
franta-hg@11
|
242 |
printRecfileKeyValue "message" "$pullResult";
|
franta-hg@11
|
243 |
echo;
|
franta-hg@11
|
244 |
done
|
franta-hg@6
|
245 |
}
|
franta-hg@6
|
246 |
|
franta-hg@6
|
247 |
# Environment: server
|
franta-hg@11
|
248 |
# User: root
|
franta-hg@6
|
249 |
# should be called from cron (usually every day) after Pull (see above)
|
franta-hg@6
|
250 |
vcs_backup_public_serverSnapshotCronTask() {
|
franta-hg@6
|
251 |
return;
|
franta-hg@6
|
252 |
}
|
franta-hg@6
|
253 |
|
franta-hg@0
|
254 |
# --- Single entry-point: --------------------------------------------------------------------------
|
franta-hg@0
|
255 |
|
franta-hg@0
|
256 |
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin";
|
franta-hg@0
|
257 |
PUBLIC_FUNCTION_PREFIX="vcs_backup_public_";
|
franta-hg@0
|
258 |
if type -t "$PUBLIC_FUNCTION_PREFIX$1" > /dev/null; then
|
franta-hg@0
|
259 |
"$PUBLIC_FUNCTION_PREFIX${@:1}";
|
franta-hg@4
|
260 |
elif [[ $(basename $0) == "vcs-backup-clone-private-hg" ]]; then "${PUBLIC_FUNCTION_PREFIX}clientSubmitBackupRequest" hg "$1" private clone;
|
franta-hg@4
|
261 |
elif [[ $(basename $0) == "vcs-backup-clone-private-git" ]]; then "${PUBLIC_FUNCTION_PREFIX}clientSubmitBackupRequest" git "$1" private clone;
|
franta-hg@4
|
262 |
elif [[ $(basename $0) == "vcs-backup-clone-public-hg" ]]; then "${PUBLIC_FUNCTION_PREFIX}clientSubmitBackupRequest" hg "$1" public clone;
|
franta-hg@4
|
263 |
elif [[ $(basename $0) == "vcs-backup-clone-public-git" ]]; then "${PUBLIC_FUNCTION_PREFIX}clientSubmitBackupRequest" git "$1" public clone;
|
franta-hg@0
|
264 |
else
|
franta-hg@0
|
265 |
echo "Unsupported sub-command: $1" >&2
|
franta-hg@0
|
266 |
echo "Available sub-commands:" >&2
|
franta-hg@0
|
267 |
declare -F | grep "$PUBLIC_FUNCTION_PREFIX" | sed "s/.*$PUBLIC_FUNCTION_PREFIX/ /g" >&2
|
franta-hg@0
|
268 |
exit 1;
|
franta-hg@0
|
269 |
fi
|