Skip to content

Commit 2e1fcc2

Browse files
authored
Merge pull request #9 from PLCnext/Fix_Backup.sh_Issues
Fix backup.sh issues
2 parents b0d1329 + b460bf1 commit 2e1fcc2

4 files changed

Lines changed: 180 additions & 87 deletions

File tree

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.md text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2
2+
*.sh text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2

Backup/backupSD.sh

Lines changed: 89 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,8 @@ if [[ $DATAPATH == "" && $StoreBackupAt == "" ]]; then
1616
fi
1717
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
1818

19-
STORE_LICENCE=false
20-
STORE_DEVICEDATA=false
21-
22-
STORE_UNIX_PASSWORD_CHANGES=true
23-
STORE_PLCnextStuff=true
24-
STORE_PLCnext_UM=true
25-
STORE_NETWORK=true
26-
STORE_SSH_KEYs=true
27-
STORE_PROJECTS=true
28-
STORE_INSTALLED_APPS=true
29-
STORE_CONFIG=true
30-
STORE_CERTS=true
31-
STORE_DATA=true
32-
33-
UNIX_PASSWORD_Changes=( "upperdir/etc/shadow" "upperdir/etc/gshadow" "upperdir/etc/group" ) # created root user or admin unix pw was chaged.
34-
PLCnextStuff="upperdir/etc/plcnext" # All PCnext Stuff that has been modified. Database files for Firewall rules etc.
35-
PLCnext_UM="upperdir/etc/plcnext/device/System/Um/Roles.settings" #
36-
NETWORK="upperdir/etc/network" # Interface file
37-
SSH_KEYs="upperdir/etc/ssh" # Unix Keys / not the Cert Storage
38-
PROJECTS="upperdir/opt/plcnext/projects" # PCWE and other Project changes.
39-
INSTALLED_APPS="upperdir/opt/plcnext/appshome" #
40-
CONFIG="upperdir/opt/plcnext/config" #
41-
CERTS="upperdir/opt/plcnext/Security" #
42-
DATA="upperdir/opt/plcnext/data" #
43-
INSTALLED_LICENSE_FILES="licence" # SD card Licenses
44-
DEVICEDATA="device_data" # Production Data of PLC.
19+
## Set the directories for the backup
20+
. ${SCRIPT_DIR}/../Backup/backupSettings.sh
4521

4622
df -ha | grep rfs
4723
${SCRIPT_DIR}/../Diagnostic/checkActivePartition.sh
@@ -53,7 +29,11 @@ fi
5329

5430
echo 'Choose the kind of Backup you want to create.'
5531
PS3="Select your choice: "
56-
options=("Upperdir" "PCWE Project only" "Changes done via FW Services" "Continue" "Exit")
32+
options=("Upperdir" \
33+
"PCWE Project only" \
34+
"Directories specified by backupSettings.sh" \
35+
"Continue" \
36+
"Exit")
5737
select opt in "${options[@]}"
5838
do
5939
case $opt in
@@ -67,11 +47,23 @@ do
6747
echo "STORE = $STORE"
6848
;;
6949

70-
"Changes done via FW Services")
50+
"Directories specified by backupSettings.sh")
51+
echo "check 'backupSettings.sh' to check or change settings"
52+
(set -o posix ; set) | grep STORE | grep true
7153
STORE="Changes"
7254
echo "STORE = $STORE"
7355
;;
74-
56+
57+
# "Everything")
58+
# STORE="Everything"
59+
# echo "STORE = $STORE"
60+
# ;;
61+
62+
# "Containers")
63+
# STORE="Everything"
64+
# echo "STORE = $STORE"
65+
# ;;
66+
7567
"Continue")
7668
if [ "$STORE" == "" ]; then
7769
echo "STORE = $STORE empty Exit Script"
@@ -134,12 +126,15 @@ done
134126
echo "Please check if the paths are correct."
135127
echo "DataPath: '$DATAPATH' The directory you want to backup."
136128
echo "StoreBackupAt: '$StoreBackupAt' the location you want to store your backup at."
137-
echo "STORE Mode: '$STORE' is activated."
138-
echo "STORE_LICENCE: '$STORE_LICENCE' is activated."
139-
echo "STORE_DEVICEDATA: '$STORE_DEVICEDATA' is activated."
129+
echo "Storage Mode: '$STORE' is activated."
130+
140131

132+
echo ":"
141133
read -r -p "Are you sure? [y/N] all settings are correct
142134
" response
135+
###
136+
### Storage Function
137+
###
143138
if [[ "$response" =~ ^([yY][eE][sS]|[yY])$ ]]
144139
then
145140
DATE=$(date -I)
@@ -149,78 +144,93 @@ then
149144
echo "STORE = $STORE"
150145
case "$STORE" in
151146
"Upperdir")
152-
tar -rpf ${StoreBackupAt}/backup-${DATE}.tar -C ${DATAPATH} upperdir;
147+
tar -rpf ${StoreBackupAt}/backup-${DATE}.tar -C ${DATAPATH} upperdir && echo "Done"
153148
;;
154149
"Changes")
155-
echo -e "Store specific folders only:"
156-
echo "STORE_UNIX_PASSWORD_CHANGES: $STORE_UNIX_PASSWORD_CHANGES
157-
| UNIX_PASSWORD_Changes: ${UNIX_PASSWORD_Changes[@]} "
158-
echo "STORE_NETWORK: $STORE_NETWORK
159-
| NETWORK: $NETWORK "
160-
echo "STORE_SSH_KEYs: $STORE_SSH_KEYs
161-
| SSH_KEYs: $SSH_KEYs "
162-
echo "STORE_PROJECTS: $STORE_PROJECTS
163-
| PROJECTS: $PROJECTS "
164-
echo "STORE_INSTALLED_APPS: $STORE_INSTALLED_APPS
165-
| INSTALLED_APPS: $INSTALLED_APPS "
166-
echo "STORE_CONFIG: $STORE_CONFIG
167-
| CONFIG: $CONFIG "
168-
echo "STORE_CERTS: $STORE_CERTS
169-
| CERTS: $CERTS "
170-
echo "STORE_DATA: $STORE_DATA
171-
| DATA: $DATA "
172-
150+
echo -e "Store specified folders only:"
151+
(set -o posix ; set) | grep "STORE" | grep "true"
173152
read -r -p "Proceed creating backup [y/N] : " response
174153
if [[ "$response" =~ ^([yY][eE][sS]|[yY])$ ]]
175154
then
176155
## Add Folders to Tar.
177156
if $STORE_UNIX_PASSWORD_CHANGES; then
178-
echo "STORED UNIX_PASSWORD_CHANGES"
179-
tar -rpf ${StoreBackupAt}/backup-${DATE}.tar -C ${DATAPATH} ${UNIX_PASSWORD_Changes[@]}
157+
echo "Storing UNIX_PASSWORD_CHANGES"
158+
tar -rpf ${StoreBackupAt}/backup-${DATE}.tar -C ${DATAPATH} ${UNIX_PASSWORD_Changes[@]} && echo "Done"
180159
fi
181160
if $STORE_NETWORK; then
182-
echo "STORED NETWORK"
183-
tar -rpf ${StoreBackupAt}/backup-${DATE}.tar -C ${DATAPATH} ${NETWORK}
161+
echo "Storing Network Settings"
162+
tar -rpf ${StoreBackupAt}/backup-${DATE}.tar -C ${DATAPATH} ${NETWORK} && echo "Done"
184163
fi
164+
185165
if $STORE_SSH_KEYs; then
186-
echo "STORED SSH_KEYs"
187-
tar -rpf ${StoreBackupAt}/backup-${DATE}.tar -C ${DATAPATH} ${SSH_KEYs}
166+
echo "Storing SSH Keys"
167+
tar -rpf ${StoreBackupAt}/backup-${DATE}.tar -C ${DATAPATH} ${SSH_KEYs} && echo "Done"
188168
fi
189-
if $STORE_PROJECTS; then
190-
echo "STORED PROJECTS"
191-
tar -rpf ${StoreBackupAt}/backup-${DATE}.tar -C ${DATAPATH} ${PROJECTS}
169+
170+
if $STORE_PLCnext_PROJECTS; then
171+
echo "Storing PROJECTS"
172+
tar -rpf ${StoreBackupAt}/backup-${DATE}.tar -C ${DATAPATH} ${PROJECTS} && echo "Done"
192173
fi
193-
if $STORE_INSTALLED_APPS; then
194-
echo "STORED INSTALLED_APPS"
195-
tar -rpf ${StoreBackupAt}/backup-${DATE}.tar -C ${DATAPATH} ${INSTALLED_APPS}
174+
175+
if $STORE_PLCnext_SYSTEM_UM; then
176+
echo "Storing SYSTEM UM Settings"
177+
tar -rpf ${StoreBackupAt}/backup-${DATE}.tar -C ${DATAPATH} ${PLCnext_UM}&& echo "Done"
196178
fi
197-
if $STORE_CONFIG; then
198-
echo "STORED CONFIG"
199-
tar -rpf ${StoreBackupAt}/backup-${DATE}.tar -C ${DATAPATH} ${CONFIG}
179+
180+
if $STORE_PLCnext_SYSTEM_SCM; then
181+
echo "Storing SYSTEM SCM Settings"
182+
tar -rpf ${StoreBackupAt}/backup-${DATE}.tar -C ${DATAPATH} ${PLCnext_SCM} && echo "Done"
200183
fi
201-
if $STORE_CERTS; then
202-
echo "STORED CERTS"
203-
tar -rpf ${StoreBackupAt}/backup-${DATE}.tar -C ${DATAPATH} ${CERTS}
184+
185+
if $STORE_PLCNext_SERVICES; then
186+
echo "Storing SERVICE Settings"
187+
tar -rpf ${StoreBackupAt}/backup-${DATE}.tar -C ${DATAPATH} ${SERVICES} && echo "Done"
188+
### If required add keys for specific services
189+
#STORE_PLCNext_SERVICES_Ehmi
190+
#STORE_PLCNext_SERVICES_Grpc
191+
#STORE_PLCNext_SERVICES_LinuxSyslog
192+
#STORE_PLCNext_SERVICES_OpcUA
193+
#STORE_PLCNext_SERVICES_PLCnextStore
194+
#STORE_PLCNext_SERVICES_PortAuthentication
195+
#STORE_PLCNext_SERVICES_Spm
196+
#STORE_PLCNext_SERVICES_SpnsProxy
197+
#STORE_PLCNext_SERVICES_Syslog
198+
#STORE_PLCNext_SERVICES_Wcm
204199
fi
205-
if $STORE_DATA; then
206-
echo "STORED DATA"
207-
tar -rpf ${StoreBackupAt}/backup-${DATE}.tar -C ${DATAPATH} ${DATA}
200+
if $STORE_PLCnext_SECURITY; then
201+
echo "Storing PLCnext_SECURITY directory"
202+
tar -rpf ${StoreBackupAt}/backup-${DATE}.tar -C ${DATAPATH} ${PLCnext_SECURITY} && echo "Done"
203+
fi
204+
if $STORE_INSTALLED_APPS; then
205+
echo "Storing INSTALLED_APPS"
206+
tar -rpf ${StoreBackupAt}/backup-${DATE}.tar -C ${DATAPATH} ${INSTALLED_APPS} && echo "Done"
208207
fi
208+
if $STORE_ALL_CONFIG; then
209+
echo "Storing CONFIG directory"
210+
tar -rpf ${StoreBackupAt}/backup-${DATE}.tar -C ${DATAPATH} ${CONFIG} && echo "Done"
211+
fi
212+
213+
if $STORE_ALL_DATA; then
214+
echo "Storing DATA directory"
215+
tar -rpf ${StoreBackupAt}/backup-${DATE}.tar -C ${DATAPATH} ${DATA} && echo "Done"
216+
fi
217+
218+
209219
fi
210220
;;
211221

212222
"PCWE")
213-
echo "STORED PCWE"
214-
tar -rpf ${StoreBackupAt}/backup-${DATE}.tar -C ${DATAPATH} ${PROJECTS}
223+
echo "Storing PCWE directory"
224+
tar -rpf ${StoreBackupAt}/backup-${DATE}.tar -C ${DATAPATH} ${PROJECTS} && echo "Done"
215225
;;
216226
esac
217227
fi
218228

219229
if $STORE_DEVICEDATA; then
220230
echo "WATCH OUT!
221231
Stored DeviceData are bound to the Hardware of the PLC do not use this backup for another PLC!"
222-
echo "STORED DEVICEDATA"
223-
tar -rpf ${StoreBackupAt}/backup-${DATE}.tar -C "/etc" ${DEVICEDATA}
232+
echo "Storing DEVICEDATA"
233+
tar -rpf ${StoreBackupAt}/backup-${DATE}.tar -C "/etc" ${DEVICEDATA} && echo "Done"
224234
fi
225235

226236
if $STORE_LICENCE; then
@@ -236,7 +246,7 @@ if $STORE_LICENCE; then
236246
then
237247
echo "Adding Licence, remember to unzip with caution!"
238248
echo "Restoring this backup might void an SD-Cards License if overwritten."
239-
tar -rpf ${StoreBackupAt}/backup-${DATE}.tar -C ${DATAPATH} ${INSTALLED_LICENSE_FILES}
249+
tar -rpf ${StoreBackupAt}/backup-${DATE}.tar -C ${DATAPATH} ${INSTALLED_LICENSE_FILES} && echo "Done"
240250
else
241251
echo "Storing licence has been skipped"
242252
fi
@@ -245,10 +255,10 @@ fi
245255
if [ -f "${StoreBackupAt}/backup-${DATE}.tar" ]; then
246256
chown admin:plcnext ${StoreBackupAt}/backup-*.tar
247257
echo "Contents of Backup:"
248-
tar --exclude "*/*" -tvf "${StoreBackupAt}/backup-${DATE}.tar"
258+
tar -tvf "${StoreBackupAt}/backup-${DATE}.tar" | grep "/$"
249259

250260
echo "How to restore:
251-
./restoreBackup.sh /media/rfs/rw/ /media/rfs/rw/backup-2021-09-10.tar
261+
./restoreBackup.sh /media/rfs/rw/ /media/rfs/rw/backup-${DATE}.tar
252262
"
253263
else
254264
echo "No backup created please fix your input data."

Backup/backupSettings.sh

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/bin/bash
2+
# ******************************************************************************
3+
# *
4+
# * Copyright (c) 2021 Phoenix Contact GmbH & Co. KG. All rights reserved.
5+
# * Licensed under the MIT. See LICENSE file in the project root for full license information.
6+
# *
7+
# ******************************************************************************
8+
9+
##
10+
## All possible options for the backup.sh script
11+
##
12+
STORE_LICENCE=false # SD card Licenses
13+
INSTALLED_LICENSE_FILES="licence"
14+
15+
STORE_DEVICEDATA=false # Production Data of PLC.
16+
DEVICEDATA="device_data"
17+
18+
STORE_UNIX_PASSWORD_CHANGES=true # Created root user, added groups etc.
19+
UNIX_PASSWORD_Changes=( "upperdir/etc/shadow" "upperdir/etc/gshadow" "upperdir/etc/group" ) # created root user or admin unix pw was chaged.
20+
21+
STORE_PLCnext_ETC=false # All modifications done to the /etc/plcnext directory
22+
PLCnext_ETC="upperdir/etc/plcnext"
23+
24+
#STORE_PLCnext_SYSTEM
25+
STORE_PLCnext_SYSTEM_UM=true # Added roles and Permissions as well as Ldap Config
26+
PLCnext_UM="upperdir/opt/plcnext/config/System/Um/"
27+
28+
STORE_PLCnext_SYSTEM_SCM=true # Service Manager changes to enable/disable System Features
29+
PLCnext_SCM="upperdir/opt/plcnext/config/System/Scm/"
30+
31+
STORE_PLCnext_SECURITY=true # Certificates IdentityStores and Truststores
32+
PLCnext_SECURITY="upperdir/opt/plcnext/Security"
33+
34+
STORE_PLCnext_PROJECTS=true # PCWE and other Project changes.
35+
PROJECTS="upperdir/opt/plcnext/projects"
36+
37+
STORE_PLCNext_SERVICES=true # Service Specific Settings
38+
SERVICES="upperdir/opt/plcnext/config/Services"
39+
40+
STORE_NETWORK=true # Store your custom IP Configuration
41+
NETWORK="upperdir/etc/network"
42+
43+
STORE_SSH_KEYs=true # Unix SSH Keys
44+
SSH_KEYs="upperdir/etc/ssh"
45+
46+
STORE_INSTALLED_APPS=false #
47+
INSTALLED_APPS="upperdir/opt/plcnext/appshome"
48+
49+
STORE_ALL_CONFIG=false # Whole config directory
50+
CONFIG="upperdir/opt/plcnext/config"
51+
52+
STORE_ALL_DATA=false # this directory holds mostly session dependend data like PIDs, FW update Status etc.
53+
DATA="upperdir/opt/plcnext/data"

Backup/restoreBackup.sh

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,27 +31,55 @@ select opt in "${options[@]}"
3131
do
3232
case $opt in
3333
"SD Licence")
34-
echo "Restore Licence."
34+
echo "Restoring Licence"
35+
36+
CURRENT_SD_LICENCE=($(ls /media/rfs/externalsd/licence))
37+
BACKUP_SD_LICENCE=($(tar -tf "${BackupLocation}" licence | sed 's/licence\///' | sed -r '/^\s*$/d'))
38+
39+
echo "CURRENT SD LICENCE: ${CURRENT_SD_LICENCE[@]}"
40+
echo "BACKUP SD LICNENCE: ${BACKUP_SD_LICENCE[@]}"
41+
if [ ${CURRENT_SD_LICENCE[0]} != ${BACKUP_SD_LICENCE[1]} ] | [ ${CURRENT_SD_LICENCE[1]} != ${BACKUP_SD_LICENCE[0]} ]; then
42+
## This is not comparing the actuall licence but the randomly generated filename
43+
## is a pretty good indicator that the licence file does not belong to this device.
44+
echo "####ATTENTION####"
45+
echo "Backup differs from current device significantly"
46+
echo "$CURRENT_SD_LICENCE != $BACKUP_SD_LICENCE"
47+
echo "Are you sure this is the same Device this Backup was created from?"
48+
fi
49+
3550
echo "Restoring this backup might void an SD-Cards License if written to a different SD-Card it was created from."
3651
read -r -p "Are you sure? [y/N] " response
37-
tar --same-owner -xf "${BackupLocation}" -C "$TargetDirectory" licence
52+
tar --same-owner -xvf "${BackupLocation}" -C "$TargetDirectory" licence && echo "Done restoring License"
3853
;;
3954

4055
"DeviceData")
41-
echo "Restore DeviceData."
42-
echo "Restoring this backup might destroy PLC data if written to a different PLC it was created from."
43-
read -r -p "Are you sure? [y/N] " response
44-
tar --same-owner -xf "${BackupLocation}" -C "/etc" device_data
56+
echo "Restoring DeviceData."
57+
echo "Restoring this backup might destroy your PLC if written to a different PLC it was created from."
58+
CURRENT_SERIAL_NUMBER=$(cat /etc/device_data/phoenixsign/production_data | grep "OEM_SERIAL=" | sed 's/;//' )
59+
BACKUP_SERIAL_NUMBER=$(tar xf "${BackupLocation}" device_data/phoenixsign/production_data -O | grep "OEM_SERIAL=" | sed 's/;//')
60+
61+
echo "CURRENT SN: $CURRENT_SERIAL_NUMBER"
62+
echo "BACKUP SN: $BACKUP_SERIAL_NUMBER"
63+
64+
if [ $CURRENT_SERIAL_NUMBER != $BACKUP_SERIAL_NUMBER ]; then
65+
echo "####ATTENTION####"
66+
echo "Backup SN differs from Device SN"
67+
echo "$BACKUP_SERIAL_NUMBER != $CURRENT_SERIAL_NUMBER"
68+
echo "Are you sure this is the same Device this Backup was created from?"
69+
fi
70+
71+
read -r -p "Continue restoring backup to device [Yes/N]" response
72+
tar --same-owner -xvf "${BackupLocation}" -C "/etc" device_data && echo "Done restoring DeviceData"
4573
;;
4674

4775
"PCWE")
4876
echo "Restore PCWE Folder from Backup."
49-
tar --same-owner -xf "${BackupLocation}" -C "$TargetDirectory" upperdir/opt/plcnext/projects/PCWE
77+
tar --same-owner -xvf "${BackupLocation}" -C "$TargetDirectory" upperdir/opt/plcnext/projects/PCWE && echo "Done restoring PCWE"
5078
;;
5179

5280
"Upperdir")
5381
echo "Restore Upperdir"
54-
tar --same-owner -xf "${BackupLocation}" -C "$TargetDirectory" upperdir
82+
tar --same-owner -xvf "${BackupLocation}" -C "$TargetDirectory" upperdir && echo "Done restoring Upperdir"
5583
;;
5684

5785
"Exit")

0 commit comments

Comments
 (0)