Collection of scripts used for data backup and snapshots, i.e. for btrfs and lizardfs.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
3.3 KiB

#!/bin/bash -e
# defaults
HOURLY_DEL=25
DAILY_DEL=8
WEEKLY_DEL=5
MONTHLY_DEL=
SOURCES=()
DESTINATION=
# overwrite defaults in configs
if test -e /etc/lizardfs-snapshots.conf; then
. /etc/lizardfs-snapshots.conf
fi
if test -e ~/.lizardfs-snapshots; then
. ~/.lizrdfs-snapshots
fi
# evaluate commandline
del=
periodity=${0%/*}
periodity=${periodity##*/}
if [[ $periodity =~ ^cron\... ]]; then
periodity=${periodity#cron}
else
periodity=
fi
case "$periodity" in
(.hourly) del=${HOURLY_DEL};;
(.daily) del=${DAILY_DEL};;
(.weekly) del=${WEEKLY_DEL};;
(.monthly) del=${MONTHLY_DEL};;
(*) periodity=".manual";;
esac
while test $# -gt 0; do
case "$1" in
(-h|--help) cat <<EOF
$0 [OPTIONS] [[SOURCES...] DESTINATION]
OPTIONS
-h, --help show this help
-n, --dry-run execute dry run, do not backup, just show commands
-d, --del <number> delete old snapshots beginning at <numbers>-th
CONFIGURATION FILE
You can have a configuration file lizardfs-snapshots.conf either in /etc or in your home.
This may define the following bash variables:
HOURLY_DEL, DAILY_DEL, WEEKLY_DEL, MONTHLY_DEL
defining the parameter --del for hourly, daily, weekly, monthly
SOURCES bash array that defines the sources to backup
DESTINATION snapshot target path
DESCRIPTION
To create regular snapshots on a daily base, just run:
sudo cp lizardfs-snapshots.sh /etc/cron.daily/lizardfs-snapshots
If lizardfs-snapshots is run from a cron.daily, cron.hourly,
cron. monthly or cron.weekly directory, the periodity is automatically
appended to the snapshot name, and the expiry is set meaningfull:
- hourly → keep only 24 hours, --del 25
- daily → keep only 7 days, --del 8
- weekly → keep only 4 weeks, --del 5
- monthly → old snapshots are not deleted
EOF
exit;;
(-n|--dry-run) dryrun=1;;
(-d|--del) shift; del="$1";;
(*) break;;
esac
if test $# -lt 1; then
echo "ERROR: missing argument, try $0 --help" 1>&2
exit 1
fi
shift
done
while test $# -gt 1; do
SOURCES+=("$1")
shift
done
if test $# -eq 1; then
DESTINATION="$1"
fi
if test -z "${SOURCES[*]}"; then
echo "ERROR: no sources specified, try $0 --help" 1>&2
exit 1
fi
if test -z "${DESTINATION}"; then
echo "ERROR: no destination specified, try $0 --help" 1>&2
exit 1
fi
if $dryrun -eq 0 && ! ( test -d "${DESTINATION}" || mkdir -p "${DESTINATION}" ); then
echo "ERROR: cannot create destination path '$DESTINATION', try $0 --help" 1>&2
exit 1
fi
date="-$(date +%Y-%m-%d-%H-%M)"
for src in "${SOURCES[@]}"; do
target="${src##*/}${periodity}"
if test $dryrun -eq 1; then
echo -e "→ \e[1mbackup $src\e[0m"
echo " " "lizardfs snapshot '${src}' '${DESTINATION}/${target}${date}'"
if test -n "$del"; then
for f in $(ls -d1 "${DESTINATION}/${target}"-* | sort -r | tail -n +"$del"); do
echo " " "rm -rf '$f'"
done
fi
else
echo -e "→ \e[1mbackup $src\e[0m"
lizardfs snapshot "${src}" "${DESTINATION}/${target}${date}"
if test -n "$del"; then
for f in $(ls -d1 "${DESTINATION}/${target}"-* | sort -r | tail -n +"$del"); do
rm -rf "$f"
done
fi
fi
done