Skip to content

Commit

Permalink
Completely reset userdata and conf folders...
Browse files Browse the repository at this point in the history
Signed-off-by: Ben Clark <ben@benjyc.uk>
  • Loading branch information
BClark09 committed Aug 6, 2017
1 parent 97d8473 commit f83bdc6
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 26 deletions.
21 changes: 9 additions & 12 deletions distributions/openhab/src/main/resources/bin/backup
Expand Up @@ -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

Expand Down Expand Up @@ -53,11 +51,10 @@ setup(){
OutputFile="$1"
fi

## Check two of the standard openHAB folders to make sure we're updating the right thing.
## 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 "The script must be called from openHAB's root directory." >&2
echo "Alternatively, set OPENHAB_USERDATA and OPENHAB_CONF environment variables." >&2
echo "Try setting OPENHAB_USERDATA and OPENHAB_CONF environment variables." >&2
exit 1
fi

Expand All @@ -76,7 +73,7 @@ setup(){
}

## Clear older stuff if it exists
rm -rf "${TempDir:?}/*"
rm -rf "${TempDir:?}/"*
}

echo " "
Expand Down Expand Up @@ -122,8 +119,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"
Expand Down
62 changes: 48 additions & 14 deletions distributions/openhab/src/main/resources/bin/restore
Expand Up @@ -3,16 +3,13 @@
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."
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

Expand All @@ -33,18 +30,27 @@ 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 "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
Expand Down Expand Up @@ -103,8 +109,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
Expand All @@ -118,14 +124,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 . -maxdepth 1 ! -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
Expand Down

0 comments on commit f83bdc6

Please sign in to comment.