dotfiles

My dotfiles, nothing really interesting to see ...
git clone https://git.onna.be/dotfiles.git
Log | Files | Refs | README

commit 3fde356858601a630c944b4b59c86d12ddc8bcbb
parent 42f940aa5a1318d009186a9380d241974aa1a024
Author: Paco Esteban <paco@onna.be>
Date:   Tue, 12 Jun 2018 18:52:02 +0200

rsync based backup script

Diffstat:
bin/backup.sh | 57+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 57 insertions(+), 0 deletions(-)

diff --git a/bin/backup.sh b/bin/backup.sh @@ -0,0 +1,57 @@ +#!/usr/bin/env bash + +# ======================================================================== +# = Rsync based backup script. +# = It uses the '--link-dest' option of rsync to create hard links to the +# = previous full backup. So incremental + full all in one. +# = +# = backup.sh <ORIG_DIR> <DEST_DIR> [RETENTION] [FILELIST] +# = <ORIG_DIR> origin resource, can be a folder local or remote +# = <DEST_DIR> destination resource, can be a folder local or remote +# = [RETENTION] number of days for backup retention (default: 30) +# = [FILELIST] list of files to backup exclusively (recursive) +# ======================================================================== + +ORIG_DIR="$1" +DEST_DIR="$2" +DATE=$(/bin/date +%Y%m%d%H%M) +RETENTION=${3:-30} +FILELIST="$4" +RSYNC_OPTIONS=() + +if [[ -z "$ORIG_DIR" || -z "$DEST_DIR" ]]; then + echo "ORIG_DIR and DEST_DIR are mandatory !!" + exit 1 +fi + +# delete older than $RETENTION days +echo "** deleting files older than $RETENTION days ..." +find "$DEST_DIR" -maxdepth 1 -name "????????????" -mtime "+$RETENTION" -delete + +LAST_BACKUP=$(ls -1dtr "$DEST_DIR"/* 2>/dev/null | tail -1) + +# if LAST_BACKUP does not exist, it's our first run +if [[ ! -z "$LAST_BACKUP" ]]; then + RSYNC_OPTIONS+=("--link-dest=$LAST_BACKUP") +fi + +if [[ ! -z "$FILELIST" ]]; then + RSYNC_OPTIONS+=("--files-from=$FILELIST") +fi + +# and now we run rsync +echo "** backing up $ORIG_DIR - $DATE ..." +mkdir "$DEST_DIR/$DATE" +rsync -azr --numeric-ids "${RSYNC_OPTIONS[@]}" "$ORIG_DIR/" "$DEST_DIR/$DATE/" +RET="$?" + +# touch to make rsync and find play nice with each other +touch "$DEST_DIR/$DATE" + +if [[ "$RET" != "0" ]] +then + echo "!! Backup Error (rsync) !!" + exit 1 +fi + +echo "done."