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