vcs-backup.sh
author František Kučera <franta-hg@frantovo.cz>
Sun, 21 Apr 2019 21:49:35 +0200
branchv_0
changeset 12 7bdf24cc2e9e
parent 11 d46ed3f5c72b
child 13 a0b7d78460c2
permissions -rwxr-xr-x
do not stuck when cloning already mirrored repository
     1 #!/bin/bash
     2 
     3 # VCS Backup
     4 # Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info)
     5 #
     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.
    10 #
    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.
    15 #
    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/>.
    18 
    19 
    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";
    31 
    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"
    38 
    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)"
    43 
    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"
    46 }
    47 
    48 
    49 # --- Private functions: ---------------------------------------------------------------------------
    50 
    51 # Environment: all
    52 # $1 = VCS type: hg, git
    53 # $2 = URL
    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 ]]; }
    55 
    56 # Environment: all
    57 # $1 = path to the config file
    58 loadConfigFile() { if [ -f "$1" ]; then . "$1"; fi }
    59 
    60 # Environment: server
    61 # $1 = URL
    62 urlToRelativeDirectoryPath() {
    63 	echo "$1" | sed -E 's@^[^:]+://@@g';
    64 }
    65 
    66 # Environment: all
    67 # $1 = optional value (if missing, reads STDIN)
    68 escapeRecfileValue() { if [[ $# = 0 ]]; then awk '{ if (NR > 1) { printf "+ " } print $_ }'; else echo "${@}" | ${FUNCNAME[0]}; fi }
    69 
    70 # Environment: all
    71 # $1 = key
    72 # $2 = value
    73 printRecfileKeyValue() { echo -n "$1: "; escapeRecfileValue "$2"; }
    74 
    75 # --- Public interface functions: ------------------------------------------------------------------
    76 
    77 # Environment: client
    78 # $1 = VCS type: hg, git
    79 # $2 = URL
    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)";
    89 			fi
    90 		fi
    91 	else
    92 		echo "Unsupported VCS type: '$1' or URL: '$2'" >&2;
    93 	fi
    94 }
    95 
    96 # Environment: server
    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 
   110 		if [[ "$3" == "public" ]]; then
   111 			cd "$VCS_BACKUP_PUBLIC_DIR";
   112 			mkdir -p "$(dirname $relativePath)";
   113 			ln -rs "../current/$relativePath" "$(dirname $relativePath)";
   114 		fi
   115 
   116 		if [[ "$4" == "clone" ]]; then
   117 			callBackSocket=$absolutePath/${VCS_BACKUP_CLONE_CALLBACK_SOCKET}
   118 			socat -u "unix-recvfrom:$callBackSocket,mode=777" - | while read m; do # TODO: ,group=${VCS_BACKUP_USER} and no 777 ?
   119 				echo "Message from the service: $m";
   120 			done &
   121 			callBackPID=$!;
   122 		fi
   123 
   124 		echo "$relativePath" | socat -u - unix-send:${VCS_BACKUP_SUBVOLUME_SOCKET};
   125 
   126 		if [[ "$4" == "clone" ]]; then
   127 			echo "Waiting for a message from the service on $callBackSocket (PID $callBackPID)";
   128 			wait -n $callBackPID;
   129 		fi
   130 	else
   131 		echo "Unsupported VCS type: '$1' or URL: '$2'" >&2;
   132 	fi
   133 }
   134 
   135 # Environment: server
   136 # User: root
   137 # Should be started as a systemd/init service.
   138 # - reads messages from from the subvolume socket – message contains the relative directory path
   139 # - creates a subvolume for given repository + necesary parent directories
   140 # - sends a message to the clone service → start cloning into the created subvolume
   141 vcs_backup_public_serverStartSubvolumeService() {
   142 	socat -u "unix-recv:${VCS_BACKUP_SUBVOLUME_SOCKET},group=${VCS_BACKUP_MANAGER},mode=770" - | while read d; do
   143 		mkdir -p $(dirname "$VCS_BACKUP_CURRENT_DIR/$d");
   144 		if [[ -e "$VCS_BACKUP_CURRENT_DIR/$d" ]]; then
   145 			callBackSocket="$VCS_BACKUP_CONFIG_DIR/$d/$VCS_BACKUP_CLONE_CALLBACK_SOCKET";
   146 			if [[ -e "$callBackSocket" ]]; then
   147 				echo "alreadyDone" | socat -u - unix-send:"$callBackSocket";
   148 			fi
   149 		else
   150 			btrfs subvolume create "$VCS_BACKUP_CURRENT_DIR/$d" && \
   151 			echo "subvolumeCreated" > "$VCS_BACKUP_CONFIG_DIR/$d/state.txt" && \
   152 			chown "${VCS_BACKUP_USER}:${VCS_BACKUP_USER}" "$VCS_BACKUP_CURRENT_DIR/$d" && \
   153 			echo "$d" | socat -u - unix-send:${VCS_BACKUP_CLONE_SOCKET};
   154 		fi
   155 	done
   156 }
   157 
   158 # Environment: server
   159 # User: $VCS_BACKUP_USER
   160 # should be started as a systemd/init service
   161 vcs_backup_public_serverStartCloneService() {
   162 	socat -u "unix-recv:${VCS_BACKUP_CLONE_SOCKET},mode=700" - | while read d; do
   163 		vcsType=$(echo "$d" | sed 's@/.*@@g');
   164 		url=$(cat "$VCS_BACKUP_CONFIG_DIR/$d/url.txt");
   165 
   166 		if isValidTypeAndURL "$vcsType" "$url"; then
   167 			if   [[ "$vcsType" == "hg"  ]]; then  hg clone -U       "$url" "$VCS_BACKUP_CURRENT_DIR/$d";
   168 			elif [[ "$vcsType" == "git" ]]; then git clone --mirror "$url" "$VCS_BACKUP_CURRENT_DIR/$d";
   169 			fi && echo "cloned" > "$VCS_BACKUP_CONFIG_DIR/$d/state.txt";
   170 		else
   171 			echo "Unsupported VCS type: '$vcsType' or URL: '$url'" >&2;
   172 		fi
   173 
   174 		callBackSocket="$VCS_BACKUP_CONFIG_DIR/$d/$VCS_BACKUP_CLONE_CALLBACK_SOCKET";
   175 		if [[ -e "$callBackSocket" ]]; then
   176 			echo "done" | socat -u - unix-send:"$callBackSocket";
   177 		fi
   178 	done
   179 }
   180 
   181 # Environment: client
   182 # prints list of repositories in recfile format
   183 # usage example: vcs-backup.sh clientListRepositories | relpipe-in-recfile | relpipe-out-tabular
   184 vcs_backup_public_clientListRepositories() {
   185 	loadConfigFile ~/.config/vcs-backup/client.cfg;
   186 	${VCS_BACKUP_SSH_COMMAND[@]} vcs-backup.sh serverListRepositories;
   187 }
   188 
   189 # Environment: server
   190 # User: $VCS_BACKUP_MANAGER
   191 vcs_backup_public_serverListRepositories() {
   192 	echo "%rec: repositories";
   193 	echo "%type: bytes int";
   194 	echo "%type: public bool";
   195 	echo;
   196 
   197 	find "$VCS_BACKUP_CONFIG_DIR" -name url.txt -printf '%P\n' | sort | xargs dirname | while read d; do
   198 		url=$(cat "$VCS_BACKUP_CONFIG_DIR/$d/url.txt");
   199 		state=$(cat "$VCS_BACKUP_CONFIG_DIR/$d/state.txt");
   200 		vcsType=$(echo "$d" | sed 's@/.*@@g');
   201 		sizeBytes=$(du -sb "$VCS_BACKUP_CURRENT_DIR/$d" | cut -f1);
   202 		[[ -e "$VCS_BACKUP_PUBLIC_DIR/$d" ]] && public="true" || public="false";
   203 		
   204 		if [[ "$vcsType" == "hg"  ]]; then lastCommit=$(hg log --limit 1 --template '{date|isodatesec}' -R "$VCS_BACKUP_CURRENT_DIR/$d" 2>/dev/null);
   205 		elif [[ "$vcsType" == "git" ]]; then lastCommit=$(git -C "$VCS_BACKUP_CURRENT_DIR/$d" log --max-count=1 --pretty="%ai"); 
   206 		else lastCommit=""; fi
   207 		
   208 		printRecfileKeyValue "type"            "$vcsType";
   209 		printRecfileKeyValue "url"             "$url";
   210 		printRecfileKeyValue "state"           "$state";
   211 		printRecfileKeyValue "public"          "$public";
   212 		printRecfileKeyValue "serverPath"      "$VCS_BACKUP_CURRENT_DIR/$d";
   213 		printRecfileKeyValue "size"            "$sizeBytes";
   214 		printRecfileKeyValue "lastCommit"      "$lastCommit";
   215 		echo;
   216 	done
   217 }
   218 
   219 # Environment: server
   220 # User: $VCS_BACKUP_USER
   221 # should be called from cron (usually every day)
   222 vcs_backup_public_serverPullCronTask() {
   223 	echo "%rec: pull";
   224 	echo "%type: started date";
   225 	echo "%type: finished date";
   226 	echo "%type: duration int";
   227 	echo "%type: resultCode int";
   228 
   229 	find "$VCS_BACKUP_CONFIG_DIR" -name url.txt -printf '%P\n' | sort | xargs dirname | while read d; do
   230 		state=$(cat "$VCS_BACKUP_CONFIG_DIR/$d/state.txt");
   231 		vcsType=$(echo "$d" | sed 's@/.*@@g');
   232 		absolutePath="$VCS_BACKUP_CURRENT_DIR/$d";
   233 
   234 
   235 		pullStarted=$(date --iso-8601=s);
   236 		pullStartedMiliseconds=$(($(date +%s%N)/1000000));
   237 		pullFinished="";
   238 		pullDuration="";
   239 		pullResult="";
   240 		pullResultCode="";
   241 		if [[ "$state" == "cloned" ]]; then
   242 			if   [[ "$vcsType" == "hg" ]];  then pullResult=$(hg pull --force --repository "$absolutePath" 2>&1); pullResultCode=$?;
   243 			elif [[ "$vcsType" == "git" ]]; then pullResult=$(git -C "$absolutePath" fetch 2>&1); pullResultCode=$?;
   244 			fi
   245 			pullFinished=$(date --iso-8601=s);
   246 			pullFinishedMiliseconds=$(($(date +%s%N)/1000000));
   247 			pullDuration=$(( $pullFinishedMiliseconds - $pullStartedMiliseconds ));
   248 		fi
   249 
   250 		printRecfileKeyValue "serverPath"      "$absolutePath";
   251 		printRecfileKeyValue "type"            "$vcsType";
   252 		printRecfileKeyValue "state"           "$state";
   253 		printRecfileKeyValue "started"         "$pullStarted";
   254 		printRecfileKeyValue "finished"        "$pullFinished";
   255 		printRecfileKeyValue "duration"        "$pullDuration";
   256 		printRecfileKeyValue "resultCode"      "$pullResultCode";
   257 		printRecfileKeyValue "message"         "$pullResult";
   258 		echo;
   259 	done
   260 }
   261 
   262 # Environment: server
   263 # User: root
   264 # should be called from cron (usually every day) after Pull (see above)
   265 vcs_backup_public_serverSnapshotCronTask() {
   266 	return;
   267 }
   268 
   269 # --- Single entry-point: --------------------------------------------------------------------------
   270 
   271 PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin";
   272 PUBLIC_FUNCTION_PREFIX="vcs_backup_public_";
   273 if type -t "$PUBLIC_FUNCTION_PREFIX$1" > /dev/null; then
   274 	"$PUBLIC_FUNCTION_PREFIX${@:1}";
   275 elif [[ $(basename $0) == "vcs-backup-clone-private-hg"  ]]; then "${PUBLIC_FUNCTION_PREFIX}clientSubmitBackupRequest" hg  "$1" private clone;
   276 elif [[ $(basename $0) == "vcs-backup-clone-private-git" ]]; then "${PUBLIC_FUNCTION_PREFIX}clientSubmitBackupRequest" git "$1" private clone;
   277 elif [[ $(basename $0) == "vcs-backup-clone-public-hg"   ]]; then "${PUBLIC_FUNCTION_PREFIX}clientSubmitBackupRequest" hg  "$1" public  clone;
   278 elif [[ $(basename $0) == "vcs-backup-clone-public-git"  ]]; then "${PUBLIC_FUNCTION_PREFIX}clientSubmitBackupRequest" git "$1" public  clone;
   279 else
   280 	echo "Unsupported sub-command: $1" >&2
   281 	echo "Available sub-commands:" >&2
   282 	declare -F | grep "$PUBLIC_FUNCTION_PREFIX" | sed "s/.*$PUBLIC_FUNCTION_PREFIX/  /g" >&2
   283 	exit 1;
   284 fi