home · contact · privacy
Add borg backuping.
authorChristian Heller <c.heller@plomlompom.de>
Sun, 16 Dec 2018 23:54:26 +0000 (00:54 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Sun, 16 Dec 2018 23:54:26 +0000 (00:54 +0100)
all_new_2018/borg.sh [new file with mode: 0755]
all_new_2018/setup_scripts/setup_server.sh

diff --git a/all_new_2018/borg.sh b/all_new_2018/borg.sh
new file mode 100755 (executable)
index 0000000..7597795
--- /dev/null
@@ -0,0 +1,106 @@
+#!/bin/sh
+set -e
+set -x
+
+standard_repo="borg"
+config_file="${HOME}/.borgrepos"
+
+usage() {
+    echo "Need operation as argument, one of:"
+    echo "init"
+    echo "store"
+    echo "check"
+    echo "export_keyfiles"
+    false
+}
+
+read_pw() {
+    stty -echo
+    printf "Passphrase: "
+    read password
+    stty echo
+    printf "\n"
+    export BORG_PASSPHRASE="${password}"
+}
+
+if [ ! -f "${config_file}" ]; then
+    echo '# file read ends at last newline' >> "${config_file}"
+fi
+if [ "$#" -lt 1 ]; then
+    usage
+fi
+first_arg="$1"
+shift
+if [ "${first_arg}" = "init" ]; then
+    if [ ! "$#" -eq 1 ]; then
+        echo "Need exactly one argument: target of form user@server"
+        false
+    fi
+    target="$1"
+    echo "Initializing: ${target}"
+    borg init --verbose --encryption=keyfile "${target}:${standard_repo}"
+    tmp_file="/tmp/new_borgrepos"
+    echo "${target}" > "${tmp_file}"
+    cat "${config_file}" >> "${tmp_file}"
+    cp "${tmp_file}" "${config_file}"
+elif [ "${first_arg}" = "store" ]; then
+    if [ "$#" -lt 2 ]; then
+        echo "Need archive name and paths to archive."
+        false
+    fi
+    archive_name=$1
+    shift
+    to_backup="$@"
+    read_pw
+    cat "${config_file}" | while read line; do
+        first_char=$(echo "${line}" | cut -c1)
+        if [ "${first_char}" = "#" ]; then
+            continue
+        fi
+        repo="${line}:${standard_repo}"
+        archive="${repo}::${archive_name}"
+        echo "Creating archive: ${archive}"
+        borg create --verbose --list "${archive}" ${to_backup}
+    done
+elif [ "${first_arg}" = "check" ]; then
+    if [ ! "$#" -eq 0 ]; then
+        echo "Need no arguments"
+        false
+    fi
+    read_pw
+    cat "${config_file}" | while read line; do
+        first_char=$(echo "${line}" | cut -c1)
+        if [ "${first_char}" = "#" ]; then
+            continue
+        fi
+        repo="${line}:${standard_repo}"
+        echo "Checking repo: ${repo}"
+        borg check --verbose "${repo}"
+    done
+elif [ "${first_arg}" = "export_keyfiles" ]; then
+    if [ ! "$#" -eq 1 ]; then
+        echo "Need output tar file name."
+        false
+    fi
+    tar_target="${1}"
+    tmp_dir="${HOME}/.borgtmp"
+    keyfiles_dir="${tmp_dir}/borg_keyfiles"
+    mkdir -p "${keyfiles_dir}"
+    cat "${config_file}" | while read line; do
+        first_char=$(echo "${line}" | cut -c1)
+        if [ "${first_char}" = "#" ]; then
+            continue
+        fi
+        repo="${line}:${standard_repo}"
+        borg key export "${repo}" "${keyfiles_dir}/${line}"
+    done
+    cur_dir="$(pwd)"
+    cd "${tmp_dir}"
+    target=$(basename "${keyfiles_dir}")
+    tar cf "${tar_target}" "${target}"
+    mv "${tar_target}" "${cur_dir}"
+    cd
+    rm -rf "${tmp_dir}"
+else
+    usage
+fi
index b47a495114ffba35d07a30fd81519160f317e8fd..2c0bdde31f676e116bcbd518f2c3d3cea402d3cc 100755 (executable)
@@ -44,3 +44,6 @@ ln -sf /usr/share/zoneinfo/Europe/Berlin /etc/localtime
 # If we have not yet set the shell for user plom, ensure it here. This
 # is mostly for convenience.
 usermod -s /bin/bash plom
+
+# We want to be able to use ALL our servers as borg backup destinations.
+apt -y install borgbackup