From b3c73a5071826553a080aa71821b181a844b125d Mon Sep 17 00:00:00 2001 From: ljf Date: Fri, 31 Aug 2018 01:09:14 +0200 Subject: [PATCH 1/3] [fix] Remove pip without venv --- conf/backup_method.j2 | 14 +++++++-- manifest.json | 4 +-- scripts/_common.sh | 68 ++++++++++++++++++++++++++++++++++++++++++- scripts/install | 9 ++++-- scripts/remove | 5 ++++ scripts/restore | 11 +++++-- scripts/upgrade | 5 ++++ 7 files changed, 106 insertions(+), 10 deletions(-) diff --git a/conf/backup_method.j2 b/conf/backup_method.j2 index 369e1e6..e8511da 100644 --- a/conf/backup_method.j2 +++ b/conf/backup_method.j2 @@ -28,13 +28,23 @@ do_backup() { current_date=$(date +"%d_%m_%y_%H:%M") pushd $work_dir set +e - borg init -e repokey $repo >> $LOGFILE 2>> $ERRFILE + if borg init -e repokey $repo >> $LOGFILE 2>> $ERRFILE; then + human_size=`echo $size | awk '{ suffix=" KMGT"; for(i=1; $1>1024 && i < length(suffix); i++) $1/=1024; print int($1) substr(suffix, i, 1), $3; }'` + # Speed in Kbps + speed=1000 + evaluated_time=$(($size / ($speed * 1000 / 8) / 3600)) + echo "Hello, + +Your first backup on $repo is starting. The backup size is evaluated to $human_size. If connectivity is 1Mbps between the 2 servers, it will take about $evaluated_time hourâ‹…s to transmit. + +This is an automated message from your beloved YunoHost server." | /usr/bin/mail.mailutils -a "Content-Type: text/plain; charset=UTF-8" -s "[YNH] First backup is starting" "root" + fi set -e borg create $repo::${name}_${current_date} ./ >> $LOGFILE 2>> $ERRFILE popd - borg prune $repo -P ${name} --keep-daily=7 --keep-weekly=8 --keep-monthly=12 >> $LOGFILE 2>> $ERRFILE + borg prune $repo -P ${name} --keep-hourly 2 --keep-daily=7 --keep-weekly=8 --keep-monthly=12 >> $LOGFILE 2>> $ERRFILE } do_mount() { diff --git a/manifest.json b/manifest.json index 30e15dc..9f238c2 100644 --- a/manifest.json +++ b/manifest.json @@ -6,7 +6,7 @@ "en": "Backup your server with borg.", "fr": "Sauvegarder votre serveur avec borg." }, - "version": "1.0", + "version": "1.0.1", "url": "https://borgbackup.readthedocs.io", "license": "BSD-3-Clause", "maintainer": { @@ -15,7 +15,7 @@ "url": "https://reflexlibre.net" }, "requirements": { - "yunohost": ">= 2.7.2" + "yunohost": ">= 3.1" }, "multi_instance": true, "services": [], diff --git a/scripts/_common.sh b/scripts/_common.sh index 1613437..769670d 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -6,7 +6,7 @@ # 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 python-jinja2" +pkg_dependencies="python3-pip python3-dev libacl1-dev libssl-dev liblz4-dev python-jinja2 python3-setuptools" #================================================= # COMMON HELPERS @@ -132,3 +132,69 @@ $(yunohost tools diagnosis | grep -B 100 "services:" | sed '/services:/d')" # Send the email to the recipients echo "$mail_message" | $mail_bin -a "Content-Type: text/plain; charset=UTF-8" -s "$mail_subject" "$recipients" } + +# Read the value of a key in a ynh manifest file +# +# usage: ynh_read_manifest manifest key +# | arg: manifest - Path of the manifest to read +# | arg: key - Name of the key to find +ynh_read_manifest () { + manifest="$1" + key="$2" + python3 -c "import sys, json;print(json.load(open('$manifest', encoding='utf-8'))['$key'])" +} + + +# Checks the app version to upgrade with the existing app version and returns: +# - UPGRADE_APP if the upstream app version has changed +# - UPGRADE_PACKAGE if only the YunoHost package has changed +# +## It stops the current script without error if the package is up-to-date +# +# This helper should be used to avoid an upgrade of an app, or the upstream part +# of it, when it's not needed +# +# To force an upgrade, even if the package is up to date, +# you have to set the variable YNH_FORCE_UPGRADE before. +# example: sudo YNH_FORCE_UPGRADE=1 yunohost app upgrade MyApp + +# usage: ynh_check_app_version_changed +ynh_check_app_version_changed () { + local force_upgrade=${YNH_FORCE_UPGRADE:-0} + local package_check=${PACKAGE_CHECK_EXEC:-0} + + # By default, upstream app version has changed + local return_value="UPGRADE_APP" + + local current_version=$(ynh_read_manifest "/etc/yunohost/apps/$YNH_APP_INSTANCE_NAME/manifest.json" "version" || echo 1.0) + local current_upstream_version="${current_version/~ynh*/}" + local update_version=$(ynh_read_manifest "../manifest.json" "version" || echo 1.0) + local update_upstream_version="${update_version/~ynh*/}" + + if [ "$current_version" == "$update_version" ] ; then + # Complete versions are the same + if [ "$force_upgrade" != "0" ] + then + echo "Upgrade forced by YNH_FORCE_UPGRADE." >&2 + unset YNH_FORCE_UPGRADE + elif [ "$package_check" != "0" ] + then + echo "Upgrade forced for package check." >&2 + else + ynh_die "Up-to-date, nothing to do" 0 + fi + elif [ "$current_upstream_version" == "$update_upstream_version" ] ; then + # Upstream versions are the same, only YunoHost package versions differ + return_value="UPGRADE_PACKAGE" + fi + echo $return_value +} + + +ynh_install_backports () { + echo "deb http://httpredir.debian.org/debian stretch-backports main" > /etc/apt/sources.list.d/$app-stretch-backports.list +} + +ynh_remove_backports () { + rm /etc/apt/sources.list.d/$app-stretch-backports.list +} diff --git a/scripts/install b/scripts/install index 79f9105..a173fc5 100755 --- a/scripts/install +++ b/scripts/install @@ -29,12 +29,17 @@ ynh_export server ssh_user passphrase on_calendar conf data apps #================================================= ynh_save_args server ssh_user passphrase on_calendar conf data apps +#================================================= +# CONFIGURE BACKPORTS +#================================================= +# We need borg 1.1+ available only in backports +ynh_install_backports + #================================================= # STORE SETTINGS FROM MANIFEST #================================================= ynh_install_app_dependencies $pkg_dependencies -pip3 install setuptools --upgrade -pip3 install borgbackup +ynh_package_install -t stretch-backports borgbackup #================================================= # ACTIVATE BACKUP METHODS diff --git a/scripts/remove b/scripts/remove index 4be7c3c..fefc270 100755 --- a/scripts/remove +++ b/scripts/remove @@ -20,6 +20,11 @@ app=$YNH_APP_INSTANCE_NAME #================================================= ynh_remove_app_dependencies +#================================================= +# REMOVE BACKPORTS +#================================================= +ynh_remove_backports + #================================================= # REMOVE FILES #================================================= diff --git a/scripts/restore b/scripts/restore index 8f115d8..8b76574 100755 --- a/scripts/restore +++ b/scripts/restore @@ -31,11 +31,16 @@ server=$(ynh_app_setting_get $app server) ssh_user=$(ynh_app_setting_get $app ssh_user) #================================================= -# STORE SETTINGS FROM MANIFEST +# CONFIGURE BACKPORTS +#================================================= +# We need borg 1.1+ available only in backports +ynh_install_backports + +#================================================= +# INSTALL DEPENDENCIES #================================================= ynh_install_app_dependencies $pkg_dependencies -pip3 install setuptools --upgrade -pip3 install borgbackup +ynh_package_install -t stretch-backports borgbackup #================================================= # ACTIVATE BACKUP METHODS diff --git a/scripts/upgrade b/scripts/upgrade index 35d72d2..a2f1c92 100755 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -15,6 +15,11 @@ source /usr/share/yunohost/helpers app=$YNH_APP_INSTANCE_NAME +#================================================= +# CHECK IF AN UPGRADE IS NEEDED +#================================================= +ynh_check_app_version_changed + #================================================= # BACKUP BEFORE UPGRADE THEN ACTIVE TRAP #================================================= From 332e149da22f9a3370b8e2472349cbdb8433b8c7 Mon Sep 17 00:00:00 2001 From: ljf Date: Fri, 31 Aug 2018 01:36:36 +0200 Subject: [PATCH 2/3] [enh] Add check_process --- check_process | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 check_process diff --git a/check_process b/check_process new file mode 100644 index 0000000..38f45b8 --- /dev/null +++ b/check_process @@ -0,0 +1,36 @@ +;; Test complet + ; Manifest + server="domain.tld:22" + ssh_user="sam" + passphrase="APassphrase" + conf=1 + data=1 + app="all" + on_calendar="Daily" + ; Checks + pkg_linter=1 + setup_sub_dir=0 + setup_root=0 + setup_nourl=0 + setup_private=0 + setup_public=0 + upgrade=1 + backup_restore=1 + multi_instance=1 + incorrect_path=0 + port_already_use=0 + change_url=0 +;;; Levels + Level 1=auto + Level 2=auto + Level 3=auto + Level 4=1 + Level 5=auto + Level 6=auto + Level 7=auto + Level 8=0 + Level 9=0 + Level 10=0 +;;; Options +Email=ljf+borg_ynh@reflexlibre.net +Notification=down