#!/bin/bash

SRC_SRV="root@unifi.swallow-grue.ts.net"
SRC="/usr/lib/unifi/data/backup/autobackup/"
DST="/mnt/union/backup/unifi/udm/"

if ( scp -o "StrictHostKeyChecking=no" -r "$SRC_SRV:$SRC" "$DST" ); then
  RES=$(ssh root@unifi.swallow-grue.ts.net "ls -1 /usr/lib/unifi/data/backup/autobackup/ | wc -l") # Count the number of files
  if [ "$RES" -gt 0 ]; then
    ssh root@unifi.swallow-grue.ts.net "rm $SRC"*
  fi 
fi