/
restore
executable file
·167 lines (145 loc) · 5.07 KB
/
restore
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/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: 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 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
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/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 " -------------------"
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
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 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 ""