/
restore
executable file
·189 lines (164 loc) · 5.55 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
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 ""