#!/bin/bash
# paul@vandervlis.nl
# push backup to client

# Local DIR
LDIR="/backup/client8"

# Excluded from sync:
EXCLUDE="\
--exclude=/mnt/** \
--exclude=/proc/** \
--exclude=/tmp/** \
--exclude=/lost+found \
--exclude=/sys/** \
--exclude=/var/cache/apt/archives/** \
--exclude=/var/log/** \
--exclude=/home/** \
--exclude=/data/** \
--exclude=/etc/hostname \
--exclude=/etc/krb5.keytab \
--exclude=/etc/udev/rules.d/** \
--exclude=/etc/ssh/ssh_host_* \
--exclude=/backup/** \
--exclude=/etc/udev/rules.d/70-persistent-net.rules \
--exclude=/etc/krb5.keytab \
--exclude=/var/tmp/systemd-* \
"

# ask machinename when not on commandline:
if test "$1" = ""; then
  read -p "Machine name (example: mm-n001): " machine
else
  machine=$1
fi
if test "$machine" = ""; then
  echo no machine.
  exit
fi

# start
echo -e "\n`date +%T` Start sync"

#sync to client
echo -e "\n`date +%T` Syncing to client:"
cd $LDIR
nice rsync -axzve ssh --numeric-ids --del $EXCLUDE \
   ./ $machine:/

echo -e "\n`date +%T` Update grub config file:"
ssh $machine "/usr/sbin/update-grub"

echo -e "\n`date +%T` Install grub in the MBR:"
ssh $machine "/usr/sbin/grub-install --recheck /dev/sda"

echo -e "\n`date +%T` Flush filesystem buffers to disk:"
ssh $machine "/bin/sync"

# Ready
echo -e "\n`date +%T` Ready, time to reboot."
echo "Do 'ssh $machine', and execute 'reboot'."
echo
