added del_subvol and named_snapshot

master
David Kebler 2024-03-02 10:20:19 -08:00
parent 61e61ac249
commit 195c34e80a
1 changed files with 83 additions and 31 deletions

View File

@ -1,4 +1,11 @@
#!/bin/bash #!/bin/bash
export BTRFS_BIN=$(which btrfs)
module_load confirm
module_load helpers
# set this for sudo
# ALL ALL = (root) NOPASSWD:/bin/btrfs
make_subvol() { make_subvol() {
local usesudo local usesudo
local uid local uid
@ -6,26 +13,38 @@ make_subvol() {
[[ $EUID -ne 0 ]] && usesudo=sudo [[ $EUID -ne 0 ]] && usesudo=sudo
uid=${2:-$USER} uid=${2:-$USER}
gid=${3:-$uid} gid=${3:-$uid}
echo $usesudo btrfs subvolume create $1 echo $usesudo $BTRFS_BIN subvolume create $1
$usesudo btrfs subvolume create $1 $usesudo $BTRFS_BIN subvolume create $1
echo $usesudo chown $uid:$gid $1 echo $usesudo chown $uid:$gid $1
$usesudo chown $uid:$gid $1 $usesudo chown $uid:$gid $1
} }
make_base_subvols() { del_subvol() {
svols="shell admin opt data docker images temp" local usesudo; local delete
BTRFSDATAROOT=${BTRFSDATAROOT:-/mnt/data} [[ $1 == "-d" ]] && delete=true && shift
# cd $BTRFSDATAROOT || exit [[ $EUID -ne 0 ]] && usesudo=sudo
for svol in $svols; do if $usesudo $BTRFS_BIN subvolume show "$1" &> /dev/null; then
make_subvol $BTRFSDATAROOT/$svol $@ if [[ -v PS1 && ! $delete ]]; then
done confirm "DELETE the subvolume $(realpath "$1") (pass -d to avoid confirm)" || return 1
fi
$usesudo $BTRFS_BIN subvolume delete $1
else
echo "$1 not a btrfs subvolume, nothing to delete"
fi
} }
# make_base_subvols() {
# svols="shell admin opt data docker images temp"
# BTRFSDATAROOT=${BTRFSDATAROOT:-/mnt/data}
# # cd $BTRFSDATAROOT || exit
# for svol in $svols; do
# make_subvol $BTRFSDATAROOT/$svol $@
# done
# }
#ssudo mount -o subvol=opt /dev/sda4 /test #ssudo mount -o subvol=opt /dev/sda4 /test
alias btvl="sudo btrfs subvolume list"
snapshot_restore () { snapshot_restore () {
local src=$1 local src=$1
local dest=$2 local dest=$2
@ -34,23 +53,23 @@ snapshot_restore () {
echo copying $src at $PWD to $2 then renaming to $name echo copying $src at $PWD to $2 then renaming to $name
# TODO check for snapshot, strip volume from source path, remove extra snapshot when done # TODO check for snapshot, strip volume from source path, remove extra snapshot when done
[[ $EUID -ne 0 ]] && usesudo=sudo [[ $EUID -ne 0 ]] && usesudo=sudo
$usesudo btrfs send $src | $usesudo btrfs -q receive $dest $usesudo $BTRFS_BIN send $src | $usesudo $BTRFS_BIN -q receive $dest
# cd $2 || return # cd $2 || return
$usesudo btrfs subvolume snapshot $dest/$src $dest/$name $usesudo $BTRFS_BIN subvolume snapshot $dest/$src $dest/$name
# todo check for snapshot then delete transfered one # todo check for snapshot then delete transfered one
} }
# ALL ALL = (root) NOPASSWD:/bin/btrfs
subvolume_size () { subvolume_size () {
local all; local all;
[[ $1 == "-a" ]] && all=true [[ $1 == "-a" ]] && all=true
if [[ $all ]]; then if [[ $all ]]; then
sudo btrfs qgroup show "${2:-$PWD}" --kbytes | tail -n +3 sudo $BTRFS_BIN qgroup show "${2:-$PWD}" --kbytes | tail -n +3
str=$(sudo btrfs qgroup show "${2:-$PWD}" --kbytes | tail -n +3); str=$(sudo $BTRFS_BIN qgroup show "${2:-$PWD}" --kbytes | tail -n +3);
else else
sudo btrfs qgroup show "${2:-$PWD}" --kbytes | grep $1 sudo $BTRFS_BIN qgroup show "${2:-$PWD}" --kbytes | grep $1
str=$(sudo btrfs qgroup show "${2:-$PWD}" --kbytes | grep $1) str=$(sudo $BTRFS_BIN qgroup show "${2:-$PWD}" --kbytes | grep $1)
fi fi
# echo $str # echo $str
@ -76,7 +95,7 @@ find_subvolumes() {
sudo /bin/find "$dir" -maxdepth $dep -type d -exec bash -c ' sudo /bin/find "$dir" -maxdepth $dep -type d -exec bash -c '
for d do for d do
subv=$(basename $d) subv=$(basename $d)
sudo btrfs subvolume show "$d" >/dev/null 2>&1 && printf "%s\n" "$subv" sudo $BTRFS_BIN subvolume show "$d" >/dev/null 2>&1 && printf "%s\n" "$subv"
done' find-sh {} + done' find-sh {} +
} }
@ -86,12 +105,55 @@ folder_snapshot() {
if [[ -d $2/$sv ]]; then if [[ -d $2/$sv ]]; then
echo snapshot $sv already exists in $2. You must manually delete target snapshots! echo snapshot $sv already exists in $2. You must manually delete target snapshots!
else else
sudo btrfs sub snap $1/$sv $2 sudo $BTRFS_BIN sub snap $1/$sv $2
fi fi
done done
} }
mount_subvolume () {
echo sudo mount $1 -o subvol=$2 $3
if mountpoint $3; then
echo $3 already a mountpoint, aborting subvolume mount
else
mkdir -p $3
sudo mount $1 -o subvol=$2 $3
fi
}
named_snapshot () {
local usage; local subvol; local target; local run; local usesudo
[[ $1 == "-r" ]] && run=true && shift
[[ $EUID -ne 0 ]] && usesudo=sudo
subvol=$(realpath "$1")
usage="usage: named_snapshot <-r> <subvolume/snapshot> <new extension name> <optional target directory>"
if sudo $BTRFS_BIN subvolume show "$1" &> /dev/null; then
# if [[ -v PS1 && ! $delete ]]; then
[[ ! $2 ]] && echo no extension name for named snapshot && echo $usage && return 1
if [[ $3 ]]; then
[[ ! -d $3 ]] && echo "target directory $3 does not exist, can not make snapshot" && echo $usage && return 2
target=$(realpath "$3")/$(basename "$(rm_ext "$subvol")").$2
else
target="$(rm_ext "$subvol").$2"
fi
[[ -v PS1 && ! $run ]] && confirm "make snapshot of $subvol to $target (pass -r to avoid confirm )" || return 4
$usesudo "$BTRFS_BIN" sub snap "$subvol" "$target"
else
echo "$subvol not a btrfs subvolume, can't make named snapshot"
echo $usage
return 3
fi
}
# alias
alias btvl="sudo $BTRFS_BIN subvolume list"
alias btrfs="sudo $BTRFS_BIN"
alias btsub="sudo $BTRFS_BIN subvolume"
@ -110,13 +172,3 @@ folder_snapshot() {
# btrfs subvolume show "$d" >/dev/null 2>&1 && printf "%s\n" "$d" # btrfs subvolume show "$d" >/dev/null 2>&1 && printf "%s\n" "$d"
# done # done
# ' find-sh {} + \) \) # ' find-sh {} + \) \)
mount_subvolume () {
echo sudo mount $1 -o subvol=$2 $3
if mountpoint $3; then
echo $3 already a mountpoint, aborting subvolume mount
else
mkdir -p $3
sudo mount $1 -o subvol=$2 $3
fi
}