From e59dabbb81182e90077452f68aa65eb4c1511652 Mon Sep 17 00:00:00 2001 From: ljf Date: Thu, 14 Jun 2018 02:13:59 +0200 Subject: [PATCH] [enh] Setup storage for friend (untested) --- README.md | 3 +++ conf/17-data_home | 21 ++++++++++++++++ manifest.json | 41 +++++++++++++++++++++++++++++++ scripts/_common.sh | 60 ++++++++++++++++++++++++++++++++++++++++++++++ scripts/backup | 24 +++++++++++++++++++ scripts/install | 58 ++++++++++++++++++++++++++++++++++++++++++++ scripts/remove | 26 ++++++++++++++++++++ scripts/restore | 59 +++++++++++++++++++++++++++++++++++++++++++++ scripts/upgrade | 30 +++++++++++++++++++++++ 9 files changed, 322 insertions(+) create mode 100644 README.md create mode 100644 conf/17-data_home create mode 100644 manifest.json create mode 100644 scripts/_common.sh create mode 100755 scripts/backup create mode 100755 scripts/install create mode 100755 scripts/remove create mode 100755 scripts/restore create mode 100755 scripts/upgrade diff --git a/README.md b/README.md new file mode 100644 index 0000000..a7891fd --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# borgserver_ynh +Offer backup storage to a friend + diff --git a/conf/17-data_home b/conf/17-data_home new file mode 100644 index 0000000..760fb3b --- /dev/null +++ b/conf/17-data_home @@ -0,0 +1,21 @@ +#!/bin/bash + +# Exit hook on subcommand error or unset variable +set -eu + +# Source YNH helpers +source /usr/share/yunohost/helpers.d/filesystem + +# Backup destination +backup_dir="\${1}/data/home" + +# Backup user home +for f in \$(find /home/* -type d -prune | awk -F/ '{print \$NF}'); do + if [[ ! "\$f" =~ ^yunohost|lost\+found ]]; then + if [ ! -e "/home/\$f/.nobackup" ]; then + ynh_backup "/home/\$f" "${backup_dir}/\$f" 1 + fi + fi +done +EOF + diff --git a/manifest.json b/manifest.json new file mode 100644 index 0000000..a5eb176 --- /dev/null +++ b/manifest.json @@ -0,0 +1,41 @@ +{ + "name": "Borg Server", + "id": "borgserver", + "packaging_format": 1, + "description": { + "en": "Offer backup storage to a friend.", + "fr": "Offrez un espace de stockage à un⋅e ami⋅e." + }, + "version": "1.0", + "url": "https://borgbackup.readthedocs.io", + "license": "BSD-3-Clause", + "maintainer": { + "name": "ljf", + "email": "ljf+borg_ynh@reflexlibre.net", + "url": "https://reflexlibre.net" + }, + "requirements": { + "yunohost": ">= 2.7.2" + }, + "multi_instance": true, + "services": [], + "arguments": { + "install" : [ + { + "name": "ssh_user", + "ask": { + "en": "Indicate the ssh user to create", + "fr": "Indiquez l'utilisateur ssh à créer" + }, + "example": "john" + }, + { + "name": "public_key", + "ask": { + "en": "Indicate the public key given by borg_ynh app", + "fr": "Indiquez la clé publique donnée par l'app borg_ynh" + } + } + ] + } +} diff --git a/scripts/_common.sh b/scripts/_common.sh new file mode 100644 index 0000000..5059d87 --- /dev/null +++ b/scripts/_common.sh @@ -0,0 +1,60 @@ +#!/bin/bash + +#================================================= +# COMMON VARIABLES +#================================================= +# App package root directory should be the parent folder +PKG_DIR=$(cd ../; pwd) + +pkg_dependencies="python3-pip python3-dev libacl1-dev libssl-dev liblz4-dev" + +#================================================= +# COMMON HELPERS +#================================================= +ynh_export () { + local ynh_arg="" + for var in $@; + do + ynh_arg=$(echo $var | awk '{print toupper($0)}') + if [ "$var" == "path_url" ]; then + ynh_arg="PATH" + fi + ynh_arg="YNH_APP_ARG_$ynh_arg" + export $var=${!ynh_arg} + done +} +# Save listed var in YunoHost app settings +# usage: ynh_save_args VARNAME1 [VARNAME2 [...]] +ynh_save_args () { + for var in $@; + do + local setting_var="$var" + if [ "$var" == "path_url" ]; then + setting_var="path" + fi + ynh_app_setting_set $app $setting_var ${!var} + done +} + +# Render templates with Jinja2 +# +# Attention : Variables should be exported before calling this helper to be +# accessible inside templates. +# +# usage: ynh_render_template some_template output_path +# | arg: some_template - Template file to be rendered +# | arg: output_path - The path where the output will be redirected to +ynh_render_template() { + local template_path=$1 + local output_path=$2 + # Taken from https://stackoverflow.com/a/35009576 + python2.7 -c 'import os, sys, jinja2; sys.stdout.write( + jinja2.Template(sys.stdin.read() + ).render(os.environ));' < $template_path > $output_path +} + +ynh_configure () { + ynh_backup_if_checksum_is_different $2 + ynh_configure "${PKG_DIR}/conf/$1.j2" $2 + ynh_store_file_checksum $2 +} diff --git a/scripts/backup b/scripts/backup new file mode 100755 index 0000000..780f511 --- /dev/null +++ b/scripts/backup @@ -0,0 +1,24 @@ +#!/bin/bash + +#================================================= +# GENERIC START +#================================================= +# IMPORT GENERIC HELPERS +#================================================= + +source /usr/share/yunohost/helpers + +#================================================= +# MANAGE SCRIPT FAILURE +#================================================= + +# Exit if an error occurs during the execution of the script +ynh_abort_if_errors + +#================================================= +# LOAD SETTINGS +#================================================= + +app=$YNH_APP_INSTANCE_NAME + + diff --git a/scripts/install b/scripts/install new file mode 100755 index 0000000..9f8927b --- /dev/null +++ b/scripts/install @@ -0,0 +1,58 @@ +#!/bin/bash + +#================================================= +# GENERIC START +#================================================= +# IMPORT GENERIC HELPERS +#================================================= + +source _common.sh +source /usr/share/yunohost/helpers + +#================================================= +# MANAGE SCRIPT FAILURE +#================================================= + +# Exit if an error occurs during the execution of the script +ynh_abort_if_errors + +#================================================= +# RETRIEVE ARGUMENTS FROM THE MANIFEST +#================================================= +export app=$YNH_APP_INSTANCE_NAME + +# Retrieve arguments +ynh_export ssh_user public_key + +#================================================= +# STORE SETTINGS FROM MANIFEST +#================================================= +ynh_save_args ssh_user public_key + +#================================================= +# STORE SETTINGS FROM MANIFEST +#================================================= +ynh_install_app_dependencies $pkg_dependencies +pip3 install setuptools --upgrade +pip3 install borgbackup + +#================================================= +# CREATE SSH USER USED BY BORG +#================================================= +adduser $ssh_user -s /bin/bash --disabled-password + +#================================================= +# AUTORIZE SSH FOR THIS USER +#================================================= +home=/home/$ssh_user +mkdir -p /home/$ssh_user/.ssh +touch /home/$ssh_user/.ssh/authorized_keys +echo "command=\"borg serve --restrict-to-path /home/$ssh_user/backup\",no-pty,no-agent-forwarding,no-port-forwarding,no-X11-forwarding,no-user-rc $public_key" >> /home/$ssh_user/.ssh/authorized_keys + +#================================================= +# AVOID BACKUP OF BACKUP +#================================================= +touch $home/.nobackup +mkdir -p /etc/yunohost/hooks.d/backup +cp -f ../conf/17-data_home /etc/yunohost/hooks.d/backup/17-data_home + diff --git a/scripts/remove b/scripts/remove new file mode 100755 index 0000000..3125ed6 --- /dev/null +++ b/scripts/remove @@ -0,0 +1,26 @@ +#!/bin/bash + +#================================================= +# GENERIC START +#================================================= +# IMPORT GENERIC HELPERS +#================================================= + +source /usr/share/yunohost/helpers + +#================================================= +# LOAD SETTINGS +#================================================= + +app=$YNH_APP_INSTANCE_NAME +ssh_user=$(ynh_app_setting_get $app ssh_user) + +#================================================= +# REMOVE DEPENDENCIES +#================================================= +ynh_remove_app_dependencies + +#================================================= +# REMOVE FILES +#================================================= +userdel $ssh_user diff --git a/scripts/restore b/scripts/restore new file mode 100755 index 0000000..1946267 --- /dev/null +++ b/scripts/restore @@ -0,0 +1,59 @@ +#!/bin/bash + +#================================================= +# GENERIC START +#================================================= +# IMPORT GENERIC HELPERS +#================================================= + +if [ ! -e _common.sh ]; then + # Get the _common.sh file if it's not in the current directory + cp ../settings/scripts/_common.sh ./_common.sh + chmod a+rx _common.sh +fi +source _common.sh +source /usr/share/yunohost/helpers + +#================================================= +# MANAGE SCRIPT FAILURE +#================================================= + +# Exit if an error occurs during the execution of the script +ynh_abort_if_errors + +#================================================= +# LOAD SETTINGS +#================================================= + +app=$YNH_APP_INSTANCE_NAME + +export ssh_user=$(ynh_app_setting_get $app ssh_user) +export public_key=$(ynh_app_setting_get $app public_key) + +#================================================= +# STORE SETTINGS FROM MANIFEST +#================================================= +ynh_install_app_dependencies $pkg_dependencies +pip3 install setuptools --upgrade +pip3 install borgbackup + +#================================================= +# CREATE SSH USER USED BY BORG +#================================================= +adduser $ssh_user -s /bin/bash --disabled-password + +#================================================= +# AUTORIZE SSH FOR THIS USER +#================================================= +home=/home/$ssh_user +mkdir -p /home/$ssh_user/.ssh +touch /home/$ssh_user/.ssh/authorized_keys +echo "command=\"borg serve --restrict-to-path /home/$ssh_user/backup\",no-pty,no-agent-forwarding,no-port-forwarding,no-X11-forwarding,no-user-rc $public_key" >> /home/$ssh_user/.ssh/authorized_keys + +#================================================= +# AVOID BACKUP OF BACKUP +#================================================= +touch $home/.nobackup +mkdir -p /etc/yunohost/hooks.d/backup +cp -f ../conf/17-data_home /etc/yunohost/hooks.d/backup/17-data_home + diff --git a/scripts/upgrade b/scripts/upgrade new file mode 100755 index 0000000..35d72d2 --- /dev/null +++ b/scripts/upgrade @@ -0,0 +1,30 @@ +#!/bin/bash + +#================================================= +# GENERIC START +#================================================= +# IMPORT GENERIC HELPERS +#================================================= + +source _common.sh +source /usr/share/yunohost/helpers + +#================================================= +# LOAD SETTINGS +#================================================= + +app=$YNH_APP_INSTANCE_NAME + +#================================================= +# BACKUP BEFORE UPGRADE THEN ACTIVE TRAP +#================================================= + +# Backup the current version of the app +ynh_backup_before_upgrade +ynh_clean_setup () { + # restore it if the upgrade fails + ynh_restore_upgradebackup +} +# Exit if an error occurs during the execution of the script +ynh_abort_if_errors +