#! /bin/bash

date=$(date -u "+%Y%m%d_%H%M%S")
hostname=$(hostname)

volume_path="/mnt/provision"
host_path="/mnt/provision/backup/${hostname}"
backup_path="${host_path}/${date}"
borg_path="/mnt/provision/borg"

borg_args="-C zlib,6 -x"
borg_prune_args="--keep-daily=7 --keep-weekly=4 --keep-monthly=6 --keep-within 1w"

etcdctl3 () { 
    ETCDCTL_API=3 /usr/bin/etcdctl --cert /etc/etcd/peer.crt --key /etc/etcd/peer.key --cacert /etc/etcd/ca.crt --endpoints "https://${hostname}:2379" ${@}
}

#check=$(df | awk '{ print $6 }' | grep -P "^${volume_path}$")
#if [ $? -ne 0 -o -z "$check" ]; then
if [ ! -d "$borg_path" ]; then
    echo "Mounting $volume_path"
    mount "$volume_path"
#    check=$(df | awk '{ print $6 }' | grep -P "^${volume_path}$")
#    [ $? -ne 0 -o -z "$check" ] && { echo "The volume $volume_path is not mounted. Skipping..." ; exit 1 ; }
fi

[ -d "$backup_path" ] && { echo "Something wrong, path $backup_path already exists..." ; exit 1 ; }

# Check the provision volume is mounted
mkdir -p "$backup_path" || { echo "Can't create ${backup_path}" ; exit 1 ; }

# etcd
mkdir -p "$backup_path/etcd" || { echo "Can't create ${backup_path}/etcd" ; exit 1 ; }
etcdctl3 --endpoints="192.168.213.1:2379" snapshot save "$backup_path/etcd/snapshot.db" > /dev/null

# heketi
mkdir -p "$backup_path/heketi" || { echo "Can't create ${backup_path}/heketi" ; exit 1 ; }
heketi-cli -s  http://heketi-storage.glusterfs.svc.cluster.local:8080 --user admin --secret "$(oc get secret heketi-storage-admin-secret -n glusterfs -o jsonpath='{.data.key}' | base64 -d)" topology info > "$backup_path/heketi/heketi_topology.json"
heketi-cli -s  http://heketi-storage.glusterfs.svc.cluster.local:8080 --user admin --secret "$(oc get secret heketi-storage-admin-secret -n glusterfs -o jsonpath='{.data.key}' | base64 -d)" db dump > "$backup_path/heketi/heketi_db.json"
gluster --xml volume info > "$backup_path/heketi/gluster-info.xml"
gluster --xml volume status > "$backup_path/heketi/gluster-status.xml"
gluster volume status > "$backup_path/heketi/gluster.txt"

mkdir -p "$backup_path/lvm" || { echo "Can't create ${backup_path}/lvm" ; exit 1 ; }
lvs > "$backup_path/lvm/lvs.txt" 2>/dev/null
lvm fullreport --reportformat json > "$backup_path/lvm/lvm.json" 2>/dev/null
dmsetup ls --tree > "$backup_path/lvm/dmesetup.txt" 2>/dev/null
vglist=$(vgdisplay | grep -oP "VG Name\s+\K.*")
for vg in $vglist; do
    vgcfgbackup -f "$backup_path/lvm/vg-$vg.backup" "$vg" &>/dev/null
done


# Gluster
#mkdir -p "$backup_path/gluster" || { echo "Can't create ${backup_path}/gluster" ; exit 1 ; }
#(
#    cd /var/lib/
#    tar cjf $backup_path/gluster/var_lib_glusterd.tar.bz2 glusterd
#)

# etc
#mkdir -p "$backup_path/etc" || { echo "Can't create ${backup_path}/etc" ; exit 1 ; }
#(
#    cd /
#    tar cjf $backup_path/etc/etc.tar.bz2 etc --exclude=selinux --exclude=udev --exclude=bash_completion.d --exclude=etc/pki --exclude=etc/services --exclude=postfix --exclude=mc 
#)

if [ -d "$borg_path" ]; then
    borg_glusterd="/var/lib/glusterd"
    borg_etc="/etc -e */etc/selinux -e */etc/udev -e */etc/bash_completion.d -e */etc/pki -e */etc/services -e */etc/postfix -e */etc/mc"

    borg_list="* ${borg_glusterd} ${borg_etc}"

    ( 
	cd ${backup_path}
	borg create ${borg_args} "$borg_path::${hostname}-${date}" $borg_list
	borg prune ${borg_prune_args} --prefix "${hostname}-" "$borg_path"
    )
    find "$host_path" -maxdepth 1 -type d -mmin +720 -print0 | xargs -0 rm -rf
fi
