quote filesystem path

master
Marc Wäckerlin 7 years ago
commit 38acf262f8
  1. 23
      btrfs-snapshots.sh

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

Loading…
Cancel
Save