vcs-backup.sh
author František Kučera <franta-hg@frantovo.cz>
Fri, 19 Apr 2019 16:53:22 +0200
branchv_0
changeset 2 4c9994be0688
parent 0 6207c211aafd
child 3 1c7536a0beda
permissions -rwxr-xr-x
repositories are private by default; can be set public by adding the "public" parameter and then it will be available through the public web interface
     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";
    28 VCS_BACKUP_USER="vcs-backup";
    29 VCS_BACKUP_MANAGER="vcs-backup-manager";
    30 
    31 # Installation – check and do it by hand:
    32 # There should be already mounted Btrfs at $VCS_BACKUP_DATA_DIR
    33 installInstructions() {
    34 cp vcs-backup.sh /usr/local/bin/
    35 adduser --disabled-password "$VCS_BACKUP"
    36 adduser --disabled-password "$VCS_BACKUP_MANAGER"
    37 
    38 mkdir "$VCS_BACKUP_CURRENT_DIR";
    39 mkdir "$VCS_BACKUP_CONFIG_DIR";
    40 mkdir "$VCS_BACKUP_SNAPSHOT_DIR";
    41 
    42 chown "${VCS_BACKUP_USER}:${VCS_BACKUP_USER}" "$VCS_BACKUP_CURRENT_DIR"
    43 chown "${VCS_BACKUP_MANAGER}:${VCS_BACKUP_MANAGER}" "$VCS_BACKUP_CONFIG_DIR"
    44 }
    45 
    46 
    47 # --- Private functions: ---------------------------------------------------------------------------
    48 
    49 # Environment: all
    50 # $1 = VCS type: hg, git
    51 # $2 = URL
    52 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 ]]; }
    53 
    54 # Environment: all
    55 # $1 = path to the config file
    56 loadConfigFile() { if [ -f "$1" ]; then . "$1"; else echo "Missing config file: $1" >&2; exit 1; fi }
    57 
    58 # Environment: server
    59 # $1 = URL
    60 urlToRelativeDirectoryPath() {
    61 	echo "$1" | sed -E 's@^[^:]+://@@g';
    62 }
    63 
    64 # --- Public interface functions: ------------------------------------------------------------------
    65 
    66 # Environment: client
    67 # $1 = VCS type: hg, git
    68 # $2 = URL
    69 # $3 = "public" or "private" (default), whether the repository should be available through the public web interface
    70 vcs_backup_public_clientSubmitBackupRequest() {
    71 	if isValidTypeAndURL "$1" "$2"; then
    72 		loadConfigFile ~/.config/vcs-backup/client.cfg
    73 		${VCS_BACKUP_SSH_COMMAND[@]} vcs-backup.sh serverSubmitBackupRequest "$1" "$2" "$3"
    74 	else
    75 		echo "Unsupported VCS type: '$1' or URL: '$2'" >&2;
    76 	fi
    77 }
    78 
    79 # Environment: server
    80 # has same parameters as clientSubmitBackupRequest (see above)
    81 vcs_backup_public_serverSubmitBackupRequest() {
    82 	if isValidTypeAndURL "$1" "$2"; then
    83 		loadConfigFile "/etc/vcs-backup/server.cfg";
    84 		relativePath=$1/$(urlToRelativeDirectoryPath "$2");
    85 		absolutePath="$VCS_BACKUP_CONFIG_DIR/$relativePath";
    86 		mkdir -p "$absolutePath";
    87 		echo "$2" > "$absolutePath/url.txt"
    88 		echo "submited" > "$absolutePath/state.txt"
    89 		setfacl -m u:${VCS_BACKUP_USER}:r  "$absolutePath/url.txt"
    90 		setfacl -m u:${VCS_BACKUP_USER}:rw "$absolutePath/state.txt"
    91 		echo "$relativePath" | socat -u - unix-send:${VCS_BACKUP_SUBVOLUME_SOCKET};
    92 		if [[ "$3" == "public" ]]; then
    93 			cd "$VCS_BACKUP_PUBLIC_DIR";
    94 			mkdir -p "$(dirname $relativePath)";
    95 			ln -rs "../current/$relativePath" "$(dirname $relativePath)";
    96 		fi
    97 	else
    98 		echo "Unsupported VCS type: '$1' or URL: '$2'" >&2;
    99 	fi
   100 }
   101 
   102 # Environment: server
   103 # Should be started as a systemd/init service.
   104 # - reads messages from from the subvolume socket – message contains the relative directory path
   105 # - creates a subvolume for given repository + necesary parent directories
   106 # - sends a message to the clone service → start cloning into the created subvolume
   107 vcs_backup_public_serverStartSubvolumeService() {
   108 	socat -u "unix-recv:${VCS_BACKUP_SUBVOLUME_SOCKET},group=${VCS_BACKUP_MANAGER},mode=770" - | while read d; do
   109 		mkdir -p $(dirname "$VCS_BACKUP_CURRENT_DIR/$d");
   110 		btrfs subvolume create "$VCS_BACKUP_CURRENT_DIR/$d" && \
   111 		echo "subvolumeCreated" > "$VCS_BACKUP_CONFIG_DIR/$d/state.txt" && \
   112 		chown "${VCS_BACKUP_USER}:${VCS_BACKUP_USER}" "$VCS_BACKUP_CURRENT_DIR/$d" && \
   113 		echo "$d" | socat -u - unix-send:${VCS_BACKUP_CLONE_SOCKET};
   114 	done
   115 }
   116 
   117 # Environment: server
   118 # should be started as a systemd/init service
   119 vcs_backup_public_serverStartCloneService() {
   120 	socat -u "unix-recv:${VCS_BACKUP_CLONE_SOCKET},mode=700" - | while read d; do
   121 		vcsType=$(echo "$d" | sed 's@/.*@@g');
   122 		url=$(cat "$VCS_BACKUP_CONFIG_DIR/$d/url.txt");
   123 		if isValidTypeAndURL "$vcsType" "$url"; then
   124 			if   [[ "$vcsType" == "hg"  ]]; then  hg clone -U     "$url" "$VCS_BACKUP_CURRENT_DIR/$d";
   125 			elif [[ "$vcsType" == "git" ]]; then git clone --bare "$url" "$VCS_BACKUP_CURRENT_DIR/$d";
   126 			fi && echo "cloned" > "$VCS_BACKUP_CONFIG_DIR/$d/state.txt";
   127 		else
   128 			echo "Unsupported VCS type: '$vcsType' or URL: '$url'" >&2;
   129 		fi
   130 		# TODO: call-back to the client if client is waiting (socket exist in the config dir)
   131 	done
   132 }
   133 
   134 # --- Single entry-point: --------------------------------------------------------------------------
   135 
   136 PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin";
   137 PUBLIC_FUNCTION_PREFIX="vcs_backup_public_";
   138 if type -t "$PUBLIC_FUNCTION_PREFIX$1" > /dev/null; then
   139 	"$PUBLIC_FUNCTION_PREFIX${@:1}";
   140 else
   141 	echo "Unsupported sub-command: $1" >&2
   142 	echo "Available sub-commands:" >&2
   143 	declare -F | grep "$PUBLIC_FUNCTION_PREFIX" | sed "s/.*$PUBLIC_FUNCTION_PREFIX/  /g" >&2
   144 	exit 1;
   145 fi