181 lines
4.5 KiB
Bash
181 lines
4.5 KiB
Bash
#!/bin/bash
|
|
|
|
|
|
_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_file="$3"
|
|
echo "Backing up volume ${volume_name}"
|
|
docker run --rm -v ${volume_name}:/volume -v ${temp_path}:/backup alpine tar -czvf /backup/volume.tar.gz -C /volume .
|
|
;;
|
|
restore)
|
|
local backup_file="$3"
|
|
echo "Restoring volume ${volume_name}"
|
|
docker volume rm ${volume_name}
|
|
docker volume create ${volume_name}
|
|
docker run --rm -v ${volume_name}:/volume -v ${temp_path}:/backup alpine tar -xzvf /backup/volume.tar.gz -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_file="$3"
|
|
echo "Backing up path ${path}"
|
|
tar -czvf ${backup_file} -C ${path} .
|
|
;;
|
|
restore)
|
|
local backup_file="$3"
|
|
echo "Restoring path ${path}"
|
|
tar -xzvf ${backup_file} -C ${path} --strip-components=1
|
|
;;
|
|
esac
|
|
}
|
|
|
|
_autocommandrun_file() {
|
|
command="$1"
|
|
value="$2"
|
|
|
|
case "$command" in
|
|
create)
|
|
;;
|
|
nuke)
|
|
rm -f ${value}
|
|
;;
|
|
backup)
|
|
local backup_file="$3"
|
|
echo "Backing up file ${value}"
|
|
cp ${value} ${backup_file}
|
|
;;
|
|
restore)
|
|
local backup_file="$3"
|
|
echo "Restoring file ${value}"
|
|
cp ${backup_file} ${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="$2"
|
|
shift
|
|
|
|
# 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#*=}"
|
|
|
|
local bfile="${temp_path}/${key}_${value}.tgz"
|
|
|
|
# Key must be one of volume, path or file
|
|
case "$key" in
|
|
volume)
|
|
_autocommandrun_volume "$command" "$value" "$bfile"
|
|
;;
|
|
path)
|
|
_autocommandrun_path "$command" "$value" "$bfile"
|
|
;;
|
|
file)
|
|
_autocommandrun_file "$command" "$value" "$bfile"
|
|
;;
|
|
*)
|
|
_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 does not exist"
|
|
[ -d "$temp_path" ] || _die "Temp path $temp_path does not exist"
|
|
|
|
local backup_temp_path="$temp_path/backup"
|
|
|
|
mkdir -p "$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" "$@"
|
|
}
|
|
|
|
|