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

122 lines
3.1 KiB
Bash

#!/bin/bash
make_subvol() {
local usesudo
local uid
local gid
[[ $EUID -ne 0 ]] && usesudo=sudo
uid=${2:-$USER}
gid=${3:-$uid}
echo $usesudo btrfs subvolume create $1
$usesudo btrfs subvolume create $1
echo $usesudo chown $uid:$gid $1
$usesudo chown $uid:$gid $1
}
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
alias btvl="sudo btrfs subvolume list"
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 send $src | $usesudo btrfs -q receive $dest
# cd $2 || return
$usesudo btrfs subvolume snapshot $dest/$src $dest/$name
# todo check for snapshot then delete transfered one
}
# ALL ALL = (root) NOPASSWD:/bin/btrfs
subvolume_size () {
local all;
[[ $1 == "-a" ]] && all=true
if [[ $all ]]; then
sudo btrfs qgroup show "${2:-$PWD}" --kbytes | tail -n +3
str=$(sudo btrfs qgroup show "${2:-$PWD}" --kbytes | tail -n +3);
else
sudo btrfs qgroup show "${2:-$PWD}" --kbytes | grep $1
str=$(sudo btrfs 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 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 sub snap $1/$sv $2
fi
done
}
# 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 {} + \) \)
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
}