#!/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/podman unshare /usr/bin/rclone $Method $folder $Dest/$folder_name echo "Starting unit..." systemctl --user start $unit >/dev/null done echo "FINISHED"