From 35d66636616eaa76bf1b91f9f69935f16d32f788 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 | 142 +++++++++++++ .../src/main/resources/bin/oh2_dir_layout | 5 + .../src/main/resources/bin/oh2_dir_layout.bat | 5 + .../openhab/src/main/resources/bin/restore | 189 ++++++++++++++++++ 4 files changed, 341 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..9ba08d954e --- /dev/null +++ b/distributions/openhab/src/main/resources/bin/backup @@ -0,0 +1,142 @@ +#!/bin/sh + +setup(){ + if [ "$1" = "--help" ] || [ "$1" = "-h" ]; then + echo "Usage: backup [filename]" + echo "" + echo " e.g. ./backup << Makes a file with a timed filename" + echo " ./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 "Set $OPENHAB_BACKUPS to change the default backup directory." + 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="$(cd "$(dirname "$0")" && cd ../.. && pwd -P)" + + 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 backing up the right thing. + if [ ! -d "$OPENHAB_USERDATA" ] || [ ! -d "$OPENHAB_CONF" ]; then + echo "Configuration paths are invalid..." >&2 + echo "Try setting 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 -a "${OPENHAB_USERDATA:?}/"* "$TempDir/userdata" +mkdir -p "$TempDir/conf" +cp -a "${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 do not include 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 + +## 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..b287c05dc1 --- /dev/null +++ b/distributions/openhab/src/main/resources/bin/restore @@ -0,0 +1,189 @@ +#!/bin/sh + +setup(){ + if [ -z "$1" ] || [ "$1" = "--help" ] || [ "$1" = "-h" ]; then + echo "Usage: restore filename" + echo "" + echo " e.g. ./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 "" + 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 restoring + if [ ! -z "$(pgrep -f "openhab2.*java")" ]; then + echo "openHAB is running! Please stop the process before restoring." >&2 + exit 1 + fi + + WorkingDir="$(cd "$(dirname "$0")" && cd ../.. && pwd -P)" + + ## 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="$WorkingDir/conf"; fi + if [ -z "$OPENHAB_USERDATA" ]; then OPENHAB_USERDATA="$WorkingDir/userdata"; fi + + echo "Using '$OPENHAB_CONF' as conf folder..." + echo "Using '$OPENHAB_USERDATA' as userdata folder..." + + ## 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 "Try setting OPENHAB_USERDATA and OPENHAB_CONF environment variables." >&2 + exit 1 + fi + + CurrentVersion="$(awk '/openhab-distro/{print $3}' "$OPENHAB_USERDATA/etc/version.properties")" + + ## Store anything in temporary folders + TempDir="/tmp/openhab2/restore" + ## 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 " -------------------" +echo " Backup Version | $BackupVersion (You are on $CurrentVersion)" +echo " Backup Timestamp | $BackupTime" +echo " Config belongs to user | $OHUser" +echo " from group | $OHGroup" +echo "" +echo "Your current configuration will become owned by $OHUser:$OHGroup." +echo "" +echo "Any existing files with the same name will be replaced." +echo "Any file without a replacement will be deleted." +echo "" +printf "Okay to Continue? [y/N]: " +read -r answer +case "$answer" in + [Yy]*) + ;; + *) + echo "Cancelling restore..." + rm -rf /tmp/openhab2 + exit 0 + ;; +esac + +## Move old configuration +rm -rf /tmp/openhab/old +mkdir -p /tmp/openhab/old +echo "Moving system files in userdata to temporary folder" +if [ -d "$OPENHAB_USERDATA/backups" ]; then + mv "$OPENHAB_USERDATA/backups" /tmp/openhab/old || { + echo "Could not move backup folder to temporary folder..." >&2 + exit 1 + } +fi +if [ -d "${OPENHAB_USERDATA:?}/etc" ]; then + mv "${OPENHAB_USERDATA:?}/etc" /tmp/openhab/old || { + echo "Could not move etc folder to temporary folder" >&2 + exit 1 + } +fi + +echo "Deleting old userdata folder..." +rm -rf "${OPENHAB_USERDATA:?}/"* + +echo "Restoring system files in userdata..." +if [ -d /tmp/openhab/old/backups ]; then + mv /tmp/openhab/old/backups "${OPENHAB_USERDATA:?}/" || { + echo "Unable to move other backup files back..." + exit 1 + } +fi +if [ -d /tmp/openhab/old/etc ]; then + mv /tmp/openhab/old/etc "${OPENHAB_USERDATA:?}/" || { + echo "Unable to move system files back..." + exit 1 + } +fi + +echo "Deleting old conf folder..." +rm -rf "${OPENHAB_CONF:?}/"* + +## Restore configuration +echo "Restoring openHAB with backup configuration..." +command cp -af "$TempDir/conf/"* "${OPENHAB_CONF:?}/" || { + echo "Failed to copy $TempDir/conf/ to $OPENHAB_CONF/..." >&2 + echo "Please check $TempDir and replace conf and userdata." >&2 + exit 1 +} +command cp -af "$TempDir/userdata/"* "${OPENHAB_USERDATA:?}/" || { + echo "Failed to copy $TempDir/userdata/ to $OPENHAB_USERDATA/..." >&2 + echo "Please check $TempDir and replace userdata." >&2 + exit 1 +} + +## Reset ownership +chown -R "$OHUser:$OHGroup" "$OPENHAB_USERDATA" || { + echo "Failed to change userdata permissions to $OHUser:$OHGroup" >&2 + exit 1 +} +chown -R "$OHUser:$OHGroup" "$OPENHAB_CONF" || { + echo "Failed to change conf permissions to $OHUser:$OHGroup" >&2 + exit 1 +} + +echo "Deleting temporary files..." +rm -rf /tmp/openhab2 +echo "Backup successfully restored!" +echo ""