#!/bin/bash
-errlog()
-{
- local lastexit=$?
- local msg="$1"
-
- echo "â ERROR: $msg"
- echo "đ EXIT $lastexit"
- return $lastexit
-}
-
-infolog()
-{
- local lastexit=$?
- local msg="$1"
-
- echo "âšī¸ INFO: $msg"
- return $lastexit
-}
-
-partition_mount()
-{
- local media="$1"
- local partition=
- local backup_path=
- partition=$(yq --raw-output ".media.\"$media\".partition" config.yaml)
- [ "$partition" != "null" ] \
- || errlog "config.yaml partition for $media missing" \
- || return $?
- [ -b "$partition" ] \
- || errlog "partition $partition not found" \
- || return $?
- backup_path=$(yq --raw-output ".backup_path" config.yaml)
- [ "$backup_path" != "null" ] \
- || errlog "config.yaml backup path missing" \
- || return $?
-
- infolog "create backup path $backup_path"
- mkdir --parents "$backup_path" \
- || errlog "could not create directory $backup_path" \
- || return $?
- infolog "mount partition $partition to $backup_path"
- mount "$partition" "$backup_path" \
- || errlog "cloud not mount $partition to $backup_path" \
- || return $?
-}
-
-device_mount()
-{
- local media="$1"
- local device="$2"
- local key=
- local volume=
-
- [ -b "$device" ] \
- || infolog "block device $device for $media not found" \
- || return $?
- infolog "media $media with device $device found"
- key=$(yq --raw-output ".media.\"$media\".key" config.yaml)
- [ "$key" != "null" ] \
- || errlog "config.yaml key for $media missing" \
- || return $?
- [ -f "$key" ] \
- || errlog "key $key not exists" \
- || return $?
- infolog "use key $key"
- volume=$(yq --raw-output ".backup_volume" config.yaml)
- [ "$volume" != "null" ] \
- || errlog "config.yaml volume for $media missing" \
- || return $?
-
- infolog "volume open $volume"
- cryptsetup open --key-file "$key" \
- "$device" \
- "$volume" \
- || errlog "could not open $device with $key" \
- || return $?
-
- partition_mount "$media" || return $?
-}
-
-mount_backup()
-{
- local medias=
- medias=$(yq --raw-output '.media | keys[]' config.yaml)
-
- [ -n "$medias" ] && [ "$medias" != "null" ] \
- || errlog "config.yaml missing media section" \
- || return $?
-
- local media=
- local device=
- for media in $medias; do
- device=$(yq --raw-output ".media.\"$media\".device" config.yaml)
- [ "$device" != "null" ] \
- || errlog "missing path for $media" \
- || return $?
- device_mount "$media" "$device" && return 0
- done
-
- errlog "no device was mounted"
-}
-
-umount_backup()
-{
- local backup_path=
- local volume=
-
- backup_path=$(yq --raw-output ".backup_path" config.yaml)
- [ "$backup_path" != "null" ] \
- || errlog "config.yaml backup path missing" \
- || return $?
-
- infolog "umount backup path $backup_path"
- umount "$backup_path" \
- || errlog "could not unmount $backup_path" \
- || return $?
-
- infolog "remove backup path directory $backup_path"
- rmdir "$backup_path" \
- || errlog "clound not remote $backup_path" \
- || return $?
-
- volume=$(yq --raw-output ".backup_volume" config.yaml)
- [ "$volume" != "null" ] \
- || errlog "config.yaml volume for $media missing" \
- || return $?
-
- infolog "close volume $volume"
- cryptsetup close "$volume" \
- || errlog "could not close $volume" \
- || return $?
-}
-
main()
{
- local cmd="$1"
-
echo "â START: backupmount"
-
- command -v yq > /dev/null \
- || errlog "command yq not found" \
- || return $?
- command -v cryptsetup > /dev/null \
- || errlog "command cryptsetup not found" \
- || return $?
-
- [ -n "$cmd" ] \
- || errlog "missing command" \
- || return $?
- [ -f "config.yaml" ] \
- || errlog "file config.yaml not exists" \
- || return $?
-
- if [ "$cmd" == "mount" ]; then
- mount_backup || return $?
- elif [ "$cmd" == "umount" ]; then
- umount_backup || return $?
- else
- errlog "unknown command" || return $?
- fi
-
- echo "â
SUCCESS: backupmount"
}
-main "$*"
+[ "$TESTMODE" == "ON" ] && return 0
+main "$@"