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