vcs-backup.sh
author František Kučera <franta-hg@frantovo.cz>
Fri, 19 Apr 2019 17:52:37 +0200
branchv_0
changeset 3 1c7536a0beda
parent 2 4c9994be0688
child 4 1539711d8657
permissions -rwxr-xr-x
optional callback → local clone
     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_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 
    43 chown "${VCS_BACKUP_USER}:${VCS_BACKUP_USER}" "$VCS_BACKUP_CURRENT_DIR"
    44 chown "${VCS_BACKUP_MANAGER}:${VCS_BACKUP_MANAGER}" "$VCS_BACKUP_CONFIG_DIR"
    45 }
    46 
    47 
    48 # --- Private functions: ---------------------------------------------------------------------------
    49 
    50 # Environment: all
    51 # $1 = VCS type: hg, git
    52 # $2 = URL
    53 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 ]]; }
    54 
    55 # Environment: all
    56 # $1 = path to the config file
    57 loadConfigFile() { if [ -f "$1" ]; then . "$1"; else echo "Missing config file: $1" >&2; exit 1; fi }
    58 
    59 # Environment: server
    60 # $1 = URL
    61 urlToRelativeDirectoryPath() {
    62 	echo "$1" | sed -E 's@^[^:]+://@@g';
    63 }
    64 
    65 # --- Public interface functions: ------------------------------------------------------------------
    66 
    67 # Environment: client
    68 # $1 = VCS type: hg, git
    69 # $2 = URL
    70 # $3 = "public" or "private" (default), whether the repository should be available through the public web interface
    71 vcs_backup_public_clientSubmitBackupRequest() {
    72 	if isValidTypeAndURL "$1" "$2"; then
    73 		loadConfigFile ~/.config/vcs-backup/client.cfg
    74 		${VCS_BACKUP_SSH_COMMAND[@]} vcs-backup.sh serverSubmitBackupRequest "$1" "$2" "$3" "$4"
    75 		if [[ "$4" == "clone" ]]; then
    76 			if   [[ "$1" == "hg"  ]]; then  hg clone "ssh://${VCS_BACKUP_SERVER}//mnt/data/current/$1/$(urlToRelativeDirectoryPath $2)";
    77 			elif [[ "$1" == "git" ]]; then git clone "ssh://${VCS_BACKUP_SERVER}//mnt/data/current/$1/$(urlToRelativeDirectoryPath $2)";
    78 			fi
    79 		fi
    80 	else
    81 		echo "Unsupported VCS type: '$1' or URL: '$2'" >&2;
    82 	fi
    83 }
    84 
    85 # Environment: server
    86 # has same parameters as clientSubmitBackupRequest (see above)
    87 vcs_backup_public_serverSubmitBackupRequest() {
    88 	if isValidTypeAndURL "$1" "$2"; then
    89 		loadConfigFile "/etc/vcs-backup/server.cfg";
    90 		relativePath=$1/$(urlToRelativeDirectoryPath "$2");
    91 		absolutePath="$VCS_BACKUP_CONFIG_DIR/$relativePath";
    92 		mkdir -p "$absolutePath";
    93 		echo "$2" > "$absolutePath/url.txt"
    94 		echo "submited" > "$absolutePath/state.txt"
    95 		setfacl -m u:${VCS_BACKUP_USER}:r  "$absolutePath/url.txt"
    96 		setfacl -m u:${VCS_BACKUP_USER}:rw "$absolutePath/state.txt"
    97 		echo "$relativePath" | socat -u - unix-send:${VCS_BACKUP_SUBVOLUME_SOCKET};
    98 
    99 		if [[ "$3" == "public" ]]; then
   100 			cd "$VCS_BACKUP_PUBLIC_DIR";
   101 			mkdir -p "$(dirname $relativePath)";
   102 			ln -rs "../current/$relativePath" "$(dirname $relativePath)";
   103 		fi
   104 
   105 		if [[ "$4" == "clone" ]]; then
   106 			socat -u "unix-recvfrom:$absolutePath/${VCS_BACKUP_CLONE_CALLBACK_SOCKET},mode=777" - | while read m; do # TODO: ,group=${VCS_BACKUP_USER} and no 777
   107 				echo "Message from the clone service: $m";
   108 			done;
   109 		fi
   110 	else
   111 		echo "Unsupported VCS type: '$1' or URL: '$2'" >&2;
   112 	fi
   113 }
   114 
   115 # Environment: server
   116 # Should be started as a systemd/init service.
   117 # - reads messages from from the subvolume socket – message contains the relative directory path
   118 # - creates a subvolume for given repository + necesary parent directories
   119 # - sends a message to the clone service → start cloning into the created subvolume
   120 vcs_backup_public_serverStartSubvolumeService() {
   121 	socat -u "unix-recv:${VCS_BACKUP_SUBVOLUME_SOCKET},group=${VCS_BACKUP_MANAGER},mode=770" - | while read d; do
   122 		mkdir -p $(dirname "$VCS_BACKUP_CURRENT_DIR/$d");
   123 		btrfs subvolume create "$VCS_BACKUP_CURRENT_DIR/$d" && \
   124 		echo "subvolumeCreated" > "$VCS_BACKUP_CONFIG_DIR/$d/state.txt" && \
   125 		chown "${VCS_BACKUP_USER}:${VCS_BACKUP_USER}" "$VCS_BACKUP_CURRENT_DIR/$d" && \
   126 		echo "$d" | socat -u - unix-send:${VCS_BACKUP_CLONE_SOCKET};
   127 	done
   128 }
   129 
   130 # Environment: server
   131 # should be started as a systemd/init service
   132 vcs_backup_public_serverStartCloneService() {
   133 	socat -u "unix-recv:${VCS_BACKUP_CLONE_SOCKET},mode=700" - | while read d; do
   134 		vcsType=$(echo "$d" | sed 's@/.*@@g');
   135 		url=$(cat "$VCS_BACKUP_CONFIG_DIR/$d/url.txt");
   136 
   137 		if isValidTypeAndURL "$vcsType" "$url"; then
   138 			if   [[ "$vcsType" == "hg"  ]]; then  hg clone -U     "$url" "$VCS_BACKUP_CURRENT_DIR/$d";
   139 			elif [[ "$vcsType" == "git" ]]; then git clone --bare "$url" "$VCS_BACKUP_CURRENT_DIR/$d";
   140 			fi && echo "cloned" > "$VCS_BACKUP_CONFIG_DIR/$d/state.txt";
   141 		else
   142 			echo "Unsupported VCS type: '$vcsType' or URL: '$url'" >&2;
   143 		fi
   144 
   145 		callBackSocket="$VCS_BACKUP_CONFIG_DIR/$d/$VCS_BACKUP_CLONE_CALLBACK_SOCKET";
   146 		if [[ -e "$callBackSocket" ]]; then
   147 			echo "done" | socat -u - unix-send:"$callBackSocket";
   148 		fi
   149 	done
   150 }
   151 
   152 # --- Single entry-point: --------------------------------------------------------------------------
   153 
   154 PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin";
   155 PUBLIC_FUNCTION_PREFIX="vcs_backup_public_";
   156 if type -t "$PUBLIC_FUNCTION_PREFIX$1" > /dev/null; then
   157 	"$PUBLIC_FUNCTION_PREFIX${@:1}";
   158 else
   159 	echo "Unsupported sub-command: $1" >&2
   160 	echo "Available sub-commands:" >&2
   161 	declare -F | grep "$PUBLIC_FUNCTION_PREFIX" | sed "s/.*$PUBLIC_FUNCTION_PREFIX/  /g" >&2
   162 	exit 1;
   163 fi