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
Signed-off-by: Ben Clark <ben@benjyc.uk>
- Loading branch information
Showing
4 changed files
with
292 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,149 @@ | ||
#!/bin/sh | ||
|
||
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 "" | ||
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 "" | ||
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 "" | ||
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=$(pwd) | ||
|
||
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 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 | ||
|
||
## 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 -ar "$OPENHAB_USERDATA/"* "$TempDir/userdata" | ||
mkdir -p "$TempDir/conf" | ||
cp -ar "$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 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" | ||
fi | ||
|
||
echo "$OPENHAB_BACKUPS" | ||
echo "$OPENHAB_USERDATA/backups" | ||
|
||
## 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,133 @@ | ||
#!/bin/sh | ||
|
||
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 "" | ||
echo " e.g. ./runtime/bin/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 | ||
|
||
## 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 updating | ||
if [ ! -z "$(ps aux | grep "openhab2.*java" | grep -v grep)" ]; then | ||
echo "openHAB is running! Please stop the process before updating." >&2 | ||
exit 1 | ||
fi | ||
|
||
## 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 | ||
|
||
echo "Using '$OPENHAB_CONF' as conf folder..." | ||
echo "Using '$OPENHAB_USERDATA' as userdata folder..." | ||
|
||
CurrentVersion="$(awk '/openhab-distro/{print $3}' "$OPENHAB_USERDATA/etc/version.properties")" | ||
|
||
## Store anything in temporary folders | ||
TempDir="/tmp/openhab2/backup" | ||
## 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 " Backup Version | $BackupVersion (You are on $CurrentVersion)" | ||
echo " Backup Timestamp | $BackupTime" | ||
echo " Config belongs to user | $OHUser" | ||
echo " from group | $OHGroup" | ||
echo "=--------------------------------" | ||
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 "" | ||
printf "Okay to Continue? [y/N]: " | ||
read -r answer | ||
case $answer in | ||
[Yy]*) | ||
;; | ||
*) | ||
echo "Cancelling restore..." | ||
rm -rf /tmp/openhab2 | ||
exit 0 | ||
;; | ||
esac | ||
|
||
## Restore configuration | ||
echo "Moving over configuration" | ||
command cp -arf "$TempDir/conf/"* "$OPENHAB_CONF" | ||
command cp -arf "$TempDir/userdata/"* "$OPENHAB_USERDATA" | ||
|
||
## Reset ownership | ||
chown -R "$OHUser:$OHGroup" "$OPENHAB_USERDATA" | ||
chown -R "$OHUser:$OHGroup" "$OPENHAB_CONF" | ||
|
||
echo "Deleting temporary files..." | ||
rm -rf /tmp/openhab2 | ||
echo "Backup successfully restored!" | ||
echo "" |