#!/bin/bash

echo "========================================================================="
echo Start Time: `date`
set -x

cp -rf /var/spool/cron/crontabs/ /opt/configs/
cp -rf /usr/local/nginx/conf/nginx.conf /opt/configs/nginx/nginx.conf
cp -rf /usr/local/nginx/conf/domain_confs/ /opt/configs/nginx/
cp -rf /etc/docker/daemon.json /opt/configs/conf/
cp -rf /etc/netdata/ /opt/configs/
cp -rf /etc/pip.conf /opt/configs/conf/
cp -rf /etc/apt/sources.list /opt/configs/conf/
cp -rf /etc/ssh/sshd_config /opt/configs/conf/
cp -rf /root/.bashrc /opt/configs/conf/bashrc
cp -rf /root/.vim_runtime/my_configs.vim /opt/configs/conf/
cp -rf /root/.ssh/config /opt/configs/conf/
cp -rf /etc/samba/smb.conf /opt/configs/conf/
cp -rf /etc/network/interfaces /opt/configs/conf/
cp -rf /root/.acme.sh/*ecc /opt/configs/acme/
cp -rf /root/.acme.sh/rustle.cc_ecc/rustle.cc.{key,cer} /opt/apps/syncthing/data/common/A_Program/nginx-1.24.0/certs/
cp -rf /opt/apps/syncthing/data/common/A_Program/FRP/* /opt/apps/frp/frpc-xtcp-visitor/
cp -rf /etc/systemd/network /opt/configs/conf/
cp -rf /etc/modprobe.d/pcspkr-blacklist.conf /opt/configs/conf/
cp -rf /lib/firmware/intel/ ibt-0040-1050* /opt/configs/conf/intel/

cd /opt && t=`date +%Y%m%dT%H%M%S`
# rsync --delete-after -avz --exclude=apps/localcr/B_koel/local/music apps configs logs scripts websites wd/72-Backups/VPS/ > /opt/logs/rsync/rsync_${t}.log
rsync --delete-after -avz apps configs logs scripts websites wd/72-Backups/VPS/ > /opt/logs/rsync/rsync_${t}.log
cd /opt/logs/rsync/
let count=`ls | wc -l`
if [[ $count -gt 10 ]]; then
    rsync_logs=()
    for((i=1;i<=$count;i++)); do
        rsync_logs[$i]=`ls | sort -r | head -n $i | tail -1`
    done

    for i in `seq 11 $count`; do
        rm -rf ${rsync_logs[$i]}
    done
fi

# on 1st of every month, do a compress
if [[ `date +%d` == '01' ]]; then
    t=`date +%Y%m%d%H%M%S` && cd /opt/wd/72-Backups/

    tar -I pigz -cf vps-${t}.tar.gz \
    --exclude=VPS/apps/localcr/I_cloudreve/local/cloudreve/uploads \
    --exclude=VPS/apps/syncthing/data \
    VPS/ > /dev/null 2>&1
    # tar -I unpigz -xf vps-20231101162641.tar.gz

    scp -r /opt/configs/certs/ arm1:/opt/ > /dev/null 2>&1 &
    scp -r /opt/configs/certs/ arm2:/opt/ > /dev/null 2>&1 &
    scp -r /opt/configs/certs/  amd:/opt/ > /dev/null 2>&1 &
    wait
fi
set +x
echo End   Time: `date`
echo "========================================================================="
echo
echo

