shell-base/modules/utility/sync.lib

91 lines
3.0 KiB
Plaintext
Raw Normal View History

#!/bin/bash
mirror_rootfs () {
2024-02-26 12:30:19 -08:00
local boot; local dest; local rootfs; local opts
[[ ! $(command -v rsync) ]] && { echo rsync must be installed to use this function; return 5; }
2024-02-26 12:30:19 -08:00
opts=" --dry-run"
[[ $1 == "--run" ]] && opts="" && shift
[[ $1 == "-v" ]] && opts+=" -v" && shift
[[ $1 == "-o" ]] && opts+=" $2" && shift
dest=$(realpath -s $1)
2024-02-26 12:30:19 -08:00
rootfs=$2
[ ! -d "$dest" ] && echo destination $dest is not a directory && return 3
[ ! -d "$rootfs/etc" ] && echo not root filesystem at $rootfs && return 3
2024-02-26 12:30:19 -08:00
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
2024-02-26 12:30:19 -08:00
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
}
2024-02-26 12:30:19 -08:00
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
[[ $1 == "-l" ]] && list=true && shift
[[ $1 == "-r" ]] && delete=true && shift
[[ "$1" == "-"* ]] && >&2 echo bad option, aborting && return 1
rfse=${1:-/tmp/rootfs_excludes.lst}
2024-02-26 12:30:19 -08:00
touch $rfse
[[ $delete ]] && rm $rfse
cat <<EOF > $rfse
.[Tt]rash*
tmp/
cache/
log/
log[s]/
lost+found
EOF
if [[ $list ]];then
cat $rfse; rm -f $rfse
else
echo $rfse
fi
}
mountpoints () {
cat /proc/mounts | cut -d ' ' -f2 | sort
}