diff --git a/modules/utility/sync.lib b/modules/utility/sync.lib index b3f13ad..4769b11 100644 --- a/modules/utility/sync.lib +++ b/modules/utility/sync.lib @@ -1,29 +1,67 @@ #!/bin/bash mirror_rootfs () { + local boot; local dest; local rootfs; local opts [[ ! $(command -v rsync) ]] && { echo rsync must be installed to use this function; return 5; } - dr="--dry-run" + opts=" --dry-run" + [[ $1 == "--run" ]] && opts="" && shift + [[ $1 == "-v" ]] && opts+=" -v" && shift + [[ $1 == "-o" ]] && opts+=" $2" && shift dest=$(realpath -s $1) - rootfs=$(realpath -s "${2:-/}") + rootfs=$2 [ ! -d "$dest" ] && echo destination $dest is not a directory && return 3 [ ! -d "$rootfs/etc" ] && echo not root filesystem at $rootfs && return 3 - echo making mirror of root filesystem to - rfse=rootfs_excludes - if sudo rsync -axHAWXS $dr --numeric-ids --delete --delete-excluded --force --exclude-from="$(rootfs_excludles)" / /mnt/adrive/nas/@rootfs; then - echo making mirror of boot partition to /mnt/adrive/nas/@rootfs - if sudo rsync -axHAWXS $dr --numeric-ids --force --delete /boot/ /mnt/adrive/nas/@boot; then - echo doit + echo making mirror of root filesystem at ${rootfs:-/} to $dest/rootfs + #TODO if destination is btrfs then make snapshot otherwise make directory + sudo btrfs subvol create $dest/rootfs >/dev/null + if sudo rsync -axHAWXS $opts --info=progress2 --numeric-ids --delete --delete-excluded --force --exclude-from="$(rootfs_excludes)" ${rootfs:-/} $dest/rootfs ; then + echo root parition mirror was made to $dest/rootfs + boot=$rootfs/boot + [[ ! $(mountpoint $boot) ]] && boot=$rootfs/boot/efi + [[ ! $(mountpoint $boot) ]] && echo neither $rootfs/boot nor $boot are mountpoints, no boot parition to mirror && return 2 + echo making mirror of boot $boot partition to $dest$boot + sudo btrfs subvol create $dest$boot >/dev/null + if sudo rsync -aHAWXS $opts --info=progress2 --numeric-ids --force --delete $boot/ $dest$boot; then + echo boot partion is mirrored else - echo unable to sync a copy of /boot, aborted backup + echo unable to sync a copy of $boot, aborted backup fi else echo unable to sync a copy of root file system, backkup failed fi } + rclone () { + local boot; local dest; local src; local opts + [[ ! $(command -v rsync) ]] && { echo rsync must be installed to use this function; return 5; } + opts=" --dry-run" + [[ $1 == "--run" ]] && opts="" && shift + [[ $1 == "-v" ]] && opts+=" -v" && shift + [[ $1 == "-o" ]] && opts+=" $2" && shift + + src=$(realpath $1) + dest=$(realpath $2) + mkdir -p $dest >/dev/null + echo making clone/mirror of $src to $dest + + if [ "$(ls -A $dest)" ]; then + echo WARNING: $dest is not empty but will be erased!, be sure before using --run + # TODO RUN CONFIRM + # if [[ $opt == *"--run"* ]]; then + fi + # if sudo rsync -axHAWXS $opts --info=progress2 --numeric-ids --delete --delete-excluded --force $src/ $dest/ ; then + cmd="sudo rsync -axHAWXS $opts --info=progress2 --numeric-ids --delete --force $src/ $dest/" + echo "$cmd" + if $cmd ; then + echo mirror/clone was successful + else + echo unable to make the clone + fi +} + rootfs_excludes () { local list; local delete @@ -31,6 +69,7 @@ local list; local delete [[ $1 == "-r" ]] && delete=true && shift [[ "$1" == "-"* ]] && >&2 echo bad option, aborting && return 1 rfse=${1:-/tmp/rootfs_excludes.lst} +touch $rfse [[ $delete ]] && rm $rfse cat < $rfse .[Tt]rash*