4 # Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info)
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 # Server-side configuration:
21 VCS_BACKUP_DATA_DIR="/mnt/data";
22 VCS_BACKUP_CURRENT_DIR="$VCS_BACKUP_DATA_DIR/current";
23 VCS_BACKUP_PUBLIC_DIR="$VCS_BACKUP_DATA_DIR/public";
24 VCS_BACKUP_CONFIG_DIR="$VCS_BACKUP_DATA_DIR/config";
25 VCS_BACKUP_SNAPSHOT_DIR="$VCS_BACKUP_DATA_DIR/snapshot";
26 VCS_BACKUP_SUBVOLUME_SOCKET="/run/vcs-backup-subvolume";
27 VCS_BACKUP_CLONE_SOCKET="/run/vcs-backup-clone/socket"; # the directory will be writable by ${VCS_BACKUP_USER}
28 VCS_BACKUP_CLONE_CALLBACK_SOCKET="clone-callback";
29 VCS_BACKUP_USER="vcs-backup";
30 VCS_BACKUP_MANAGER="vcs-backup-manager";
32 # Installation – check and do it by hand:
33 # There should be already mounted Btrfs at $VCS_BACKUP_DATA_DIR
34 installInstructions() {
35 cp vcs-backup.sh /usr/local/bin/
36 adduser --disabled-password "$VCS_BACKUP"
37 adduser --disabled-password "$VCS_BACKUP_MANAGER"
39 mkdir "$VCS_BACKUP_CURRENT_DIR";
40 mkdir "$VCS_BACKUP_CONFIG_DIR";
41 mkdir "$VCS_BACKUP_SNAPSHOT_DIR";
42 mkdir "$(dirname VCS_BACKUP_CLONE_SOCKET)"
44 chown "${VCS_BACKUP_USER}:${VCS_BACKUP_USER}" "$(dirname VCS_BACKUP_CLONE_SOCKET)"
45 chown "${VCS_BACKUP_MANAGER}:${VCS_BACKUP_MANAGER}" "$VCS_BACKUP_CONFIG_DIR"
49 # --- Private functions: ---------------------------------------------------------------------------
52 # $1 = VCS type: hg, git
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 ]]; }
57 # $1 = path to the config file
58 loadConfigFile() { if [ -f "$1" ]; then . "$1"; fi }
62 urlToRelativeDirectoryPath() {
63 echo "$1" | sed -E 's@^[^:]+://@@g';
67 # $1 = optional value (if missing, reads STDIN)
68 escapeRecfileValue() { if [[ $# = 0 ]]; then awk '{ if (NR > 1) { printf "+ " } print $_ }'; else echo "${@}" | ${FUNCNAME[0]}; fi }
73 printRecfileKeyValue() { echo -n "$1: "; escapeRecfileValue "$2"; }
75 # --- Public interface functions: ------------------------------------------------------------------
78 # $1 = VCS type: hg, git
80 # $3 = "public" or "private" (default), whether the repository should be available through the public web interface
81 # $4 = "clone" (optional), if present, will also clone the backup locally
82 vcs_backup_public_clientSubmitBackupRequest() {
83 if isValidTypeAndURL "$1" "$2"; then
84 loadConfigFile ~/.config/vcs-backup/client.cfg
85 ${VCS_BACKUP_SSH_COMMAND[@]} vcs-backup.sh serverSubmitBackupRequest "$1" "$2" "$3" "$4"
86 if [[ "$4" == "clone" ]]; then
87 if [[ "$1" == "hg" ]]; then hg clone "ssh://${VCS_BACKUP_SERVER}/$VCS_BACKUP_CURRENT_DIR/$1/$(urlToRelativeDirectoryPath $2)";
88 elif [[ "$1" == "git" ]]; then git clone "ssh://${VCS_BACKUP_SERVER}/$VCS_BACKUP_CURRENT_DIR/$1/$(urlToRelativeDirectoryPath $2)";
92 echo "Unsupported VCS type: '$1' or URL: '$2'" >&2;
97 # User: $VCS_BACKUP_MANAGER
98 # has same parameters as clientSubmitBackupRequest (see above)
99 vcs_backup_public_serverSubmitBackupRequest() {
100 if isValidTypeAndURL "$1" "$2"; then
101 loadConfigFile "/etc/vcs-backup/server.cfg";
102 relativePath=$1/$(urlToRelativeDirectoryPath "$2");
103 absolutePath="$VCS_BACKUP_CONFIG_DIR/$relativePath";
104 mkdir -p "$absolutePath";
105 echo "$2" > "$absolutePath/url.txt"
106 echo "submited" > "$absolutePath/state.txt"
107 setfacl -m u:${VCS_BACKUP_USER}:r "$absolutePath/url.txt"
108 setfacl -m u:${VCS_BACKUP_USER}:rw "$absolutePath/state.txt"
109 echo "$relativePath" | socat -u - unix-send:${VCS_BACKUP_SUBVOLUME_SOCKET};
111 if [[ "$3" == "public" ]]; then
112 cd "$VCS_BACKUP_PUBLIC_DIR";
113 mkdir -p "$(dirname $relativePath)";
114 ln -rs "../current/$relativePath" "$(dirname $relativePath)";
117 if [[ "$4" == "clone" ]]; then
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
119 echo "Message from the clone service: $m";
123 echo "Unsupported VCS type: '$1' or URL: '$2'" >&2;
127 # Environment: server
129 # Should be started as a systemd/init service.
130 # - reads messages from from the subvolume socket – message contains the relative directory path
131 # - creates a subvolume for given repository + necesary parent directories
132 # - sends a message to the clone service → start cloning into the created subvolume
133 vcs_backup_public_serverStartSubvolumeService() {
134 socat -u "unix-recv:${VCS_BACKUP_SUBVOLUME_SOCKET},group=${VCS_BACKUP_MANAGER},mode=770" - | while read d; do
135 mkdir -p $(dirname "$VCS_BACKUP_CURRENT_DIR/$d");
136 btrfs subvolume create "$VCS_BACKUP_CURRENT_DIR/$d" && \
137 echo "subvolumeCreated" > "$VCS_BACKUP_CONFIG_DIR/$d/state.txt" && \
138 chown "${VCS_BACKUP_USER}:${VCS_BACKUP_USER}" "$VCS_BACKUP_CURRENT_DIR/$d" && \
139 echo "$d" | socat -u - unix-send:${VCS_BACKUP_CLONE_SOCKET};
143 # Environment: server
144 # User: $VCS_BACKUP_USER
145 # should be started as a systemd/init service
146 vcs_backup_public_serverStartCloneService() {
147 socat -u "unix-recv:${VCS_BACKUP_CLONE_SOCKET},mode=700" - | while read d; do
148 vcsType=$(echo "$d" | sed 's@/.*@@g');
149 url=$(cat "$VCS_BACKUP_CONFIG_DIR/$d/url.txt");
151 if isValidTypeAndURL "$vcsType" "$url"; then
152 if [[ "$vcsType" == "hg" ]]; then hg clone -U "$url" "$VCS_BACKUP_CURRENT_DIR/$d";
153 elif [[ "$vcsType" == "git" ]]; then git clone --mirror "$url" "$VCS_BACKUP_CURRENT_DIR/$d";
154 fi && echo "cloned" > "$VCS_BACKUP_CONFIG_DIR/$d/state.txt";
156 echo "Unsupported VCS type: '$vcsType' or URL: '$url'" >&2;
159 callBackSocket="$VCS_BACKUP_CONFIG_DIR/$d/$VCS_BACKUP_CLONE_CALLBACK_SOCKET";
160 if [[ -e "$callBackSocket" ]]; then
161 echo "done" | socat -u - unix-send:"$callBackSocket";
166 # Environment: client
167 # prints list of repositories in recfile format
168 # usage example: vcs-backup.sh clientListRepositories | relpipe-in-recfile | relpipe-out-tabular
169 vcs_backup_public_clientListRepositories() {
170 loadConfigFile ~/.config/vcs-backup/client.cfg;
171 ${VCS_BACKUP_SSH_COMMAND[@]} vcs-backup.sh serverListRepositories;
174 # Environment: server
175 # User: $VCS_BACKUP_MANAGER
176 vcs_backup_public_serverListRepositories() {
177 echo "%rec: repositories";
178 echo "%type: bytes int";
179 echo "%type: public bool";
182 find "$VCS_BACKUP_CONFIG_DIR" -name url.txt -printf '%P\n' | sort | xargs dirname | while read d; do
183 url=$(cat "$VCS_BACKUP_CONFIG_DIR/$d/url.txt");
184 state=$(cat "$VCS_BACKUP_CONFIG_DIR/$d/state.txt");
185 vcsType=$(echo "$d" | sed 's@/.*@@g');
186 sizeBytes=$(du -sb "$VCS_BACKUP_CURRENT_DIR/$d" | cut -f1);
187 [[ -e "$VCS_BACKUP_PUBLIC_DIR/$d" ]] && public="true" || public="false";
189 if [[ "$vcsType" == "hg" ]]; then lastCommit=$(hg log --limit 1 --template '{date|isodatesec}' -R "$VCS_BACKUP_CURRENT_DIR/$d" 2>/dev/null);
190 elif [[ "$vcsType" == "git" ]]; then lastCommit=$(git -C "$VCS_BACKUP_CURRENT_DIR/$d" log --max-count=1 --pretty="%ai");
191 else lastCommit=""; fi
193 printRecfileKeyValue "type" "$vcsType";
194 printRecfileKeyValue "url" "$url";
195 printRecfileKeyValue "state" "$state";
196 printRecfileKeyValue "public" "$public";
197 printRecfileKeyValue "serverPath" "$VCS_BACKUP_CURRENT_DIR/$d";
198 printRecfileKeyValue "size" "$sizeBytes";
199 printRecfileKeyValue "lastCommit" "$lastCommit";
204 # Environment: server
205 # User: $VCS_BACKUP_USER
206 # should be called from cron (usually every day)
207 vcs_backup_public_serverPullCronTask() {
211 # Environment: server
212 # User: $VCS_BACKUP_USER
213 # should be called from cron (usually every day) after Pull (see above)
214 vcs_backup_public_serverSnapshotCronTask() {
218 # --- Single entry-point: --------------------------------------------------------------------------
220 PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin";
221 PUBLIC_FUNCTION_PREFIX="vcs_backup_public_";
222 if type -t "$PUBLIC_FUNCTION_PREFIX$1" > /dev/null; then
223 "$PUBLIC_FUNCTION_PREFIX${@:1}";
224 elif [[ $(basename $0) == "vcs-backup-clone-private-hg" ]]; then "${PUBLIC_FUNCTION_PREFIX}clientSubmitBackupRequest" hg "$1" private clone;
225 elif [[ $(basename $0) == "vcs-backup-clone-private-git" ]]; then "${PUBLIC_FUNCTION_PREFIX}clientSubmitBackupRequest" git "$1" private clone;
226 elif [[ $(basename $0) == "vcs-backup-clone-public-hg" ]]; then "${PUBLIC_FUNCTION_PREFIX}clientSubmitBackupRequest" hg "$1" public clone;
227 elif [[ $(basename $0) == "vcs-backup-clone-public-git" ]]; then "${PUBLIC_FUNCTION_PREFIX}clientSubmitBackupRequest" git "$1" public clone;
229 echo "Unsupported sub-command: $1" >&2
230 echo "Available sub-commands:" >&2
231 declare -F | grep "$PUBLIC_FUNCTION_PREFIX" | sed "s/.*$PUBLIC_FUNCTION_PREFIX/ /g" >&2