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
franta-hg@0
     1
#!/bin/bash
franta-hg@0
     2
franta-hg@0
     3
# VCS Backup
franta-hg@0
     4
# Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info)
franta-hg@0
     5
#
franta-hg@0
     6
# This program is free software: you can redistribute it and/or modify
franta-hg@0
     7
# it under the terms of the GNU General Public License as published by
franta-hg@0
     8
# the Free Software Foundation, either version 3 of the License, or
franta-hg@0
     9
# (at your option) any later version.
franta-hg@0
    10
#
franta-hg@0
    11
# This program is distributed in the hope that it will be useful,
franta-hg@0
    12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@0
    13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@0
    14
# GNU General Public License for more details.
franta-hg@0
    15
#
franta-hg@0
    16
# You should have received a copy of the GNU General Public License
franta-hg@0
    17
# along with this program. If not, see <http://www.gnu.org/licenses/>.
franta-hg@0
    18
franta-hg@0
    19
franta-hg@0
    20
# Server-side configuration:
franta-hg@0
    21
VCS_BACKUP_DATA_DIR="/mnt/data";
franta-hg@0
    22
VCS_BACKUP_CURRENT_DIR="$VCS_BACKUP_DATA_DIR/current";
franta-hg@0
    23
VCS_BACKUP_CONFIG_DIR="$VCS_BACKUP_DATA_DIR/config";
franta-hg@0
    24
VCS_BACKUP_SNAPSHOT_DIR="$VCS_BACKUP_DATA_DIR/snapshot";
franta-hg@0
    25
VCS_BACKUP_SUBVOLUME_SOCKET="/run/vcs-backup-subvolume";
franta-hg@0
    26
VCS_BACKUP_CLONE_SOCKET="/run/vcs-backup-clone";
franta-hg@0
    27
VCS_BACKUP_USER="vcs-backup";
franta-hg@0
    28
VCS_BACKUP_MANAGER="vcs-backup-manager";
franta-hg@0
    29
franta-hg@0
    30
# Installation – check and do it by hand:
franta-hg@0
    31
# There should be already mounted Btrfs at $VCS_BACKUP_DATA_DIR
franta-hg@0
    32
installInstructions() {
franta-hg@0
    33
cp vcs-backup.sh /usr/local/bin/
franta-hg@0
    34
adduser --disabled-password "$VCS_BACKUP"
franta-hg@0
    35
adduser --disabled-password "$VCS_BACKUP_MANAGER"
franta-hg@0
    36
franta-hg@0
    37
mkdir "$VCS_BACKUP_CURRENT_DIR";
franta-hg@0
    38
mkdir "$VCS_BACKUP_CONFIG_DIR";
franta-hg@0
    39
mkdir "$VCS_BACKUP_SNAPSHOT_DIR";
franta-hg@0
    40
franta-hg@0
    41
chown "${VCS_BACKUP_USER}:${VCS_BACKUP_USER}" "$VCS_BACKUP_CURRENT_DIR"
franta-hg@0
    42
chown "${VCS_BACKUP_MANAGER}:${VCS_BACKUP_MANAGER}" "$VCS_BACKUP_CONFIG_DIR"
franta-hg@0
    43
}
franta-hg@0
    44
franta-hg@0
    45
franta-hg@0
    46
# --- Private functions: ---------------------------------------------------------------------------
franta-hg@0
    47
franta-hg@0
    48
# Environment: all
franta-hg@0
    49
# $1 = VCS type: hg, git
franta-hg@0
    50
# $2 = URL
franta-hg@0
    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 ]]; }
franta-hg@0
    52
franta-hg@0
    53
# Environment: all
franta-hg@0
    54
# $1 = path to the config file
franta-hg@0
    55
loadConfigFile() { if [ -f "$1" ]; then . "$1"; else echo "Missing config file: $1" >&2; exit 1; fi }
franta-hg@0
    56
franta-hg@0
    57
# Environment: server
franta-hg@0
    58
# $1 = URL
franta-hg@0
    59
urlToRelativeDirectoryPath() {
franta-hg@0
    60
	echo "$1" | sed -E 's@^[^:]+://@@g';
franta-hg@0
    61
}
franta-hg@0
    62
franta-hg@0
    63
# --- Public interface functions: ------------------------------------------------------------------
franta-hg@0
    64
franta-hg@0
    65
# Environment: client
franta-hg@0
    66
# $1 = VCS type: hg, git
franta-hg@0
    67
# $2 = URL
franta-hg@0
    68
vcs_backup_public_clientSubmitBackupRequest() {
franta-hg@0
    69
	if isValidTypeAndURL "$1" "$2"; then
franta-hg@0
    70
		loadConfigFile ~/.config/vcs-backup/client.cfg
franta-hg@0
    71
		${VCS_BACKUP_SSH_COMMAND[@]} vcs-backup.sh serverSubmitBackupRequest "$1" "$2"
franta-hg@0
    72
	else
franta-hg@0
    73
		echo "Unsupported VCS type: '$1' or URL: '$2'" >&2;
franta-hg@0
    74
	fi
franta-hg@0
    75
}
franta-hg@0
    76
franta-hg@0
    77
# Environment: server
franta-hg@0
    78
# $1 = VCS type: hg, git
franta-hg@0
    79
# $2 = URL
franta-hg@0
    80
vcs_backup_public_serverSubmitBackupRequest() {
franta-hg@0
    81
	if isValidTypeAndURL "$1" "$2"; then
franta-hg@0
    82
		loadConfigFile "/etc/vcs-backup/server.cfg";
franta-hg@0
    83
		relativePath=$1/$(urlToRelativeDirectoryPath "$2");
franta-hg@0
    84
		absolutePath="$VCS_BACKUP_CONFIG_DIR/$relativePath";
franta-hg@0
    85
		mkdir -p "$absolutePath";
franta-hg@0
    86
		echo "$1" > "$absolutePath/type.txt"
franta-hg@0
    87
		echo "$2" > "$absolutePath/url.txt"
franta-hg@0
    88
		echo "submited" > "$absolutePath/state.txt"
franta-hg@0
    89
		setfacl -m u:${VCS_BACKUP_USER}:r  "$absolutePath/type.txt"
franta-hg@0
    90
		setfacl -m u:${VCS_BACKUP_USER}:r  "$absolutePath/url.txt"
franta-hg@0
    91
		setfacl -m u:${VCS_BACKUP_USER}:rw "$absolutePath/state.txt"
franta-hg@0
    92
		echo "$relativePath" | socat -u - unix-send:${VCS_BACKUP_SUBVOLUME_SOCKET};
franta-hg@0
    93
	else
franta-hg@0
    94
		echo "Unsupported VCS type: '$1' or URL: '$2'" >&2;
franta-hg@0
    95
	fi
franta-hg@0
    96
}
franta-hg@0
    97
franta-hg@0
    98
# Environment: server
franta-hg@0
    99
# Should be started as a systemd/init service.
franta-hg@0
   100
# - reads messages from from the subvolume socket – message contains the relative directory path
franta-hg@0
   101
# - creates a subvolume for given repository + necesary parent directories
franta-hg@0
   102
# - sends a message to the clone service → start cloning into the created subvolume
franta-hg@0
   103
vcs_backup_public_serverStartSubvolumeService() {
franta-hg@0
   104
	socat -u "unix-recv:${VCS_BACKUP_SUBVOLUME_SOCKET},group=${VCS_BACKUP_MANAGER},mode=770" - | while read d; do
franta-hg@0
   105
		mkdir -p $(dirname "$VCS_BACKUP_CURRENT_DIR/$d");
franta-hg@0
   106
		btrfs subvolume create "$VCS_BACKUP_CURRENT_DIR/$d" && \
franta-hg@0
   107
		echo "subvolumeCreated" > "$VCS_BACKUP_CONFIG_DIR/$d/state.txt" && \
franta-hg@0
   108
		chown "${VCS_BACKUP_USER}:${VCS_BACKUP_USER}" "$VCS_BACKUP_CURRENT_DIR/$d" && \
franta-hg@0
   109
		echo "$d" | socat -u - unix-send:${VCS_BACKUP_CLONE_SOCKET};
franta-hg@0
   110
	done
franta-hg@0
   111
}
franta-hg@0
   112
franta-hg@0
   113
# Environment: server
franta-hg@0
   114
# should be started as a systemd/init service
franta-hg@0
   115
vcs_backup_public_serverStartCloneService() {
franta-hg@0
   116
	socat -u "unix-recv:${VCS_BACKUP_CLONE_SOCKET},mode=700" - | while read d; do
franta-hg@0
   117
		# FIXME: hg vs. git, clone
franta-hg@0
   118
		vcsType=$(cat "$VCS_BACKUP_CONFIG_DIR/$d/type.txt");
franta-hg@0
   119
		url=$(cat "$VCS_BACKUP_CONFIG_DIR/$d/url.txt");
franta-hg@0
   120
		if isValidTypeAndURL "$vcsType" "$url"; then
franta-hg@0
   121
			if   [[ "$vcsType" == "hg"  ]]; then  hg clone -U     "$url" "$VCS_BACKUP_CURRENT_DIR/$d";
franta-hg@0
   122
			elif [[ "$vcsType" == "git" ]]; then git clone --bare "$url" "$VCS_BACKUP_CURRENT_DIR/$d";
franta-hg@0
   123
			fi && echo "cloned" > "$VCS_BACKUP_CONFIG_DIR/$d/state.txt";
franta-hg@0
   124
		else
franta-hg@0
   125
			echo "Unsupported VCS type: '$vcsType' or URL: '$url'" >&2;
franta-hg@0
   126
		fi
franta-hg@0
   127
		# TODO: call-back to the client if client is waiting (socket exist in the config dir)
franta-hg@0
   128
	done
franta-hg@0
   129
}
franta-hg@0
   130
franta-hg@0
   131
# --- Single entry-point: --------------------------------------------------------------------------
franta-hg@0
   132
franta-hg@0
   133
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin";
franta-hg@0
   134
PUBLIC_FUNCTION_PREFIX="vcs_backup_public_";
franta-hg@0
   135
if type -t "$PUBLIC_FUNCTION_PREFIX$1" > /dev/null; then
franta-hg@0
   136
	"$PUBLIC_FUNCTION_PREFIX${@:1}";
franta-hg@0
   137
else
franta-hg@0
   138
	echo "Unsupported sub-command: $1" >&2
franta-hg@0
   139
	echo "Available sub-commands:" >&2
franta-hg@0
   140
	declare -F | grep "$PUBLIC_FUNCTION_PREFIX" | sed "s/.*$PUBLIC_FUNCTION_PREFIX/  /g" >&2
franta-hg@0
   141
	exit 1;
franta-hg@0
   142
fi