Skip to content

Commit

Permalink
Harden some variable placement
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 8, 2017
1 parent f83bdc6 commit 2acb05c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 20 deletions.
7 changes: 2 additions & 5 deletions distributions/openhab/src/main/resources/bin/backup
Expand Up @@ -95,9 +95,9 @@ setup "$1"
## Copy userdata and conf folders
echo "Copying configuration to temporary folder..."
mkdir -p "$TempDir/userdata"
cp -a "$OPENHAB_USERDATA/"* "$TempDir/userdata"
cp -a "${OPENHAB_USERDATA:?}/"* "$TempDir/userdata"
mkdir -p "$TempDir/conf"
cp -a "$OPENHAB_CONF/"* "$TempDir/conf"
cp -a "${OPENHAB_CONF:?}/"* "$TempDir/conf"

## Remove non-transferable userdata files
echo "Removing unnecessary files..."
Expand All @@ -123,9 +123,6 @@ if [ "$OPENHAB_BACKUPS" = "$OPENHAB_USERDATA/backups" ]; then
rm -rf "$TempDir/userdata/backups"
fi

echo "$OPENHAB_BACKUPS"
echo "$OPENHAB_USERDATA/backups"

## Create archive
mkdir -p "$OPENHAB_BACKUPS"
echo "Zipping folder..."
Expand Down
53 changes: 38 additions & 15 deletions distributions/openhab/src/main/resources/bin/restore
Expand Up @@ -114,7 +114,7 @@ echo "Any file without a replacement will be deleted."
echo ""
printf "Okay to Continue? [y/N]: "
read -r answer
case $answer in
case "$answer" in
[Yy]*)
;;
*)
Expand All @@ -125,27 +125,50 @@ 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
}
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 "Moving over backup configuration"
command cp -af "$TempDir/conf/"* "$OPENHAB_CONF/" || {
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/" || {
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
Expand Down

0 comments on commit 2acb05c

Please sign in to comment.