repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

nushell/modules/btrfs.nu

Raw
# Btrfs Maintenance Script
# This script checks if your system is Btrfs and attempts to fix "Full" states
# by running a balance on chunks > 75% full.
export def maintenance [
	mount_point: path = '/' # mount point to check, `/` by default
] {
	print "๐Ÿ” Checking filesystem type..."
	let fs_type = df | where Mounted == $mount_point | get Filesystem.0

	if $fs_type != "btrfs" {
			print $"โŒ Error: Not a Btrfs filesystem ($fs_type). Aborting."
			exit 1
	}

	print "โœ… Filesystem is Btrfs."

	print "\n๐Ÿ“Š Current Btrfs Usage:"
	^btrfs filesystem usage $mount_point

	print "\n๐Ÿ›   Starting Btrfs Balance..."

	try {
			^sudo btrfs balance start -dusage=75 -musage=75 $mount_point
			print "โœ… Balance command sent successfully."
	} catch { |err |
			print $"โŒ Failed to start balance: $err"
			print "Ensure you have sudo privileges."
			exit 1
	}

	print "\nโณ Waiting for balance to complete or show status..."
	^sudo btrfs status $mount_point

	if ($in | str contains "Balance") {
			print "\nโš   Balance is still running. You can check progress with:"
			print $"  btrfs balance status $MOUNT_POINT"
	}

	print "\n๐Ÿ“ˆ Checking storage after balance:"
	^btrfs filesystem usage $mount_point

	print "\nโœ… Maintenance Complete."
}