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 | 17 ++- lizardfs/README.md | 25 ++++ lizardfs/lizsrdfs-snapshots.sh | 123 ++++++++++++++++++ 6 files changed, 188 insertions(+), 33 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 +if ! test -x "$1"; then + echo "ERROR: call $0 path/script" 1>&2 + exit 1 +fi + for f in hourly daily weekly monthly; do - sudo cp btrfs-snapshots.sh /etc/cron.$f/btrfs-snapshots; + name=${1##*/} + echo "installing $1 to /etc/cron.$f/${name%.*}" + sudo cp $1 /etc/cron.$f/${name%.*} done -if ! test -e /etc/btrfs-snapshots.conf; then - sudo tee /etc/btrfs-snapshots.conf > /dev/null < 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