#!${dash-output:dash}

# trap "echo Backing up failed for $${:hostname}" ERR

export HOME=$${directory:home}

# Clean status file (no history needed)
${coreutils-output:rm} -f $${:status_log}
${coreutils-output:rm} -f $${:statistic_log}

# Inform about beginning of backup
${coreutils-output:echo} "`${coreutils-output:date} -u`,$${:statistic_log}, $${:hostname} backup running" >> $${:status_log}
${coreutils-output:echo} "Available only if backup succeed." >> $${:statistic_log}

# set -e
cd $${:datadirectory}
${rdiff-backup-output:rdiff-backup} \
  $${:exclude_string} \
  --include='$${:include}' \
  --exclude='**' \
  --remote-schema '${dropbear-output:ssh} -T -y -i $${:sshkey} %s $${:remote_schema}' \
  $${:connection}::/ ./

RESULT=$?
 
# Inform about backup status
${coreutils-output:rm} -f $${:status_log}
if [ $RESULT -eq 0 ]
then
  ${coreutils-output:echo} "`${coreutils-output:date} -u`,$${:statistic_log},$${:hostname} backup success" >> $${:status_log}
  ${coreutils-output:rm} -f $${:statistic_log}
  ${findutils-output:find} rdiff-backup-data/ -maxdepth 1 -name "session_statistic*" | ${coreutils-output:sort} | ${coreutils-output:tail} -n 1 | ${findutils-output:xargs} ${rdiff-backup-output:rdiff-backup} --calculate-average >> $${:statistic_log}
else
  ${coreutils-output:echo} "`${coreutils-output:date} -u`,$${:statistic_log},$${:hostname} backup failed" >> $${:status_log}
fi

# python scripts/verify_with_sudo.py ./ $${:connection}:/