From 182d0f2fdd535324a8dc71443344c49fcca9f67e Mon Sep 17 00:00:00 2001 From: Fl1tzi Date: Wed, 12 Apr 2023 18:33:50 +0200 Subject: [PATCH] initial commit --- backup.sh | 107 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100755 backup.sh diff --git a/backup.sh b/backup.sh new file mode 100755 index 0000000..de1198d --- /dev/null +++ b/backup.sh @@ -0,0 +1,107 @@ +#!/bin/bash + +# A Backup script designed for podman containers backed by systemd +# +# Templates: https://git.fl1tzi.com/server/systemd-infrastructure +# +# NOTE: This script will by only backup units that are already running. + +# ------------------------ +# CONFIGURATION +# ------------------------ + +declare -A UnitMap +# Declare which units uses which folder. +# eg. UnitMap["my_unit"]="my_folder" + +# This is the directory where all folder are stored, with your compose.yaml files inside them and the data. +# DO NOT FORGET THE SLASH AT THE END AND DO NOT USE WHITESPACES! +RootDir=$HOME/compose/ + +# The destination in Rclone +# eg. "cloud:example-folder" -> "google-drive:example-folder/FOLDER" +# eg. "cloud:" -> "cloud:/FOLDER" +# eg. "cloud:." -> "cloud:./FOLDER" +Dest="cloud:." + +# The method that will be used by Rclone +# eg. "clone" +# eg. "clone --max-age 2d" +# eg. "sync --progress" +Method="sync" + +# ------------------------ +# SCRIPT +# ------------------------ + +if [ $? -ne 0 ] +then + echo "Error in configuration." + exit 1 +fi + +printf "Starting backup...\n" + +for unit in "${!UnitMap[@]}" +do + echo "---[ $unit ]---" + + folder="$RootDir${UnitMap[$unit]}" + folder_name="${UnitMap[$unit]}" + + # print out folder + printf "folder:\n\t$folder\n" + + # check if dir exists + if [ ! -d $folder ] + then + echo "The folder does not exist." + echo "SKIPPING UNIT" + continue + fi + + systemctl --user status $unit >/dev/null + + # get error code from the status + exit_code=$? + + # 0 = active start backup + # 1 = failed skip + # 2 = unused skip + # 3 = not active skip + # 4 = no such unit skip + if [ $exit_code -eq 0 ] + then + printf "unit status:\n\t$exit_code = Unit is running\n" + else + printf "unit status:\n\t$exit_code\n" + if [ $exit_code -eq 1 ] + then + printf "\tThe unit failed\n" + elif [ $exit_code -eq 2 ] + then + printf "\tThe unit is unused\n" + elif [ $exit_code -eq 3 ] + then + printf "\tThe unit is not active\n" + elif [ $exit_code -eq 4 ] + then + printf "\tThe unit does not exist\n" + fi + echo "SKIPPING UNIT" + continue + fi + + echo "Stopping unit..." + systemctl --user stop $unit >/dev/null + + echo "Starting backup..." + /usr/bin/rclone $Method $folder $Dest/$folder_name + + echo "Starting unit..." + systemctl --user start $unit >/dev/null +done + +echo "FINISHED" + +