first version: IPC (datagram unix domain socket + SSH), create Btrfs subvolume, clone Mercurial or Git repository
4 # Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info)
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.
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.
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/>.
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";
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"
37 mkdir "$VCS_BACKUP_CURRENT_DIR";
38 mkdir "$VCS_BACKUP_CONFIG_DIR";
39 mkdir "$VCS_BACKUP_SNAPSHOT_DIR";
41 chown "${VCS_BACKUP_USER}:${VCS_BACKUP_USER}" "$VCS_BACKUP_CURRENT_DIR"
42 chown "${VCS_BACKUP_MANAGER}:${VCS_BACKUP_MANAGER}" "$VCS_BACKUP_CONFIG_DIR"
46 # --- Private functions: ---------------------------------------------------------------------------
49 # $1 = VCS type: hg, git
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 ]]; }
54 # $1 = path to the config file
55 loadConfigFile() { if [ -f "$1" ]; then . "$1"; else echo "Missing config file: $1" >&2; exit 1; fi }
59 urlToRelativeDirectoryPath() {
60 echo "$1" | sed -E 's@^[^:]+://@@g';
63 # --- Public interface functions: ------------------------------------------------------------------
66 # $1 = VCS type: hg, git
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"
73 echo "Unsupported VCS type: '$1' or URL: '$2'" >&2;
78 # $1 = VCS type: hg, git
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};
94 echo "Unsupported VCS type: '$1' or URL: '$2'" >&2;
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};
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";
125 echo "Unsupported VCS type: '$vcsType' or URL: '$url'" >&2;
127 # TODO: call-back to the client if client is waiting (socket exist in the config dir)
131 # --- Single entry-point: --------------------------------------------------------------------------
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}";
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