#!/bin/sh

set -x

FREQ=$1; shift
POSTGRES=/root/backup/postgres
BACKUP=/root/backup

if [[ "$FREQ" = "daily" ]]; then
    mkdir -p $POSTGRES
    ssh postgres@anna "pg_dumpall"  | xz -9 -T2 > $POSTGRES/postgres-anna.dump.xz
fi

/usr/bin/rsnapshot -c /root/arch-deploy/rsnapshot/disks.conf $FREQ >&2
R=$?

if [[ "$FREQ" = "daily" ]]; then
    TS=$BACKUP/daily.0/timestamp
    rm $TS
    date +"%Y-%m-%d_%H-%M-%S" > $TS
fi

set +x
{ df -h /backup* /root/backup*; echo; cd $BACKUP; for i in daily.{0..13} weekly.{0..7}; do printf "$i: "; cat $i/timestamp; done; } |& mail -s "backup - antea - $R" xstill@fi.muni.cz
