Your Name 1776a7e45f .
2025-05-05 21:43:07 +12:00

198 lines
5.3 KiB
Bash

#!/bin/bash
MYID=$(id -u)
MYGRP=$(id -g)
_autocommandrun_volume() {
command="$1"
volume_name="$2"
case "$command" in
create)
echo "Creating volume ${volume_name}"
docker volume create ${volume_name}
;;
nuke)
echo "Nuking volume ${volume_name}"
docker volume rm ${volume_name}
;;
backup)
local backup_folder="$3"
echo "Backing up volume ${volume_name}"
docker run --rm -v ${volume_name}:/volume -v ${backup_folder}:/backup debian bash -c "tar -czvf /backup/backup.tgz -C /volume . && chown -R $MYID:$MYGRP /backup"
;;
restore)
local backup_folder="$3"
echo "Restoring volume ${volume_name}"
docker volume rm ${volume_name}
docker volume create ${volume_name}
docker run --rm -v ${volume_name}:/volume -v ${backup_folder}:/backup debian bash -c "tar -xzvf /backup/backup.tgz -C /volume --strip-components=1"
;;
esac
}
_autocommandrun_path() {
command="$1"
path="$2"
case "$command" in
create)
echo "Creating path ${path}"
mkdir -p ${path}
;;
nuke)
echo "Nuking path ${path}"
rm -rf ${path}
;;
backup)
local backup_folder="$3"
echo "Backing up path ${path}"
tar -czvf ${backup_folder}/backup.tgz -C ${path} .
;;
restore)
local backup_folder="$3"
echo "Restoring path ${path}"
tar -xzvf ${backup_folder}/backup.tgz -C ${path} --strip-components=1
;;
esac
}
_autocommandrun_file() {
command="$1"
value="$2"
case "$command" in
create)
;;
nuke)
rm -f ${value}
;;
backup)
local backup_folder="$3"
echo "Backing up file ${value}"
# get filename from path
local filename=$(basename ${value})
cp ${value} ${backup_folder}/${filename}
;;
restore)
local backup_folder="$3"
echo "Restoring file ${value}"
local filename=$(basename ${value})
cp ${backup_folder}/${filename} ${value}
;;
esac
}
_autocommandparse() {
# first argument is the command
# if the command is backup or restore, then the last two arguments are the backup file and the temporary path
# all other arguments are of form:
# key=value
# where key can be one of volume, path or file.
# value is the path or volume name.
# we iterate over the key=value arguments, and for each we call:
# autorun <command> <backupfile> <key> <value>
local command="$1"
shift
local temp_path="$1"
shift
echo "autocommandparse: command=$command temp_path=$temp_path"
# Extract the backup file and temp path (last two arguments)
local args=("$@")
local arg_count=${#args[@]}
# Process all key=value pairs
for ((i=0; i<$arg_count; i++)); do
local pair="${args[$i]}"
# Skip if not in key=value format
if [[ "$pair" != *"="* ]]; then
continue
fi
local key="${pair%%=*}"
local value="${pair#*=}"
# create backup folder unique to key/value.
local bfolder="${key}_${value}"
# remove any non-alphanumeric characters, that aren't dash or underscore from the bfile
targetpath=""
if [ ! "$temp_path" == "-" ]; then
bfolder=$(echo "$bfolder" | tr -cd '[:alnum:]_-')
mkdir -p ${temp_path}/${bfolder}
targetpath="${temp_path}/${bfolder}"
fi
# Key must be one of volume, path or file
case "$key" in
volume)
_autocommandrun_volume "$command" "$value" "$targetpath"
;;
path)
_autocommandrun_path "$command" "$value" "$targetpath"
;;
file)
_autocommandrun_file "$command" "$value" "$targetpath"
;;
*)
_die "Unknown key $key passed to auto${command}. We only support volume, path and file."
;;
esac
done
}
autocreate() {
_autocommandparse create "-" "$@"
}
autonuke() {
_autocommandparse nuke "-" "$@"
}
autobackup() {
local backup_file="$1"
shift
local temp_path="$1"
shift
[ -f "$backup_file" ] && _die "Backup file $backup_file already exists"
[ -d "$temp_path" ] || _die "Temp path $temp_path does not exist"
local backup_temp_path="$temp_path/backup"
mkdir -p "$backup_temp_path"
echo "_autocommandparse [backup] [$backup_temp_path] [$@]"
_autocommandparse backup "$backup_temp_path" "$@"
tar zcvf "$backup_file" -C "$backup_temp_path" .
}
autorestore() {
local backup_file="$1"
shift
local temp_path="$1"
shift
[ -f "$backup_file" ] || _die "Backup file $backup_file does not exist"
[ -d "$temp_path" ] || _die "Temp path $temp_path does not exist"
local restore_temp_path="$temp_path/restore"
mkdir -p "$restore_temp_path"
tar zxvf "$backup_file" -C "$restore_temp_path" --strip-components=1
_autocommandparse restore "$restore_temp_path" "$@"
}