Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add backup and restore scripts for Linux/MacOS (#507)
Signed-off-by: Ben Clark <ben@benjyc.uk>
- Loading branch information
1 parent
661134b
commit 334f234
Showing
4 changed files
with
341 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 "" |