#!{{ shell_binary }}
LC_ALL=C
export LC_ALL
umask 077
sync_element () {
  path=$1
  backup_path=$2
  shift 2
  element_list=$*
  for element in $element_list
  do
    cd $path;
    if [ -f $element ] || [ -d $element ]; then
       {{ rsync_binary }} -rlptgov --safe-links --delete $element  $backup_path;
    fi
  done
}
sync_element {{ directory['srv'] }}/runner  {{ directory['backup'] }}/runner/ instance project  proxy.db
# We sync .* appart
date +%s -u > {{ directory['etc'] }}/.resilient-timestamp
cp -r {{ directory['etc'] }}/.??* {{ directory['backup'] }}/etc/
sync_element {{ directory['etc'] }}  {{ directory['backup'] }}/etc/ config.json ssh
if [ -d {{ directory['backup'] }}/runner/software ]; then
  rm {{ directory['backup'] }}/runner/software/*
fi

cd {{ directory['backup'] }} && find -type f ! -name backup.signature -print0 | xargs -P4 -0 sha256sum | LC_ALL=C sort -k 66 > backup.signature
