From 182df98cef3b22c8a8cd30ae516137f039b3548e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20W=C3=A4ckerlin?= Date: Wed, 18 Apr 2018 11:30:20 +0200 Subject: [PATCH] reorg and new lizardfs-snapshots.sh --- README.md | 36 ++--- btrfs/README.md | 20 +++ .../btrfs-snapshots.sh | 0 install.sh | 19 ++- lizardfs/README.md | 25 ++++ lizardfs/lizsrdfs-snapshots.sh | 123 ++++++++++++++++++ 6 files changed, 189 insertions(+), 34 deletions(-) create mode 100644 btrfs/README.md rename btrfs-snapshots.sh => btrfs/btrfs-snapshots.sh (100%) create mode 100644 lizardfs/README.md create mode 100755 lizardfs/lizsrdfs-snapshots.sh diff --git a/README.md b/README.md index b0ba737..1b001a3 100644 --- a/README.md +++ b/README.md @@ -1,29 +1,17 @@ -Use BTRFS Snapshots For Backups -=============================== - -btrfs-snapshots.sh ------------------- - -Creates a snapshot for all btrfs volumes specified. Snapshot is named -from the subvol name or if there is no subvol, from the path by -appending `-snapshot-YYYY-MM-DD-HH-mm`. - -To create regular snapshots on a daily base, just run: - - sudo cp btrfs-snapshots.sh /etc/cron.daily/btrfs-snapshots - -If `btrfs-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. - -Try: `btrfs-snapshots.sh --help` +Backup And Snapshot Scripts +=========================== +- [BTRFS](btrfs/README.md) +- [LizardFs](lizardfs/README.md) Installation ------------ -Just call `./install.sh` to install all cron jobs. Then there will -always be the last 24 hourly backups, the last 7 daily backups, tha -last 4 weekly backups and all monthly backups of all your btrfs -filesystems mounted in `/etc/fstab`. \ No newline at end of file +Just call `./install.sh path/script` to install all cron jobs. Then +there will always be the last 24 hourly backups, the last 7 daily +backups, the last 4 weekly backups and all monthly backups, unless you +configure something different. + +e.g.: + + ./install.sh lizardfs/lizsrdfs-snapshots.sh diff --git a/btrfs/README.md b/btrfs/README.md new file mode 100644 index 0000000..84d4325 --- /dev/null +++ b/btrfs/README.md @@ -0,0 +1,20 @@ +Use BTRFS Snapshots For Backups +=============================== + +btrfs-snapshots.sh +------------------ + +Creates a snapshot for all btrfs volumes specified. Snapshot is named +from the subvol name or if there is no subvol, from the path by +appending `-snapshot-YYYY-MM-DD-HH-mm`. + +To create regular snapshots on a daily base, just run: + + sudo cp btrfs-snapshots.sh /etc/cron.daily/btrfs-snapshots + +If `btrfs-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. + +Try: `btrfs-snapshots.sh --help` diff --git a/btrfs-snapshots.sh b/btrfs/btrfs-snapshots.sh similarity index 100% rename from btrfs-snapshots.sh rename to btrfs/btrfs-snapshots.sh diff --git a/install.sh b/install.sh index 9781218..e706b93 100755 --- a/install.sh +++ b/install.sh @@ -1,13 +1,12 @@ #!/bin/bash -for f in hourly daily weekly monthly; do - sudo cp btrfs-snapshots.sh /etc/cron.$f/btrfs-snapshots; -done -if ! test -e /etc/btrfs-snapshots.conf; then - sudo tee /etc/btrfs-snapshots.conf > /dev/null <&2 + exit 1 fi + +for f in hourly daily weekly monthly; do + name=${1##*/} + echo "installing $1 to /etc/cron.$f/${name%.*}" + sudo cp $1 /etc/cron.$f/${name%.*} +done diff --git a/lizardfs/README.md b/lizardfs/README.md new file mode 100644 index 0000000..981fb30 --- /dev/null +++ b/lizardfs/README.md @@ -0,0 +1,25 @@ +LizardFS Snapshots And Backups +============================== + +Creates a lizardfs snapshot of all sources to a destination. If +`--del` is specified, only the given number of snapshots is kept, +older snapshots are deleted. + +To create regular snapshots on a daily base, just run: + + sudo cp btrfs-snapshots.sh /etc/cron.daily/btrfs-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. Otherwise the name is `manual` and nothing is expired by +default. + +You can create a configuration file, specifiying some defaults, +e.g. `/etc/lizardfs-snapshots.conf`: + + SOURCES=( /var/volumes/configs /var/volumes/volumes ) + DESTINATION=/var/volumes/backup/snapshots + MONTHLY_DEL=13 + +Try: `lizardfs-snapshots.sh --help` \ No newline at end of file diff --git a/lizardfs/lizsrdfs-snapshots.sh b/lizardfs/lizsrdfs-snapshots.sh new file mode 100755 index 0000000..e61ddec --- /dev/null +++ b/lizardfs/lizsrdfs-snapshots.sh @@ -0,0 +1,123 @@ +#!/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 < delete old snapshots beginning at -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