shell-base/modules/filesystem/btrfs/btrfs.mod

175 lines
4.7 KiB
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() {
local usesudo
local uid
local gid
[[ $EUID -ne 0 ]] && usesudo=sudo
uid=${2:-$USER}
gid=${3:-$uid}
echo $usesudo $BTRFS_BIN subvolume create $1
$usesudo $BTRFS_BIN subvolume create $1
echo $usesudo chown $uid:$gid $1
$usesudo chown $uid:$gid $1
}
del_subvol() {
local usesudo; local delete
[[ $1 == "-d" ]] && delete=true && shift
[[ $EUID -ne 0 ]] && usesudo=sudo
if $usesudo $BTRFS_BIN subvolume show "$1" &> /dev/null; then
if [[ -v PS1 && ! $delete ]]; then
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
snapshot_restore () {
local src=$1
local dest=$2
local name=${3:-$(echo $src| rev | cut -f 2- -d '.' | rev)}
local usesudo
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
[[ $EUID -ne 0 ]] && usesudo=sudo
$usesudo $BTRFS_BIN send $src | $usesudo $BTRFS_BIN -q receive $dest
# cd $2 || return
$usesudo $BTRFS_BIN subvolume snapshot $dest/$src $dest/$name
# todo check for snapshot then delete transfered one
}
subvolume_size () {
local all;
[[ $1 == "-a" ]] && all=true
if [[ $all ]]; then
sudo $BTRFS_BIN qgroup show "${2:-$PWD}" --kbytes | tail -n +3
str=$(sudo $BTRFS_BIN qgroup show "${2:-$PWD}" --kbytes | tail -n +3);
else
sudo $BTRFS_BIN qgroup show "${2:-$PWD}" --kbytes | grep $1
str=$(sudo $BTRFS_BIN qgroup show "${2:-$PWD}" --kbytes | grep $1)
fi
# echo $str
subvolumeFolderSize=0;
while read line; do
FIELDS=( $line )
thisLineKb="${FIELDS[2]/'.00KiB'/''}";
# echo $thisLineKb
subvolumeFolderSize=$((subvolumeFolderSize+thisLineKb));
done <<< "$str"
[[ $all ]] && echo "size of entire filesystem at ${2:-$PWD}" || echo size of subvolumes/snapshots $1 at "${2:-$PWD}"
echo $subvolumeFolderSize KB
echo "~ $((subvolumeFolderSize/1024)) MB"
echo "~ $((subvolumeFolderSize/1024/1024)) GB"
}
find_subvolumes() {
dir=${1:-.}
dep=${2:-1}
sudo /bin/find "$dir" -maxdepth $dep -type d -exec bash -c '
for d do
subv=$(basename $d)
sudo $BTRFS_BIN subvolume show "$d" >/dev/null 2>&1 && printf "%s\n" "$subv"
done' find-sh {} +
}
folder_snapshot() {
for sv in $(find_subvolumes $1 1); do
if [[ -d $2/$sv ]]; then
echo snapshot $sv already exists in $2. You must manually delete target snapshots!
else
sudo $BTRFS_BIN sub snap $1/$sv $2
fi
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"
# sudo find / -type d -exec sh -c '
# for d do
# btrfs subvolume show "$d" >/dev/null 2>&1 && printf "%s\n" "$d"
# done' find-sh {} +
# You may want to exclude paths that are beyond suspicion. The following code excludes /proc, /sys and /dev:
# sudo find / -type d \( \
# \( -path /proc -prune \) -o \
# \( -path /sys -prune \) -o \
# \( -path /dev -prune \) -o \
# \( -exec sh -c '
# for d do
# btrfs subvolume show "$d" >/dev/null 2>&1 && printf "%s\n" "$d"
# done
# ' find-sh {} + \) \)