Skip to content

Commit

Permalink
Adding Option to store Containers as images.
Browse files Browse the repository at this point in the history
  • Loading branch information
OWarneke committed Jan 31, 2022
1 parent b460bf1 commit c4383dd
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 10 deletions.
56 changes: 48 additions & 8 deletions Backup/backupSD.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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[@]}"
Expand All @@ -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"
Expand All @@ -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
Expand All @@ -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")
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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:"
Expand Down
5 changes: 4 additions & 1 deletion Backup/backupSettings.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
DATA="upperdir/opt/plcnext/data"

STORE_CONTAINER=false
CONTAINER_ENGINE=podman
26 changes: 25 additions & 1 deletion Backup/restoreBackup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c4383dd

Please sign in to comment.