diff --git a/btrfs-snapshots.sh b/btrfs-snapshots.sh index 0dff4a4..d029426 100755 --- a/btrfs-snapshots.sh +++ b/btrfs-snapshots.sh @@ -2,9 +2,15 @@ # defaults HOURLY_DEL=25 +HOURLY_FAST=1 DAILY_DEL=8 +DAILY_FAST=0 WEEKLY_DEL=5 +WEEKLY_FAST=1 MONTHLY_DEL= +MONTHLY_FAST=1 +FAST=0 +REBALANCE=0 BTRFS_VOLUMES=${BTRFS_VOLUMES:-$(awk '!/^#/ && $3=="btrfs" {print $2}' /etc/fstab | tr '\n' ' ' | sed 's, $,,')} TMP_MNT=${TMP_MNT:-/var/tmp/btrfs-backup} @@ -20,7 +26,8 @@ fi vols=() del=${DEL} dryrun=0 -fast=0 +fast=${FAST} +rebalance=${REBALANCE} periodity=${0%/*} periodity=${periodity##*/} if [[ $periodity =~ ^cron\... ]]; then @@ -30,10 +37,10 @@ else fi case "$periodity" in - (.hourly) del=${HOURLY_DEL}; fast=1;; - (.daily) del=${DAILY_DEL};; - (.weekly) del=${WEEKLY_DEL}; fast=1;; - (.monthly) del=${MONTHLY_DEL}; fast=1;; + (.hourly) del=${HOURLY_DEL}; fast=${HOURLY_FAST};; + (.daily) del=${DAILY_DEL}; fast=${DAILY_FAST};; + (.weekly) del=${WEEKLY_DEL}; fast=${WEEKLY_FAST};; + (.monthly) del=${MONTHLY_DEL}; fast=${MONTHLY_FAST};; esac while test $# -gt 0; do case "$1" in @@ -45,6 +52,7 @@ OPTIONS -h, --help show this help -n, --dry-run execute dry run, do not backup, just show commands -f, --fast fast run, do not optimize btrfs (otherwise done daily) + -r, --rebalance enable rebalancing (always disabled in fast mode) -p, --path add a path that contains a btrfs (sub-) volume (defaults to: ${BTRFS_VOLUMES}) -m, --mnt temporary mount point (default: ${TMP_MNT}) @@ -81,6 +89,7 @@ EOF exit;; (-n|--dry-run) dryrun=1;; (-f|--fast) fast=1;; + (-r|--rebalance) rebalance=1;; (-p|--path) shift; vols+=( "$1" );; (-m|--mnt) shift; TMP_MNT="$1";; (-d|--del) shift; del="$1";; @@ -135,7 +144,9 @@ for fs in ${BTRFS_VOLUMES}; do echo " " umount "$TMP_MNT" if test $fast -eq 0; then echo " " btrfs filesystem defragment ${fs} - echo " " btrfs balance start ${fs} + if test $rebalance -eq 1; then + echo " " btrfs balance start ${fs} + fi fi else sudo btrfs subvolume snapshot "${TMP_MNT}${subvol}" "${TMP_MNT}${target}${date}" @@ -149,8 +160,10 @@ for fs in ${BTRFS_VOLUMES}; do if test $fast -eq 0; then echo "Defragment subvolume: '${fs}'" sudo btrfs filesystem defragment ${fs} - echo "Rebalance subvolume: '${fs}'" - sudo btrfs balance start ${fs} + if test $rebalance -eq 1; then + echo "Rebalance subvolume: '${fs}'" + sudo btrfs balance start ${fs} + fi fi else echo "ERROR: no device found for $fs" 1>&2