From c4383dd07a6fc6e08ede862aaa0ca8d4ef34e9d7 Mon Sep 17 00:00:00 2001 From: OWarneke Date: Mon, 31 Jan 2022 11:31:03 +0100 Subject: [PATCH] Adding Option to store Containers as images. --- Backup/backupSD.sh | 56 ++++++++++++++++++++++++++++++++++------ Backup/backupSettings.sh | 5 +++- Backup/restoreBackup.sh | 26 ++++++++++++++++++- 3 files changed, 77 insertions(+), 10 deletions(-) diff --git a/Backup/backupSD.sh b/Backup/backupSD.sh index a6f4f6d..65fc9d7 100644 --- a/Backup/backupSD.sh +++ b/Backup/backupSD.sh @@ -32,6 +32,7 @@ PS3="Select your choice: " options=("Upperdir" \ "PCWE Project only" \ "Directories specified by backupSettings.sh" \ +"Containers" \ "Continue" \ "Exit") select opt in "${options[@]}" @@ -58,12 +59,7 @@ do # STORE="Everything" # echo "STORE = $STORE" # ;; - - # "Containers") - # STORE="Everything" - # echo "STORE = $STORE" - # ;; - + "Continue") if [ "$STORE" == "" ]; then echo "STORE = $STORE empty Exit Script" @@ -83,7 +79,15 @@ done echo 'Do you wish to add stored licenses and PLC device data to this backup?' PS3="Select your choice: " -options=("Don't Store Licence" "Store Licence" "Don't Store DeviceData" "Store DeviceData" "Continue" "Exit") +options=("Don't Store Licence" +"Store Licence" +"Don't Store DeviceData" +"Store DeviceData" +"Don't Store Containers" +"Store Containers" +"Continue" +"Exit" +) select opt in "${options[@]}" do case $opt in @@ -103,15 +107,29 @@ do STORE_DEVICEDATA=true echo "STORE_DEVICEDATA = $STORE_DEVICEDATA" ;; + "Store Containers") + STORE_CONTAINER=true + echo "STORE = $STORE" + ;; + "Dont Store Containers") + STORE_CONTAINER=false + echo "STORE = $STORE" + ;; "Continue") if [ "$STORE_LICENCE" == "" ]; then echo "STORE_LICENCE = $STORE_LICENCE empty. Exit Script." exit 1 fi + if [ "$STORE_DEVICEDATA" == "" ]; then echo "STORE_DEVICEDATA = $STORE_DEVICEDATA empty. Exit Script." exit 1 fi + + if [ "$STORE_CONTAINER" == "" ]; then + echo "STORE_DEVICEDATA = $STORE_CONTAINER empty. Exit Script." + exit 1 + fi break ;; "Exit") @@ -143,9 +161,11 @@ then echo "STORE = $STORE" case "$STORE" in - "Upperdir") + "Upperdir") + echo "Storing Upperdir" tar -rpf ${StoreBackupAt}/backup-${DATE}.tar -C ${DATAPATH} upperdir && echo "Done" ;; + "Changes") echo -e "Store specified folders only:" (set -o posix ; set) | grep "STORE" | grep "true" @@ -252,6 +272,26 @@ if $STORE_LICENCE; then fi fi +if $STORE_CONTAINER; then + echo "Storing Containers" + ## Get a list of all containers + CONTAINERS=($(podman container ls -aq)) + mkdir ${DATAPATH}Containers + # Export Containers and add them to the backup.tar + for CONTAINER in "${CONTAINERS[@]}" + do + echo "Storing Container:$CONTAINER" + echo "This just saves the current state of the Container as an Image!" + echo "You still need to Setup the Container again when restoring this backup" + echo "podman run -v XX -p xxx ... " + ## Need to stop continers? + $CONTAINER_ENGINE export $CONTAINER --output "${DATAPATH}/Containers/$CONTAINER.tar" + tar -rpf ${StoreBackupAt}/backup-${DATE}.tar -C ${DATAPATH} "Containers/$CONTAINER.tar" + rm "${DATAPATH}/Containers/$CONTAINER.tar" + done && echo "Done" + ;; +fi + if [ -f "${StoreBackupAt}/backup-${DATE}.tar" ]; then chown admin:plcnext ${StoreBackupAt}/backup-*.tar echo "Contents of Backup:" diff --git a/Backup/backupSettings.sh b/Backup/backupSettings.sh index ff8fda6..e56599b 100644 --- a/Backup/backupSettings.sh +++ b/Backup/backupSettings.sh @@ -50,4 +50,7 @@ STORE_ALL_CONFIG=false # Whole config directory CONFIG="upperdir/opt/plcnext/config" STORE_ALL_DATA=false # this directory holds mostly session dependend data like PIDs, FW update Status etc. -DATA="upperdir/opt/plcnext/data" \ No newline at end of file +DATA="upperdir/opt/plcnext/data" + +STORE_CONTAINER=false +CONTAINER_ENGINE=podman \ No newline at end of file diff --git a/Backup/restoreBackup.sh b/Backup/restoreBackup.sh index 46945dc..87d6407 100644 --- a/Backup/restoreBackup.sh +++ b/Backup/restoreBackup.sh @@ -9,6 +9,10 @@ CURRENT=$(pwd) TargetDirectory="$1" BackupLocation="$2" +SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" +## Set the directories for the backup +. ${SCRIPT_DIR}/../Backup/backupSettings.sh + if [ "$1" == "" ]; then echo "please add backup location as argument" echo "Example: @@ -26,7 +30,12 @@ tar -tvf "${BackupLocation}" echo 'Choose all the directories you wish to restore.' PS3="Select your choice: " -options=("SD Licence" "DeviceData" "PCWE" "Upperdir" "Exit") +options=("SD Licence" \ +"DeviceData" \ +"PCWE" \ +"Upperdir" \ +"Containers" \ +"Exit") select opt in "${options[@]}" do case $opt in @@ -82,6 +91,21 @@ do tar --same-owner -xvf "${BackupLocation}" -C "$TargetDirectory" upperdir && echo "Done restoring Upperdir" ;; + "Containers") + echo "Restoring Containers" + echo "This just restores the current state of the saved Container as an Image!" + echo "You still need to Setup the Container again after restoring this backup" + echo "podman run -v XX -p xxx ... " + tar --same-owner -xvf "${BackupLocation}" -C "$TargetDirectory" Containers && echo "Done restoring Container.tar" + CONTAINERS=($(ls $TargetDirectory/Containers)) + for CONTAINER in "${CONTAINERS[@]}" + do + echo "Importing $CONTAINER" + $CONTAINER_ENGINE import $TargetDirectory/Containers/$CONTAINER + rm $TargetDirectory/Containers/$CONTAINER + done && echo "Done" + ;; + "Exit") exit 1 break