Last active
August 22, 2024 07:05
-
-
Save arrogantrabbit/18d32430a8250ff6d77a470b976928e0 to your computer and use it in GitHub Desktop.
Script to rebalance the large zfs dataset minimizing the downtime
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/zsh | |
set -e | |
## -- configuration -- ## | |
# Specify the dataset name that needs to be rebalanced and provide stop_services and | |
# start_services functions | |
dataset="pool1/storagenode-one" | |
jail="storj" | |
stop_services() | |
{ | |
iocage stop "$jail" | |
} | |
start_services() | |
{ | |
iocage start "$jail" | |
} | |
## -- implementation -- ## | |
# total umber of passes | |
np=6 | |
# Name of the temporary dataset | |
target="${dataset}-temp" | |
# Name(s) of the temporary snapshots(s) | |
snapshot="tmpcloning" | |
## -- helpers -- ## | |
send_snapshot() | |
{ | |
if [ "1" -eq "$1" ]; then | |
echo "Copying initial snapshot $1" | |
zfs snapshot -r "${dataset}@$snapshot$1" | |
zfs send -Rv "${dataset}@$snapshot$1" | zfs receive "$target" | |
else | |
echo "Copying incremental snapsgot $1 since $(($1-1))" | |
zfs snapshot -r "${dataset}@$snapshot$1" | |
zfs send -Rvi "${dataset}@$snapshot$(($1-1))" "${dataset}@$snapshot$1" | zfs receive "$target" | |
fi | |
} | |
rename_datasets() | |
{ | |
echo "Renaming datasets" | |
zfs rename ${dataset} ${dataset}-old | |
zfs rename $target ${dataset} | |
} | |
destroy_tmp_data() | |
{ | |
for i in $(seq $np); do | |
zfs destroy -r "${dataset}-old@$snapshot$i" | |
zfs destroy -r "${dataset}@$snapshot$i" | |
done | |
zfs destroy -r "${dataset}-old" | |
} | |
## -- action -- ## | |
for i in $(seq $(("$np"-1))); do | |
send_snapshot "$i" | |
done | |
stop_services | |
send_snapshot $np | |
rename_datasets | |
start_services | |
destroy_tmp_data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment