#!/bin/sh

targethost=kiiski
exitnow=0

while ! ssh $targethost date; do
  echo "Waiting 60 seconds and trying again (couldn't reach $targethost)"
  sleep 60 
done

echo "Backup starts: $(date)"

exclude="Loki_Demos pics ohjelmointi/cvs wav vmware qtella divx mp3 freenet kampe download windows images tmp openoffice sailo"

for i in $exclude; do
  exclude2="$exclude2 --exclude $i"
done

#echo $exclude2

cd ~/

#making sure we can later clone the system again...
dpkg --get-selections > recovery/installed_packages
dpkg --get-selections \* > recovery/all_selections

tar $exclude2 -c . /etc | bzip2 | gpg --encrypt -r tuma | ssh $targethost cat \> ~/atlantic-nightly-backup-UUSI.tar.bz2.gpg || echo "Backup didn't finish successfully, I won't overwrite previous ones!" && exit 1

ssh $targethost mv ~/atlantic-nightly-backup.tar.bz2.gpg ~/atlantic-nightly-backup-yesterday.tar.bz2.gpg
ssh $targethost mv ~/atlantic-nightly-backup-UUSI.tar.bz2.gpg atlantic-nightly-backup.tar.bz2.gpg

echo Kokotietoja:
ssh $targethost du -m ~/atlantic-nightly\*

echo "Backup finished: $(date)"

