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.
127 lines
3.4 KiB
127 lines
3.4 KiB
7 years ago
|
#!/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=
|
||
7 years ago
|
dryrun=0
|
||
7 years ago
|
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
|
||
|
|
||
7 years ago
|
You can have a configuration file lizardfs-snapshots.conf either in
|
||
|
/etc or in your home.
|
||
7 years ago
|
|
||
|
This may define the following bash variables:
|
||
|
HOURLY_DEL, DAILY_DEL, WEEKLY_DEL, MONTHLY_DEL
|
||
7 years ago
|
defining the parameter --del for
|
||
|
hourly, daily, weekly, monthly
|
||
7 years ago
|
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
|
||
7 years ago
|
if test "$dryrun" -eq 0 && ! ( test -d "${DESTINATION}" || mkdir -p "${DESTINATION}" ); then
|
||
7 years ago
|
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"
|
||
6 years ago
|
echo " " "lizardfs makesnapshot -lf '${src}' '${DESTINATION}/${target}${date}'"
|
||
7 years ago
|
if test -n "$del"; then
|
||
|
for f in $(ls -d1 "${DESTINATION}/${target}"-* | sort -r | tail -n +"$del"); do
|
||
7 years ago
|
echo " " "lizardfs rremove '$f'"
|
||
7 years ago
|
done
|
||
|
fi
|
||
|
else
|
||
|
echo -e "→ \e[1mbackup $src\e[0m"
|
||
6 years ago
|
lizardfs makesnapshot -lf "${src}" "${DESTINATION}/${target}${date}"
|
||
7 years ago
|
if test -n "$del"; then
|
||
|
for f in $(ls -d1 "${DESTINATION}/${target}"-* | sort -r | tail -n +"$del"); do
|
||
7 years ago
|
lizardfs rremove "$f"
|
||
7 years ago
|
done
|
||
|
fi
|
||
|
fi
|
||
|
done
|