2023-02-07 09:31:25 -08:00
|
|
|
#!/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"
|
|
|
|
|
2023-02-22 11:39:34 -08:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-02-07 09:31:25 -08:00
|
|
|
# ALL ALL = (root) NOPASSWD:/bin/btrfs
|
|
|
|
|
2023-02-22 11:39:34 -08:00
|
|
|
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"
|
|
|
|
|
2023-12-14 13:59:24 -08:00
|
|
|
}
|
|
|
|
|
2023-12-16 08:27:37 -08:00
|
|
|
find_subvolumes() {
|
|
|
|
dir=${1:-.}
|
|
|
|
dep=${2:-1}
|
|
|
|
sudo /bin/find "$dir" -maxdepth $dep -type d -exec bash -c '
|
2023-12-14 13:59:24 -08:00
|
|
|
for d do
|
2023-12-16 08:27:37 -08:00
|
|
|
subv=$(basename $d)
|
|
|
|
sudo btrfs subvolume show "$d" >/dev/null 2>&1 && printf "%s\n" "$subv"
|
2023-12-14 13:59:24 -08:00
|
|
|
done' find-sh {} +
|
|
|
|
}
|
|
|
|
|
2023-12-16 08:27:37 -08:00
|
|
|
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
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-12-14 13:59:24 -08:00
|
|
|
# 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
|
2023-12-15 10:23:44 -08:00
|
|
|
# ' find-sh {} + \) \)
|
|
|
|
|
|
|
|
mount_subvolume () {
|
2023-12-15 11:17:06 -08:00
|
|
|
echo sudo mount $1 -o subvol=$2 $3
|
|
|
|
if mountpoint $3; then
|
|
|
|
echo $3 already a mountpoint, aborting subvolume mount
|
|
|
|
else
|
2023-12-15 10:23:44 -08:00
|
|
|
mkdir -p $3
|
|
|
|
sudo mount $1 -o subvol=$2 $3
|
2023-12-15 11:17:06 -08:00
|
|
|
fi
|
2023-12-15 10:23:44 -08:00
|
|
|
}
|