From bb7ab9841c1a89776e94c82728fba3f1a7c675d1 Mon Sep 17 00:00:00 2001 From: Ben Clark Date: Thu, 3 Aug 2017 22:31:59 +0100 Subject: [PATCH] Add backup and restore scripts for Linux/MacOS Signed-off-by: Ben Clark --- .../openhab/src/main/resources/bin/backup | 149 ++++++++++++++++++ .../src/main/resources/bin/oh2_dir_layout | 5 + .../src/main/resources/bin/oh2_dir_layout.bat | 5 + .../openhab/src/main/resources/bin/restore | 133 ++++++++++++++++ 4 files changed, 292 insertions(+) create mode 100755 distributions/openhab/src/main/resources/bin/backup create mode 100755 distributions/openhab/src/main/resources/bin/restore diff --git a/distributions/openhab/src/main/resources/bin/backup b/distributions/openhab/src/main/resources/bin/backup new file mode 100755 index 0000000000..b92836e7d0 --- /dev/null +++ b/distributions/openhab/src/main/resources/bin/backup @@ -0,0 +1,149 @@ +#!/bin/sh + +setup(){ + ## Keep the script general by allowing the user to provide the version number to download. + if [ "$1" = "--help" ] || [ "$1" = "-h" ]; then + echo "Usage: ./runtime/bin/backup [filename]" + echo "" + echo " e.g. ./runtime/bin/backup << Makes a file with a timed filename" + echo " ./runtime/bin/backup myBackup.zip << Makes a file called myBackup.zip" + echo "" + echo "Use this script to backup your openHAB configuration, you can use the 'restore' script" + echo "from any machine to transfer your configuration across to another instance." + echo "" + echo "You can place this script anywhere, but you should run it from" + echo "inside the openHAB root folder if you do not have OPENHAB_* directories set." + echo "" + exit 0 + fi + + ## Ask to run as root to prevent us from running sudo in this script. + if [ "$(id -u)" -ne 0 ]; then + echo "Please run this script as root! (e.g. use sudo)" >&2 + exit 1 + fi + + command -v zip >/dev/null 2>&1 || { + echo "'zip' program was not found, please install it first." >&2 + exit 1 + } + + ## Set path variables + if [ -r /etc/profile.d/openhab2.sh ]; then + . /etc/profile.d/openhab2.sh + elif [ -r /etc/default/openhab2 ]; then + . /etc/default/openhab2 + fi + + WorkingDir=$(pwd) + + if [ -z "$OPENHAB_CONF" ]; then OPENHAB_CONF="$WorkingDir/conf"; fi + if [ -z "$OPENHAB_USERDATA" ]; then OPENHAB_USERDATA="$WorkingDir/userdata"; fi + if [ -z "$OPENHAB_BACKUPS" ]; then OPENHAB_BACKUPS="$WorkingDir/backups"; fi + + echo "Using '$OPENHAB_CONF' as conf folder..." + echo "Using '$OPENHAB_USERDATA' as userdata folder..." + + timestamp=$(date +"%y_%m_%d-%H_%M_%S") + ## Set the filename + if [ -z "$1" ]; then + echo "Using '$OPENHAB_BACKUPS' as backup folder..." + OutputFile="$OPENHAB_BACKUPS/openhab2-backup-$timestamp.zip" + else + OutputFile="$1" + fi + + ## Check two of the standard openHAB folders to make sure we're updating the right thing. + if [ ! -d "$OPENHAB_USERDATA" ] || [ ! -d "$OPENHAB_CONF" ]; then + echo "Configuration paths are invalid..." >&2 + echo "The script must be called from openHAB's root directory." >&2 + echo "Alternatively, set OPENHAB_USERDATA and OPENHAB_CONF environment variables." >&2 + exit 1 + fi + + ## Find the group and user of the current openHAB folders. + OHUser=$(ls -ld "$OPENHAB_USERDATA" | awk '{print $3}') + OHGroup=$(ls -ld "$OPENHAB_USERDATA" | awk '{print $4}') + + CurrentVersion="$(awk '/openhab-distro/{print $3}' "$OPENHAB_USERDATA/etc/version.properties")" + + ## Store anything in temporary folders + TempDir="/tmp/openhab2/backup" + echo "Making Temporary Directory" + mkdir -p "$TempDir" || { + echo "Failed to make temporary directory: $TempDir" >&2 + exit 1 + } + + ## Clear older stuff if it exists + rm -rf "${TempDir:?}/*" +} + +echo " " +echo "#########################################" +echo " openHAB 2.x.x backup script " +echo "#########################################" +echo " " + +setup "$1" + +## Set backup properties file. +{ + echo "version=$CurrentVersion" + echo "timestamp=$timestamp" + echo "user=$OHUser" + echo "group=$OHGroup" +} > "$TempDir/backup.properties" + +## Copy userdata and conf folders +echo "Copying configuration to temporary folder..." +mkdir -p "$TempDir/userdata" +cp -ar "$OPENHAB_USERDATA/"* "$TempDir/userdata" +mkdir -p "$TempDir/conf" +cp -ar "$OPENHAB_CONF/"* "$TempDir/conf" + +## Remove non-transferable userdata files +echo "Removing unnecessary files..." +rm -rf "$TempDir/userdata/cache" +rm -rf "$TempDir/userdata/tmp" +rm -rf "$TempDir/userdata/etc/all.policy" +rm -rf "$TempDir/userdata/etc/branding.properties" +rm -rf "$TempDir/userdata/etc/branding-ssh.properties" +rm -rf "$TempDir/userdata/etc/config.properties" +rm -rf "$TempDir/userdata/etc/custom.properties" +rm -rf "$TempDir/userdata/etc/version.properties" +rm -rf "$TempDir/userdata/etc/distribution.info" +rm -rf "$TempDir/userdata/etc/jre.properties" +rm -rf "$TempDir/userdata/etc/profile.cfg" +rm -rf "$TempDir/userdata/etc/startup.properties" +rm -rf "$TempDir/userdata/etc/org.apache.karaf"* +rm -rf "$TempDir/userdata/etc/org.ops4j.pax.url.mvn.cfg" + +## If the backup directory is inside userdata folder delete it +## Mainly for apt/rpm automatic +if [ "$OPENHAB_BACKUPS" = "$OPENHAB_USERDATA/backups" ]; then +echo "Backup Directory is inside userdata, not including in this backup!" +rm -rf "$TempDir/userdata/backups" +fi + +echo "$OPENHAB_BACKUPS" +echo "$OPENHAB_USERDATA/backups" + +## Create archive +mkdir -p "$OPENHAB_BACKUPS" +echo "Zipping folder..." + +## Jump into directory before making zip +## Cleans file structure +( cd "$TempDir" || exit + zip -qr "$OutputFile" . || { + echo "zip failed to store a backup." + exit 1 + } +) || exit 1 + +echo "Removing temporary files..." +rm -rf /tmp/openhab2 + +echo "Success! Backup made in $OutputFile" +echo "" diff --git a/distributions/openhab/src/main/resources/bin/oh2_dir_layout b/distributions/openhab/src/main/resources/bin/oh2_dir_layout index d4509e719d..2c9a5dd634 100644 --- a/distributions/openhab/src/main/resources/bin/oh2_dir_layout +++ b/distributions/openhab/src/main/resources/bin/oh2_dir_layout @@ -21,6 +21,11 @@ if [ -z ${OPENHAB_LOGDIR} ]; then export OPENHAB_LOGDIR="${OPENHAB_USERDATA}/logs" fi +if [ -z ${OPENHAB_BACKUP} ]; then + export OPENHAB_BACKUPS="${OPENHAB_HOME}/backups" +fi + + # Make sure the tmp folder exists as Karaf requires it if [ ! -d "${OPENHAB_USERDATA}/tmp" ]; then mkdir "${OPENHAB_USERDATA}/tmp" diff --git a/distributions/openhab/src/main/resources/bin/oh2_dir_layout.bat b/distributions/openhab/src/main/resources/bin/oh2_dir_layout.bat index 138a40fba8..ff7445674b 100644 --- a/distributions/openhab/src/main/resources/bin/oh2_dir_layout.bat +++ b/distributions/openhab/src/main/resources/bin/oh2_dir_layout.bat @@ -23,6 +23,11 @@ IF NOT [%OPENHAB_LOGDIR%] == [] GOTO :logs_set set OPENHAB_LOGDIR=%OPENHAB_USERDATA%\logs :logs_set +:check_backups +IF NOT [%OPENHAB_BACKUPS%] == [] GOTO :backups_set +set OPENHAB_BACKUPS=%OPENHAB_HOME%\backups +:backups_set + rem Make sure the tmp folder exists as Karaf requires it IF NOT EXIST "%OPENHAB_USERDATA%\tmp" ( mkdir "%OPENHAB_USERDATA%\tmp" diff --git a/distributions/openhab/src/main/resources/bin/restore b/distributions/openhab/src/main/resources/bin/restore new file mode 100755 index 0000000000..f8ce174faa --- /dev/null +++ b/distributions/openhab/src/main/resources/bin/restore @@ -0,0 +1,133 @@ +#!/bin/sh + +setup(){ + ## Keep the script general by allowing the user to provide the version number to download. + if [ -z "$1" ] || [ "$1" = "--help" ] || [ "$1" = "-h" ]; then + echo "Usage: ./runtime/bin/restore filename" + echo "" + echo " e.g. ./runtime/bin/restore myBackup.zip << Restores config from myBackup.zip" + echo "" + echo "Use this script to restore an openHAB configuration that was previously made with" + echo "the openHAB 'backup' script." + echo "" + echo "You can place this script anywhere, but you should run it from" + echo "inside the openHAB root folder if you do not have OPENHAB_* directories set." + echo "" + exit 0 + fi + + ## Ask to run as root to prevent us from running sudo in this script. + if [ "$(id -u)" -ne 0 ]; then + echo "Please run this script as root! (e.g. use sudo)" >&2 + exit 1 + fi + + command -v unzip >/dev/null 2>&1 || { + echo "'unzip' program was not found, please install it first." >&2 + exit 1 + } + + ## Check to see if processes are running before updating + if [ ! -z "$(ps aux | grep "openhab2.*java" | grep -v grep)" ]; then + echo "openHAB is running! Please stop the process before updating." >&2 + exit 1 + fi + + ## Set path variables + if [ -r /etc/profile.d/openhab2.sh ]; then + . /etc/profile.d/openhab2.sh + elif [ -r /etc/default/openhab2 ]; then + . /etc/default/openhab2 + fi + if [ -z "$OPENHAB_CONF" ]; then OPENHAB_CONF="./conf"; fi + if [ -z "$OPENHAB_USERDATA" ]; then OPENHAB_USERDATA="./userdata"; fi + + echo "Using '$OPENHAB_CONF' as conf folder..." + echo "Using '$OPENHAB_USERDATA' as userdata folder..." + + CurrentVersion="$(awk '/openhab-distro/{print $3}' "$OPENHAB_USERDATA/etc/version.properties")" + + ## Store anything in temporary folders + TempDir="/tmp/openhab2/backup" + ## Clear older stuff if it exists + rm -rf "${TempDir:?}" + echo "Making Temporary Directory" + mkdir -p "$TempDir" || { + echo "Failed to make temporary directory: $TempDir" >&2 + exit 1 + } +} + +echo " " +echo "##########################################" +echo " openHAB 2.x.x restore script " +echo "##########################################" +echo " " + +InputFile="$1" + +setup "$InputFile" + +## Extract zip file +echo "Extracting zip file to temporary folder." + +unzip -oq "$InputFile" -d "$TempDir" || { + echo "Unable to unzip $InputFile, Aborting..." >&2 + exit 1 +} + +## Check for backup properties list. +if [ ! -f "$TempDir/backup.properties" ]; then + echo "Backup was not created by openHAB scripts, please resort to a manual restore..." >&2 + exit 1 +fi + +## Grab information with backup.properties +str="$(awk '/version=/{print $1}' "$TempDir/backup.properties")" +BackupVersion=${str#*=} +str="$(awk '/timestamp=/{print $1}' "$TempDir/backup.properties")" +BackupTime=${str#*=} +str="$(awk '/user=/{print $1}' "$TempDir/backup.properties")" +OHUser=${str#*=} +str="$(awk '/group=/{print $1}' "$TempDir/backup.properties")" +OHGroup=${str#*=} + +## Feeback to user +echo "---------------------------------" +echo " Backup Information: |" +echo " Backup Version | $BackupVersion (You are on $CurrentVersion)" +echo " Backup Timestamp | $BackupTime" +echo " Config belongs to user | $OHUser" +echo " from group | $OHGroup" +echo "=--------------------------------" +echo "" +echo "Your current configuration will become owned by $OHUser:$OHGroup." +echo "" +echo "Any existing files with the same name will be replaced, but" +echo "this will NOT delete existing files that have no replacement." +echo "" +printf "Okay to Continue? [y/N]: " +read -r answer +case $answer in + [Yy]*) + ;; + *) + echo "Cancelling restore..." + rm -rf /tmp/openhab2 + exit 0 + ;; +esac + +## Restore configuration +echo "Moving over configuration" +command cp -arf "$TempDir/conf/"* "$OPENHAB_CONF" +command cp -arf "$TempDir/userdata/"* "$OPENHAB_USERDATA" + +## Reset ownership +chown -R "$OHUser:$OHGroup" "$OPENHAB_USERDATA" +chown -R "$OHUser:$OHGroup" "$OPENHAB_CONF" + +echo "Deleting temporary files..." +rm -rf /tmp/openhab2 +echo "Backup successfully restored!" +echo ""