Browse Source

[mod] backup script rewritten (inspired and adpated from example_ynh).

Currently neither backup nor restore are useful but are safe enough to
not break the upgrade script. That's what matters for now.
pitchum 7 years ago
parent
commit
8cbe81be03
3 changed files with 194 additions and 4 deletions
  1. 130 0
      scripts/_common.sh
  2. 57 4
      scripts/backup
  3. 7 0
      scripts/upgrade

+ 130 - 0
scripts/_common.sh

@@ -0,0 +1,130 @@
+#!/bin/bash
+#
+# Common variables
+#
+
+pkg_dependencies="php5-fpm sipcalc dnsutils openvpn curl fake-hwclock"
+
+
+# Experimental helpers
+# Cf. https://github.com/YunoHost-Apps/Experimental_helpers/blob/72b0bc77c68d4a4a2bf4e95663dbc05e4a762a0a/ynh_read_manifest/ynh_read_manifest
+read_json () {
+    sudo python3 -c "import sys, json;print(json.load(open('$1'))['$2'])"
+}
+read_manifest () {
+    if [ -f '../manifest.json' ] ; then
+        read_json '../manifest.json' "$1"
+    else
+        read_json '../settings/manifest.json' "$1"
+    fi
+}
+
+
+# Helper to start/stop/.. a systemd service from a yunohost context,
+# *and* the systemd service itself needs to be able to run yunohost
+# commands.
+#
+# Hence the need to release the lock during the operation
+#
+# usage : ynh_systemctl yolo restart
+#
+function ynh_systemctl()
+{
+  local ACTION="$1"
+  local SERVICE="$2"
+  local LOCKFILE="/var/run/moulinette_yunohost.lock"
+
+  # Launch the action
+  sudo systemctl "$ACTION" "$SERVICE" &
+  local SYSCTLACTION=$!
+
+  # Save and release the lock...
+  cp $LOCKFILE $LOCKFILE.bkp.$$
+  rm $LOCKFILE
+
+  # Wait for the end of the action
+  wait $SYSCTLACTION
+
+  # Make sure the lock is released...
+  while [ -f $LOCKFILE ]
+  do
+    sleep 0.1
+  done
+
+  # Restore the old lock
+  mv $LOCKFILE.bkp.$$ $LOCKFILE
+}
+
+# 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'])"
+}
+
+# Read the upstream version from the manifest
+# The version number in the manifest is defined by <upstreamversion>~ynh<packageversion>
+# For example : 4.3-2~ynh3
+# This include the number before ~ynh
+# In the last example it return 4.3-2
+#
+# usage: ynh_app_upstream_version
+ynh_app_upstream_version () {
+    manifest_path="../manifest.json"
+    if [ ! -e "$manifest_path" ]; then
+        manifest_path="../settings/manifest.json"	# Into the restore script, the manifest is not at the same place
+    fi
+    version_key=$(ynh_read_manifest "$manifest_path" "version")
+    echo "${version_key/~ynh*/}"
+}
+
+# Read package version from the manifest
+# The version number in the manifest is defined by <upstreamversion>~ynh<packageversion>
+# For example : 4.3-2~ynh3
+# This include the number after ~ynh
+# In the last example it return 3
+#
+# usage: ynh_app_package_version
+ynh_app_package_version () {
+    manifest_path="../manifest.json"
+    if [ ! -e "$manifest_path" ]; then
+        manifest_path="../settings/manifest.json"	# Into the restore script, the manifest is not at the same place
+    fi
+    version_key=$(ynh_read_manifest "$manifest_path" "version")
+    echo "${version_key/*~ynh/}"
+}
+
+# Exit without error if the package is up to date
+#
+# This helper should be used to avoid an upgrade of a package
+# 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_abort_if_up_to_date
+ynh_abort_if_up_to_date () {
+	local force_upgrade=${YNH_FORCE_UPGRADE:-0}
+	local package_check=${PACKAGE_CHECK_EXEC:-0}
+
+	local version=$(ynh_read_manifest "/etc/yunohost/apps/$YNH_APP_INSTANCE_NAME/manifest.json" "version" || echo 1.0)
+	local last_version=$(ynh_read_manifest "../manifest.json" "version" || echo 1.0)
+	if [ "$version" = "$last_version" ]
+	then
+		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
+	fi
+}

+ 57 - 4
scripts/backup

@@ -1,9 +1,62 @@
 #!/bin/bash
 
-backup_dir="${1}/apps/vpnclient"
-mkdir -p "${backup_dir}/"
+#=================================================
+# GENERIC START
+#=================================================
+# IMPORT GENERIC HELPERS
+#=================================================
 
-sudo cp -a /etc/openvpn/keys/ "${backup_dir}/"
-sudo cp -a /etc/openvpn/client.conf.tpl "${backup_dir}/"
+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
+#=================================================
+
+ynh_abort_if_errors # Stop script if an error is detected
+
+#=================================================
+# LOAD SETTINGS
+#=================================================
+
+app=$YNH_APP_INSTANCE_NAME
+
+final_path=$(ynh_app_setting_get $app final_path)
+domain=$(ynh_app_setting_get $app domain)
+
+#=================================================
+# STANDARD BACKUP STEPS
+#=================================================
+# BACKUP THE APP MAIN DIR
+#=================================================
+
+ynh_backup "$final_path"
+
+#=================================================
+# BACKUP THE NGINX CONFIGURATION
+#=================================================
+
+ynh_backup "/etc/nginx/conf.d/$domain.d/${app}.conf"
+
+#=================================================
+# BACKUP THE PHP-FPM CONFIGURATION
+#=================================================
+
+ynh_backup "/etc/php5/fpm/pool.d/$app.conf"
+
+#=================================================
+# SPECIFIC BACKUP
+#=================================================
+# BACKUP SYSTEMD
+#=================================================
+
+ynh_backup "/etc/systemd/system/ynh-$app.service" # XXX maybe hard-coding the full name would be better
+ynh_backup "/etc/systemd/system/ynh-$app-checker.service" # XXX maybe hard-coding the full name would be better
+ynh_backup "/etc/systemd/system/ynh-$app-checker.timer" # XXX maybe hard-coding the full name would be better
 
 exit 0

+ 7 - 0
scripts/upgrade

@@ -36,6 +36,13 @@ if [ -f /etc/php5/fpm/pool.d/vpnadmin.conf ]; then
 fi
 test -d /var/www/vpnadmin && mv /var/www/vpnadmin /var/www/${app}
 
+# Versions known to have a buggy backup script
+buggy_versions="1.0.0 1.0.1 1.1.0"
+curr_version=$(read_manifest version)
+if echo $buggy_versions | grep -w $curr_version > /dev/null; then
+  echo "Your current version of ${app} is very old: ${curr_version}. Please ignore the next warning." >&2
+fi
+
 #=================================================
 # BACKUP BEFORE UPGRADE THEN ACTIVE TRAP
 #=================================================