first version: IPC (datagram unix domain socket + SSH), create Btrfs subvolume, clone Mercurial or Git repository
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/vcs-backup.sh Thu Apr 18 16:12:06 2019 +0200
1.3 @@ -0,0 +1,142 @@
1.4 +#!/bin/bash
1.5 +
1.6 +# VCS Backup
1.7 +# Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info)
1.8 +#
1.9 +# This program is free software: you can redistribute it and/or modify
1.10 +# it under the terms of the GNU General Public License as published by
1.11 +# the Free Software Foundation, either version 3 of the License, or
1.12 +# (at your option) any later version.
1.13 +#
1.14 +# This program is distributed in the hope that it will be useful,
1.15 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1.16 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.17 +# GNU General Public License for more details.
1.18 +#
1.19 +# You should have received a copy of the GNU General Public License
1.20 +# along with this program. If not, see <http://www.gnu.org/licenses/>.
1.21 +
1.22 +
1.23 +# Server-side configuration:
1.24 +VCS_BACKUP_DATA_DIR="/mnt/data";
1.25 +VCS_BACKUP_CURRENT_DIR="$VCS_BACKUP_DATA_DIR/current";
1.26 +VCS_BACKUP_CONFIG_DIR="$VCS_BACKUP_DATA_DIR/config";
1.27 +VCS_BACKUP_SNAPSHOT_DIR="$VCS_BACKUP_DATA_DIR/snapshot";
1.28 +VCS_BACKUP_SUBVOLUME_SOCKET="/run/vcs-backup-subvolume";
1.29 +VCS_BACKUP_CLONE_SOCKET="/run/vcs-backup-clone";
1.30 +VCS_BACKUP_USER="vcs-backup";
1.31 +VCS_BACKUP_MANAGER="vcs-backup-manager";
1.32 +
1.33 +# Installation – check and do it by hand:
1.34 +# There should be already mounted Btrfs at $VCS_BACKUP_DATA_DIR
1.35 +installInstructions() {
1.36 +cp vcs-backup.sh /usr/local/bin/
1.37 +adduser --disabled-password "$VCS_BACKUP"
1.38 +adduser --disabled-password "$VCS_BACKUP_MANAGER"
1.39 +
1.40 +mkdir "$VCS_BACKUP_CURRENT_DIR";
1.41 +mkdir "$VCS_BACKUP_CONFIG_DIR";
1.42 +mkdir "$VCS_BACKUP_SNAPSHOT_DIR";
1.43 +
1.44 +chown "${VCS_BACKUP_USER}:${VCS_BACKUP_USER}" "$VCS_BACKUP_CURRENT_DIR"
1.45 +chown "${VCS_BACKUP_MANAGER}:${VCS_BACKUP_MANAGER}" "$VCS_BACKUP_CONFIG_DIR"
1.46 +}
1.47 +
1.48 +
1.49 +# --- Private functions: ---------------------------------------------------------------------------
1.50 +
1.51 +# Environment: all
1.52 +# $1 = VCS type: hg, git
1.53 +# $2 = URL
1.54 +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 ]]; }
1.55 +
1.56 +# Environment: all
1.57 +# $1 = path to the config file
1.58 +loadConfigFile() { if [ -f "$1" ]; then . "$1"; else echo "Missing config file: $1" >&2; exit 1; fi }
1.59 +
1.60 +# Environment: server
1.61 +# $1 = URL
1.62 +urlToRelativeDirectoryPath() {
1.63 + echo "$1" | sed -E 's@^[^:]+://@@g';
1.64 +}
1.65 +
1.66 +# --- Public interface functions: ------------------------------------------------------------------
1.67 +
1.68 +# Environment: client
1.69 +# $1 = VCS type: hg, git
1.70 +# $2 = URL
1.71 +vcs_backup_public_clientSubmitBackupRequest() {
1.72 + if isValidTypeAndURL "$1" "$2"; then
1.73 + loadConfigFile ~/.config/vcs-backup/client.cfg
1.74 + ${VCS_BACKUP_SSH_COMMAND[@]} vcs-backup.sh serverSubmitBackupRequest "$1" "$2"
1.75 + else
1.76 + echo "Unsupported VCS type: '$1' or URL: '$2'" >&2;
1.77 + fi
1.78 +}
1.79 +
1.80 +# Environment: server
1.81 +# $1 = VCS type: hg, git
1.82 +# $2 = URL
1.83 +vcs_backup_public_serverSubmitBackupRequest() {
1.84 + if isValidTypeAndURL "$1" "$2"; then
1.85 + loadConfigFile "/etc/vcs-backup/server.cfg";
1.86 + relativePath=$1/$(urlToRelativeDirectoryPath "$2");
1.87 + absolutePath="$VCS_BACKUP_CONFIG_DIR/$relativePath";
1.88 + mkdir -p "$absolutePath";
1.89 + echo "$1" > "$absolutePath/type.txt"
1.90 + echo "$2" > "$absolutePath/url.txt"
1.91 + echo "submited" > "$absolutePath/state.txt"
1.92 + setfacl -m u:${VCS_BACKUP_USER}:r "$absolutePath/type.txt"
1.93 + setfacl -m u:${VCS_BACKUP_USER}:r "$absolutePath/url.txt"
1.94 + setfacl -m u:${VCS_BACKUP_USER}:rw "$absolutePath/state.txt"
1.95 + echo "$relativePath" | socat -u - unix-send:${VCS_BACKUP_SUBVOLUME_SOCKET};
1.96 + else
1.97 + echo "Unsupported VCS type: '$1' or URL: '$2'" >&2;
1.98 + fi
1.99 +}
1.100 +
1.101 +# Environment: server
1.102 +# Should be started as a systemd/init service.
1.103 +# - reads messages from from the subvolume socket – message contains the relative directory path
1.104 +# - creates a subvolume for given repository + necesary parent directories
1.105 +# - sends a message to the clone service → start cloning into the created subvolume
1.106 +vcs_backup_public_serverStartSubvolumeService() {
1.107 + socat -u "unix-recv:${VCS_BACKUP_SUBVOLUME_SOCKET},group=${VCS_BACKUP_MANAGER},mode=770" - | while read d; do
1.108 + mkdir -p $(dirname "$VCS_BACKUP_CURRENT_DIR/$d");
1.109 + btrfs subvolume create "$VCS_BACKUP_CURRENT_DIR/$d" && \
1.110 + echo "subvolumeCreated" > "$VCS_BACKUP_CONFIG_DIR/$d/state.txt" && \
1.111 + chown "${VCS_BACKUP_USER}:${VCS_BACKUP_USER}" "$VCS_BACKUP_CURRENT_DIR/$d" && \
1.112 + echo "$d" | socat -u - unix-send:${VCS_BACKUP_CLONE_SOCKET};
1.113 + done
1.114 +}
1.115 +
1.116 +# Environment: server
1.117 +# should be started as a systemd/init service
1.118 +vcs_backup_public_serverStartCloneService() {
1.119 + socat -u "unix-recv:${VCS_BACKUP_CLONE_SOCKET},mode=700" - | while read d; do
1.120 + # FIXME: hg vs. git, clone
1.121 + vcsType=$(cat "$VCS_BACKUP_CONFIG_DIR/$d/type.txt");
1.122 + url=$(cat "$VCS_BACKUP_CONFIG_DIR/$d/url.txt");
1.123 + if isValidTypeAndURL "$vcsType" "$url"; then
1.124 + if [[ "$vcsType" == "hg" ]]; then hg clone -U "$url" "$VCS_BACKUP_CURRENT_DIR/$d";
1.125 + elif [[ "$vcsType" == "git" ]]; then git clone --bare "$url" "$VCS_BACKUP_CURRENT_DIR/$d";
1.126 + fi && echo "cloned" > "$VCS_BACKUP_CONFIG_DIR/$d/state.txt";
1.127 + else
1.128 + echo "Unsupported VCS type: '$vcsType' or URL: '$url'" >&2;
1.129 + fi
1.130 + # TODO: call-back to the client if client is waiting (socket exist in the config dir)
1.131 + done
1.132 +}
1.133 +
1.134 +# --- Single entry-point: --------------------------------------------------------------------------
1.135 +
1.136 +PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin";
1.137 +PUBLIC_FUNCTION_PREFIX="vcs_backup_public_";
1.138 +if type -t "$PUBLIC_FUNCTION_PREFIX$1" > /dev/null; then
1.139 + "$PUBLIC_FUNCTION_PREFIX${@:1}";
1.140 +else
1.141 + echo "Unsupported sub-command: $1" >&2
1.142 + echo "Available sub-commands:" >&2
1.143 + declare -F | grep "$PUBLIC_FUNCTION_PREFIX" | sed "s/.*$PUBLIC_FUNCTION_PREFIX/ /g" >&2
1.144 + exit 1;
1.145 +fi