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