#!/bin/sh

create_archive ()
{
  /bin/tar -c -f - /var/log 2>/dev/null | /usr/bin/zstd -o /opt/userdata/log/var_log_$(/bin/date +"%Y%m%d_%H%M").tar.zst 2>/dev/null
}

clean_old_archives ()
{
  while [ $(/bin/ls -1 /opt/userdata/log/var_log_*.tar.zst 2>/dev/null| /usr/bin/wc -l) -ge 5 ]; do
    /bin/rm $(/bin/ls -1 /opt/userdata/log/var_log_*.tar.zst | /usr/bin/head -n 1 )
  done
}

clean_old_archives
create_archive
