diff --git a/distributions/openhab/src/main/resources/bin/backup b/distributions/openhab/src/main/resources/bin/backup index ee8c9bd2a2..3e66004ead 100755 --- a/distributions/openhab/src/main/resources/bin/backup +++ b/distributions/openhab/src/main/resources/bin/backup @@ -3,17 +3,15 @@ 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 "Usage: 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 " 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 "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 "" + echo "Set $OPENHAB_BACKUPS to change the default backup directory." exit 0 fi @@ -76,7 +74,7 @@ setup(){ } ## Clear older stuff if it exists - rm -rf "${TempDir:?}/*" + rm -rf "${TempDir:?}/"* } echo " " @@ -122,8 +120,8 @@ 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" + echo "Backup Directory is inside userdata, not including in this backup!" + rm -rf "$TempDir/userdata/backups" fi echo "$OPENHAB_BACKUPS" diff --git a/distributions/openhab/src/main/resources/bin/restore b/distributions/openhab/src/main/resources/bin/restore index 87af95b83f..c078cdc386 100755 --- a/distributions/openhab/src/main/resources/bin/restore +++ b/distributions/openhab/src/main/resources/bin/restore @@ -3,9 +3,9 @@ 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 "Usage: restore filename" echo "" - echo " e.g. ./runtime/bin/restore myBackup.zip << Restores config from myBackup.zip" + 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." @@ -33,18 +33,28 @@ setup(){ 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="./conf"; fi - if [ -z "$OPENHAB_USERDATA" ]; then OPENHAB_USERDATA="./userdata"; 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 "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 + CurrentVersion="$(awk '/openhab-distro/{print $3}' "$OPENHAB_USERDATA/etc/version.properties")" ## Store anything in temporary folders @@ -103,8 +113,8 @@ 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, but" -echo "this will NOT delete existing files that have no replacement." +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 @@ -118,14 +128,42 @@ case $answer in ;; esac +## Move old configuration +echo "Deleting current configuration" +( cd "$OPENHAB_USERDATA" + if [ ! -d "./jsondb" ] || [ ! -d "./etc" ]; then + echo "Couldn't find anything sensible in $OPENHAB_USERDATA skipping..." + else + find . ! -name 'etc' ! -name '.' ! -name 'backups' -type d -exec rm -rf {} + + fi +) || { + echo "Failed to delete current userdata contents." >&2 + exit 1 +} +rm -rf "${OPENHAB_CONF:?}/"* + ## Restore configuration -echo "Moving over configuration" -command cp -af "$TempDir/conf/"* "$OPENHAB_CONF" -command cp -af "$TempDir/userdata/"* "$OPENHAB_USERDATA" +echo "Moving over 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" -chown -R "$OHUser:$OHGroup" "$OPENHAB_CONF" +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