#!/bin/bash
## (c) 2017-2020 torh@bogus.net
## synchronises ~/$DIRS between two hosts
## - only copies newer files 
## - keeps 1 backup of old/modified files
##
## make sure you've set up ssh w/key authentication
## protip: install krypton (https://krypt.co) to 
## make key protection painless
##
## usage: syncdir.sh [<host> [<user> [<dirstosync>]]]
##
## change DIRS to something that you want to sync
## or specify after host and username on command line
## - e.g. syncdir.sh somehost someuser "dir1 dir2 dir3"
##
DIRS="sync development mail docs"
##
if [[ -n $1 ]]; then
	SYNCHOST=$1
else
	SYNCHOST=example.bogus.net
fi
if [[ -n $2 ]]; then
	SYNCUSER=$2
else
	SYNCUSER=myuser
fi
if [[ -n $3 ]]; then
	SYNCDIRS=$3
else
	SYNCDIRS=$DIRS
fi
for SYNCDIR in $SYNCDIRS;do
	rsync --exclude="*~~" --delete-excluded -bavPu --chmod=og-rwx $SYNCUSER@$SYNCHOST:$SYNCDIR ~/.
	rsync --exclude="*~~" --delete-excluded -bavPu --chmod=og-rwx ~/$SYNCDIR $SYNCUSER@$SYNCHOST:
done
