vcs-backup.sh
author František Kučera <franta-hg@frantovo.cz>
Thu, 24 Oct 2019 22:04:47 +0200
branchv_0
changeset 23 1222714ae08f
parent 21 735961d9596e
child 24 1d46fb79b1af
permissions -rwxr-xr-x
fix license version: GNU GPLv3
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@23
     8
# the Free Software Foundation, version 3 of the License.
franta-hg@0
     9
#
franta-hg@0
    10
# This program is distributed in the hope that it will be useful,
franta-hg@0
    11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@0
    12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@0
    13
# GNU General Public License for more details.
franta-hg@0
    14
#
franta-hg@0
    15
# You should have received a copy of the GNU General Public License
franta-hg@0
    16
# along with this program. If not, see <http://www.gnu.org/licenses/>.
franta-hg@0
    17
franta-hg@0
    18
franta-hg@21
    19
# VCS Backup is a configuration for setting up VCS (version control system) mirrors.
franta-hg@13
    20
# Currently Mercurial (Hg) and Git are supported.
franta-hg@13
    21
# Features:
franta-hg@13
    22
#  - mirrors remote repositories
franta-hg@13
    23
#  - creates Btrfs subvolume for each repository
franta-hg@13
    24
#  - does periodic pull to keep mirrors up to date
franta-hg@13
    25
#  - does periodic Btrfs snapshot to keep history (git push --force done on the remote repository will lead to modifications or deletions in our current mirror, but previous versions will be kept in the snapshots)
franta-hg@13
    26
#  - provides web interface for remote clonning of our mirrors (see systemd and etc folders)
franta-hg@13
    27
#  - can be controlled over SSH by a sane person / owner of the system
franta-hg@13
    28
#  - provides reports in the recfile format (to be processed using GNU Recutils or Relational pipes):
franta-hg@13
    29
#     - list of repositories/mirrors
franta-hg@13
    30
#     - results of pull operations
franta-hg@13
    31
franta-hg@13
    32
franta-hg@13
    33
# This is an asynchronous message-driven shell script that runs distributed across two machines and four user accounts. You have been warned :-)
franta-hg@14
    34
franta-hg@14
    35
franta-hg@16
    36
# Server-side configuration: / loadServerConfigFile()
franta-hg@0
    37
VCS_BACKUP_DATA_DIR="/mnt/data";
franta-hg@0
    38
VCS_BACKUP_CURRENT_DIR="$VCS_BACKUP_DATA_DIR/current";
franta-hg@2
    39
VCS_BACKUP_PUBLIC_DIR="$VCS_BACKUP_DATA_DIR/public";
franta-hg@0
    40
VCS_BACKUP_CONFIG_DIR="$VCS_BACKUP_DATA_DIR/config";
franta-hg@0
    41
VCS_BACKUP_SNAPSHOT_DIR="$VCS_BACKUP_DATA_DIR/snapshot";
franta-hg@0
    42
VCS_BACKUP_SUBVOLUME_SOCKET="/run/vcs-backup-subvolume";
franta-hg@7
    43
VCS_BACKUP_CLONE_SOCKET="/run/vcs-backup-clone/socket"; # the directory will be writable by ${VCS_BACKUP_USER}
franta-hg@3
    44
VCS_BACKUP_CLONE_CALLBACK_SOCKET="clone-callback";
franta-hg@0
    45
VCS_BACKUP_USER="vcs-backup";
franta-hg@0
    46
VCS_BACKUP_MANAGER="vcs-backup-manager";
franta-hg@0
    47
franta-hg@16
    48
# Client-side configuration: / see loadClientConfigFile()
franta-hg@16
    49
VCS_BACKUP_SERVER="$VCS_BACKUP_MANAGER@localhost";
franta-hg@16
    50
VCS_BACKUP_SSH_COMMAND=(ssh "$VCS_BACKUP_SERVER");
franta-hg@16
    51
franta-hg@0
    52
# Installation – check and do it by hand:
franta-hg@0
    53
# There should be already mounted Btrfs at $VCS_BACKUP_DATA_DIR
franta-hg@0
    54
installInstructions() {
franta-hg@0
    55
cp vcs-backup.sh /usr/local/bin/
franta-hg@0
    56
adduser --disabled-password "$VCS_BACKUP"
franta-hg@0
    57
adduser --disabled-password "$VCS_BACKUP_MANAGER"
franta-hg@0
    58
franta-hg@0
    59
mkdir "$VCS_BACKUP_CURRENT_DIR";
franta-hg@0
    60
mkdir "$VCS_BACKUP_CONFIG_DIR";
franta-hg@0
    61
mkdir "$VCS_BACKUP_SNAPSHOT_DIR";
franta-hg@5
    62
mkdir "$(dirname VCS_BACKUP_CLONE_SOCKET)"
franta-hg@0
    63
franta-hg@5
    64
chown "${VCS_BACKUP_USER}:${VCS_BACKUP_USER}" "$(dirname VCS_BACKUP_CLONE_SOCKET)"
franta-hg@0
    65
chown "${VCS_BACKUP_MANAGER}:${VCS_BACKUP_MANAGER}" "$VCS_BACKUP_CONFIG_DIR"
franta-hg@0
    66
}
franta-hg@0
    67
franta-hg@0
    68
franta-hg@0
    69
# --- Private functions: ---------------------------------------------------------------------------
franta-hg@0
    70
franta-hg@0
    71
# Environment: all
franta-hg@0
    72
# $1 = VCS type: hg, git
franta-hg@0
    73
# $2 = URL
franta-hg@0
    74
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
    75
franta-hg@0
    76
# Environment: all
franta-hg@0
    77
# $1 = path to the config file
franta-hg@5
    78
loadConfigFile() { if [ -f "$1" ]; then . "$1"; fi }
franta-hg@16
    79
loadClientConfigFile() { loadConfigFile ~/.config/vcs-backup/client.cfg; }
franta-hg@16
    80
loadServerConfigFile() { loadConfigFile "/etc/vcs-backup/server.cfg"; }
franta-hg@0
    81
franta-hg@0
    82
# Environment: server
franta-hg@0
    83
# $1 = URL
franta-hg@0
    84
urlToRelativeDirectoryPath() {
franta-hg@0
    85
	echo "$1" | sed -E 's@^[^:]+://@@g';
franta-hg@0
    86
}
franta-hg@0
    87
franta-hg@17
    88
# Environment: server
franta-hg@17
    89
# Lists relative paths (starting wiht hg or git) of all configured repositories
franta-hg@17
    90
allRepositories() { find "$VCS_BACKUP_CONFIG_DIR" -name url.txt -printf '%P\n' | sort | xargs --no-run-if-empty dirname; }
franta-hg@17
    91
franta-hg@9
    92
# Environment: all
franta-hg@9
    93
# $1 = optional value (if missing, reads STDIN)
franta-hg@9
    94
escapeRecfileValue() { if [[ $# = 0 ]]; then awk '{ if (NR > 1) { printf "+ " } print $_ }'; else echo "${@}" | ${FUNCNAME[0]}; fi }
franta-hg@9
    95
franta-hg@9
    96
# Environment: all
franta-hg@9
    97
# $1 = key
franta-hg@9
    98
# $2 = value
franta-hg@9
    99
printRecfileKeyValue() { echo -n "$1: "; escapeRecfileValue "$2"; }
franta-hg@9
   100
franta-hg@0
   101
# --- Public interface functions: ------------------------------------------------------------------
franta-hg@0
   102
franta-hg@0
   103
# Environment: client
franta-hg@0
   104
# $1 = VCS type: hg, git
franta-hg@0
   105
# $2 = URL
franta-hg@2
   106
# $3 = "public" or "private" (default), whether the repository should be available through the public web interface
franta-hg@10
   107
# $4 = "clone" (optional), if present, will also clone the backup locally
franta-hg@0
   108
vcs_backup_public_clientSubmitBackupRequest() {
franta-hg@0
   109
	if isValidTypeAndURL "$1" "$2"; then
franta-hg@3
   110
		${VCS_BACKUP_SSH_COMMAND[@]} vcs-backup.sh serverSubmitBackupRequest "$1" "$2" "$3" "$4"
franta-hg@3
   111
		if [[ "$4" == "clone" ]]; then
franta-hg@10
   112
			if   [[ "$1" == "hg"  ]]; then  hg clone "ssh://${VCS_BACKUP_SERVER}/$VCS_BACKUP_CURRENT_DIR/$1/$(urlToRelativeDirectoryPath $2)";
franta-hg@10
   113
			elif [[ "$1" == "git" ]]; then git clone "ssh://${VCS_BACKUP_SERVER}/$VCS_BACKUP_CURRENT_DIR/$1/$(urlToRelativeDirectoryPath $2)";
franta-hg@3
   114
			fi
franta-hg@3
   115
		fi
franta-hg@0
   116
	else
franta-hg@0
   117
		echo "Unsupported VCS type: '$1' or URL: '$2'" >&2;
franta-hg@0
   118
	fi
franta-hg@0
   119
}
franta-hg@0
   120
franta-hg@0
   121
# Environment: server
franta-hg@5
   122
# User: $VCS_BACKUP_MANAGER
franta-hg@2
   123
# has same parameters as clientSubmitBackupRequest (see above)
franta-hg@0
   124
vcs_backup_public_serverSubmitBackupRequest() {
franta-hg@0
   125
	if isValidTypeAndURL "$1" "$2"; then
franta-hg@0
   126
		relativePath=$1/$(urlToRelativeDirectoryPath "$2");
franta-hg@0
   127
		absolutePath="$VCS_BACKUP_CONFIG_DIR/$relativePath";
franta-hg@15
   128
		# TODO: stop if directory already exists / only add public link?
franta-hg@0
   129
		mkdir -p "$absolutePath";
franta-hg@0
   130
		echo "$2" > "$absolutePath/url.txt"
franta-hg@0
   131
		echo "submited" > "$absolutePath/state.txt"
franta-hg@0
   132
		setfacl -m u:${VCS_BACKUP_USER}:r  "$absolutePath/url.txt"
franta-hg@0
   133
		setfacl -m u:${VCS_BACKUP_USER}:rw "$absolutePath/state.txt"
franta-hg@3
   134
franta-hg@2
   135
		if [[ "$3" == "public" ]]; then
franta-hg@2
   136
			cd "$VCS_BACKUP_PUBLIC_DIR";
franta-hg@2
   137
			mkdir -p "$(dirname $relativePath)";
franta-hg@18
   138
			ln -rsf "../current/$relativePath" "$(dirname $relativePath)";
franta-hg@18
   139
		elif [[ "$3" == "private" && -e "$VCS_BACKUP_PUBLIC_DIR/$relativePath" ]]; then
franta-hg@18
   140
			rm "$VCS_BACKUP_PUBLIC_DIR/$relativePath";
franta-hg@2
   141
		fi
franta-hg@3
   142
franta-hg@3
   143
		if [[ "$4" == "clone" ]]; then
franta-hg@12
   144
			callBackSocket=$absolutePath/${VCS_BACKUP_CLONE_CALLBACK_SOCKET}
franta-hg@12
   145
			socat -u "unix-recvfrom:$callBackSocket,mode=777" - | while read m; do # TODO: ,group=${VCS_BACKUP_USER} and no 777 ?
franta-hg@12
   146
				echo "Message from the service: $m";
franta-hg@12
   147
			done &
franta-hg@12
   148
			callBackPID=$!;
franta-hg@12
   149
		fi
franta-hg@12
   150
franta-hg@12
   151
		echo "$relativePath" | socat -u - unix-send:${VCS_BACKUP_SUBVOLUME_SOCKET};
franta-hg@12
   152
franta-hg@12
   153
		if [[ "$4" == "clone" ]]; then
franta-hg@12
   154
			echo "Waiting for a message from the service on $callBackSocket (PID $callBackPID)";
franta-hg@12
   155
			wait -n $callBackPID;
franta-hg@3
   156
		fi
franta-hg@0
   157
	else
franta-hg@0
   158
		echo "Unsupported VCS type: '$1' or URL: '$2'" >&2;
franta-hg@0
   159
	fi
franta-hg@0
   160
}
franta-hg@0
   161
franta-hg@0
   162
# Environment: server
franta-hg@5
   163
# User: root
franta-hg@0
   164
# Should be started as a systemd/init service.
franta-hg@0
   165
# - reads messages from from the subvolume socket – message contains the relative directory path
franta-hg@0
   166
# - creates a subvolume for given repository + necesary parent directories
franta-hg@0
   167
# - sends a message to the clone service → start cloning into the created subvolume
franta-hg@0
   168
vcs_backup_public_serverStartSubvolumeService() {
franta-hg@0
   169
	socat -u "unix-recv:${VCS_BACKUP_SUBVOLUME_SOCKET},group=${VCS_BACKUP_MANAGER},mode=770" - | while read d; do
franta-hg@0
   170
		mkdir -p $(dirname "$VCS_BACKUP_CURRENT_DIR/$d");
franta-hg@12
   171
		if [[ -e "$VCS_BACKUP_CURRENT_DIR/$d" ]]; then
franta-hg@19
   172
			echo "cloned" > "$VCS_BACKUP_CONFIG_DIR/$d/state.txt";
franta-hg@12
   173
		else
franta-hg@12
   174
			btrfs subvolume create "$VCS_BACKUP_CURRENT_DIR/$d" && \
franta-hg@12
   175
			echo "subvolumeCreated" > "$VCS_BACKUP_CONFIG_DIR/$d/state.txt" && \
franta-hg@19
   176
			chown "${VCS_BACKUP_USER}:${VCS_BACKUP_USER}" "$VCS_BACKUP_CURRENT_DIR/$d"
franta-hg@12
   177
		fi
franta-hg@19
   178
		echo "$d" | socat -u - unix-send:${VCS_BACKUP_CLONE_SOCKET};
franta-hg@0
   179
	done
franta-hg@0
   180
}
franta-hg@0
   181
franta-hg@0
   182
# Environment: server
franta-hg@5
   183
# User: $VCS_BACKUP_USER
franta-hg@0
   184
# should be started as a systemd/init service
franta-hg@0
   185
vcs_backup_public_serverStartCloneService() {
franta-hg@0
   186
	socat -u "unix-recv:${VCS_BACKUP_CLONE_SOCKET},mode=700" - | while read d; do
franta-hg@2
   187
		vcsType=$(echo "$d" | sed 's@/.*@@g');
franta-hg@0
   188
		url=$(cat "$VCS_BACKUP_CONFIG_DIR/$d/url.txt");
franta-hg@19
   189
		state=$(cat "$VCS_BACKUP_CONFIG_DIR/$d/state.txt");
franta-hg@3
   190
franta-hg@0
   191
		if isValidTypeAndURL "$vcsType" "$url"; then
franta-hg@19
   192
			if [[ "$state" == "cloned"  ]]; then
franta-hg@19
   193
				# Already cloned repository → just pull
franta-hg@19
   194
				if   [[ "$vcsType" == "hg"  ]]; then hg -R "$VCS_BACKUP_CURRENT_DIR/$d" pull;
franta-hg@19
   195
				elif [[ "$vcsType" == "git" ]]; then git -C "$VCS_BACKUP_CURRENT_DIR/$d" fetch;
franta-hg@19
   196
				fi
franta-hg@19
   197
			else
franta-hg@19
   198
				# New repository → clone
franta-hg@19
   199
				if   [[ "$vcsType" == "hg"  ]]; then  hg clone -U       "$url" "$VCS_BACKUP_CURRENT_DIR/$d";
franta-hg@19
   200
				elif [[ "$vcsType" == "git" ]]; then git clone --mirror "$url" "$VCS_BACKUP_CURRENT_DIR/$d";
franta-hg@19
   201
				fi && echo "cloned" > "$VCS_BACKUP_CONFIG_DIR/$d/state.txt";
franta-hg@19
   202
			fi
franta-hg@0
   203
		else
franta-hg@0
   204
			echo "Unsupported VCS type: '$vcsType' or URL: '$url'" >&2;
franta-hg@0
   205
		fi
franta-hg@3
   206
franta-hg@3
   207
		callBackSocket="$VCS_BACKUP_CONFIG_DIR/$d/$VCS_BACKUP_CLONE_CALLBACK_SOCKET";
franta-hg@3
   208
		if [[ -e "$callBackSocket" ]]; then
franta-hg@3
   209
			echo "done" | socat -u - unix-send:"$callBackSocket";
franta-hg@3
   210
		fi
franta-hg@0
   211
	done
franta-hg@0
   212
}
franta-hg@0
   213
franta-hg@7
   214
# Environment: client
franta-hg@7
   215
# prints list of repositories in recfile format
franta-hg@7
   216
# usage example: vcs-backup.sh clientListRepositories | relpipe-in-recfile | relpipe-out-tabular
franta-hg@7
   217
vcs_backup_public_clientListRepositories() {
franta-hg@7
   218
	${VCS_BACKUP_SSH_COMMAND[@]} vcs-backup.sh serverListRepositories;
franta-hg@7
   219
}
franta-hg@7
   220
franta-hg@7
   221
# Environment: server
franta-hg@7
   222
# User: $VCS_BACKUP_MANAGER
franta-hg@7
   223
vcs_backup_public_serverListRepositories() {
franta-hg@15
   224
	printRecfileKeyValue "%rec"  "repository";
franta-hg@14
   225
	printRecfileKeyValue "%type" "bytes int";
franta-hg@14
   226
	printRecfileKeyValue "%type" "public bool";
franta-hg@7
   227
	echo;
franta-hg@7
   228
franta-hg@17
   229
	allRepositories | while read d; do
franta-hg@7
   230
		url=$(cat "$VCS_BACKUP_CONFIG_DIR/$d/url.txt");
franta-hg@7
   231
		state=$(cat "$VCS_BACKUP_CONFIG_DIR/$d/state.txt");
franta-hg@7
   232
		vcsType=$(echo "$d" | sed 's@/.*@@g');
franta-hg@7
   233
		sizeBytes=$(du -sb "$VCS_BACKUP_CURRENT_DIR/$d" | cut -f1);
franta-hg@7
   234
		[[ -e "$VCS_BACKUP_PUBLIC_DIR/$d" ]] && public="true" || public="false";
franta-hg@7
   235
		
franta-hg@7
   236
		if [[ "$vcsType" == "hg"  ]]; then lastCommit=$(hg log --limit 1 --template '{date|isodatesec}' -R "$VCS_BACKUP_CURRENT_DIR/$d" 2>/dev/null);
franta-hg@7
   237
		elif [[ "$vcsType" == "git" ]]; then lastCommit=$(git -C "$VCS_BACKUP_CURRENT_DIR/$d" log --max-count=1 --pretty="%ai"); 
franta-hg@7
   238
		else lastCommit=""; fi
franta-hg@7
   239
		
franta-hg@9
   240
		printRecfileKeyValue "type"            "$vcsType";
franta-hg@9
   241
		printRecfileKeyValue "url"             "$url";
franta-hg@9
   242
		printRecfileKeyValue "state"           "$state";
franta-hg@9
   243
		printRecfileKeyValue "public"          "$public";
franta-hg@9
   244
		printRecfileKeyValue "serverPath"      "$VCS_BACKUP_CURRENT_DIR/$d";
franta-hg@9
   245
		printRecfileKeyValue "size"            "$sizeBytes";
franta-hg@9
   246
		printRecfileKeyValue "lastCommit"      "$lastCommit";
franta-hg@7
   247
		echo;
franta-hg@7
   248
	done
franta-hg@7
   249
}
franta-hg@7
   250
franta-hg@6
   251
# Environment: server
franta-hg@6
   252
# User: $VCS_BACKUP_USER
franta-hg@6
   253
# should be called from cron (usually every day)
franta-hg@6
   254
vcs_backup_public_serverPullCronTask() {
franta-hg@14
   255
	printRecfileKeyValue "%rec"  "pull";
franta-hg@14
   256
	printRecfileKeyValue "%type" "started date";
franta-hg@14
   257
	printRecfileKeyValue "%type" "finished date";
franta-hg@14
   258
	printRecfileKeyValue "%type" "duration int";
franta-hg@14
   259
	printRecfileKeyValue "%type" "resultCode int";
franta-hg@11
   260
franta-hg@17
   261
	allRepositories | while read d; do
franta-hg@11
   262
		state=$(cat "$VCS_BACKUP_CONFIG_DIR/$d/state.txt");
franta-hg@11
   263
		vcsType=$(echo "$d" | sed 's@/.*@@g');
franta-hg@11
   264
		absolutePath="$VCS_BACKUP_CURRENT_DIR/$d";
franta-hg@11
   265
franta-hg@11
   266
		pullStarted=$(date --iso-8601=s);
franta-hg@11
   267
		pullStartedMiliseconds=$(($(date +%s%N)/1000000));
franta-hg@11
   268
		pullFinished="";
franta-hg@11
   269
		pullDuration="";
franta-hg@11
   270
		pullResult="";
franta-hg@11
   271
		pullResultCode="";
franta-hg@11
   272
		if [[ "$state" == "cloned" ]]; then
franta-hg@11
   273
			if   [[ "$vcsType" == "hg" ]];  then pullResult=$(hg pull --force --repository "$absolutePath" 2>&1); pullResultCode=$?;
franta-hg@11
   274
			elif [[ "$vcsType" == "git" ]]; then pullResult=$(git -C "$absolutePath" fetch 2>&1); pullResultCode=$?;
franta-hg@11
   275
			fi
franta-hg@11
   276
			pullFinished=$(date --iso-8601=s);
franta-hg@11
   277
			pullFinishedMiliseconds=$(($(date +%s%N)/1000000));
franta-hg@11
   278
			pullDuration=$(( $pullFinishedMiliseconds - $pullStartedMiliseconds ));
franta-hg@11
   279
		fi
franta-hg@11
   280
franta-hg@21
   281
		printRecfileKeyValue "type"            "$vcsType";
franta-hg@11
   282
		printRecfileKeyValue "serverPath"      "$absolutePath";
franta-hg@11
   283
		printRecfileKeyValue "state"           "$state";
franta-hg@11
   284
		printRecfileKeyValue "started"         "$pullStarted";
franta-hg@11
   285
		printRecfileKeyValue "finished"        "$pullFinished";
franta-hg@11
   286
		printRecfileKeyValue "duration"        "$pullDuration";
franta-hg@11
   287
		printRecfileKeyValue "resultCode"      "$pullResultCode";
franta-hg@11
   288
		printRecfileKeyValue "message"         "$pullResult";
franta-hg@11
   289
		echo;
franta-hg@11
   290
	done
franta-hg@6
   291
}
franta-hg@6
   292
franta-hg@6
   293
# Environment: server
franta-hg@11
   294
# User: root
franta-hg@6
   295
# should be called from cron (usually every day) after Pull (see above)
franta-hg@6
   296
vcs_backup_public_serverSnapshotCronTask() {
franta-hg@15
   297
	printRecfileKeyValue "%rec"  "snapshot";
franta-hg@15
   298
	printRecfileKeyValue "%type" "started date";
franta-hg@15
   299
	printRecfileKeyValue "%type" "finished date";
franta-hg@15
   300
	printRecfileKeyValue "%type" "duration int";
franta-hg@15
   301
	printRecfileKeyValue "%type" "resultCode int";
franta-hg@15
   302
	
franta-hg@17
   303
	allRepositories | while read d; do
franta-hg@15
   304
		state=$(cat "$VCS_BACKUP_CONFIG_DIR/$d/state.txt");
franta-hg@15
   305
		vcsType=$(echo "$d" | sed 's@/.*@@g');
franta-hg@15
   306
		absolutePath="$VCS_BACKUP_CURRENT_DIR/$d";
franta-hg@15
   307
franta-hg@15
   308
		started=$(date --iso-8601=s);
franta-hg@15
   309
		startedMiliseconds=$(($(date +%s%N)/1000000));
franta-hg@15
   310
		finished="";
franta-hg@15
   311
		duration="";
franta-hg@15
   312
		result="";
franta-hg@15
   313
		resultCode="";
franta-hg@15
   314
		snapshotPath="";
franta-hg@15
   315
		if [[ "$state" == "cloned" ]]; then
franta-hg@15
   316
			snapshotPath="$VCS_BACKUP_SNAPSHOT_DIR/$d/$(date --iso-8601=date)";
franta-hg@15
   317
			mkdir -p $(dirname "$snapshotPath");
franta-hg@15
   318
			result=$(btrfs subvolume snapshot -r "$absolutePath" "$snapshotPath" 2>&1);
franta-hg@15
   319
			resultCode=$?;
franta-hg@15
   320
			finished=$(date --iso-8601=s);
franta-hg@15
   321
			finishedMiliseconds=$(($(date +%s%N)/1000000));
franta-hg@15
   322
			duration=$(( $finishedMiliseconds - $startedMiliseconds ));
franta-hg@15
   323
		fi
franta-hg@15
   324
franta-hg@15
   325
		printRecfileKeyValue "currentPath"     "$absolutePath";
franta-hg@15
   326
		printRecfileKeyValue "snapshotPath"    "$snapshotPath";
franta-hg@15
   327
		printRecfileKeyValue "type"            "$vcsType";
franta-hg@15
   328
		printRecfileKeyValue "state"           "$state";
franta-hg@15
   329
		printRecfileKeyValue "started"         "$started";
franta-hg@15
   330
		printRecfileKeyValue "finished"        "$finished";
franta-hg@15
   331
		printRecfileKeyValue "duration"        "$duration";
franta-hg@15
   332
		printRecfileKeyValue "resultCode"      "$resultCode";
franta-hg@15
   333
		printRecfileKeyValue "message"         "$result";
franta-hg@15
   334
		echo;
franta-hg@15
   335
	done
franta-hg@6
   336
}
franta-hg@6
   337
franta-hg@0
   338
# --- Single entry-point: --------------------------------------------------------------------------
franta-hg@0
   339
franta-hg@16
   340
loadClientConfigFile;
franta-hg@16
   341
loadServerConfigFile;
franta-hg@0
   342
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin";
franta-hg@0
   343
PUBLIC_FUNCTION_PREFIX="vcs_backup_public_";
franta-hg@0
   344
if type -t "$PUBLIC_FUNCTION_PREFIX$1" > /dev/null; then
franta-hg@0
   345
	"$PUBLIC_FUNCTION_PREFIX${@:1}";
franta-hg@4
   346
elif [[ $(basename $0) == "vcs-backup-clone-private-hg"  ]]; then "${PUBLIC_FUNCTION_PREFIX}clientSubmitBackupRequest" hg  "$1" private clone;
franta-hg@4
   347
elif [[ $(basename $0) == "vcs-backup-clone-private-git" ]]; then "${PUBLIC_FUNCTION_PREFIX}clientSubmitBackupRequest" git "$1" private clone;
franta-hg@4
   348
elif [[ $(basename $0) == "vcs-backup-clone-public-hg"   ]]; then "${PUBLIC_FUNCTION_PREFIX}clientSubmitBackupRequest" hg  "$1" public  clone;
franta-hg@4
   349
elif [[ $(basename $0) == "vcs-backup-clone-public-git"  ]]; then "${PUBLIC_FUNCTION_PREFIX}clientSubmitBackupRequest" git "$1" public  clone;
franta-hg@0
   350
else
franta-hg@0
   351
	echo "Unsupported sub-command: $1" >&2
franta-hg@0
   352
	echo "Available sub-commands:" >&2
franta-hg@0
   353
	declare -F | grep "$PUBLIC_FUNCTION_PREFIX" | sed "s/.*$PUBLIC_FUNCTION_PREFIX/  /g" >&2
franta-hg@0
   354
	exit 1;
franta-hg@0
   355
fi