#!/bin/bash

cd /4server/data/

while :
do

for dir in ???-???-*; do
  if [ -d "${dir}/cc" ]; then
    if [ -f "${dir}/cc/backup" ]; then
      echo "$(date '+%Y-%m-%d %H:%M:%S') BACKUP for: ${dir%/}"
      /4server/sbin/backupContainer ${dir%/} 2
      rm "${dir}/cc/backup"
    fi

    if [ -f "${dir}/cc/restart" ]; then
      echo "$(date '+%Y-%m-%d %H:%M:%S') Restart for: ${dir%/}"
      /4server/sbin/startContainer ${dir%/}
      rm "${dir}/cc/restart"
    fi

    if [ -f "${dir}/cc/restore" ]; then
      FILENAME=$(head -n 1 "${dir}/cc/restore")
      echo "$(date '+%Y-%m-%d %H:%M:%S') Restore for: ${dir%/} - $FILENAME"
      /4server/sbin/ODOO_19/restore ${dir%/} $FILENAME
      rm "${dir}/cc/restore"
    fi
  fi

  if [ -d "${dir}/n8n" ]; then
    if [ -f "${dir}/n8n/database.sqlite-journal" ]; then
      echo "$(date '+%Y-%m-%d %H:%M:%S') Restart N8N (journal) for: ${dir%/}"
      /4server/sbin/startContainer ${dir%/}
    fi
  fi

done

sleep 60

done
