#!/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/sysctl.conf /opt/configs/conf/
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 /root/.acme.sh/*ecc /opt/configs/acme/
cp -rf /opt/scripts/update/restore.sh /opt/apps/syncthing/data/common/F-Backup/Linux/
scp -r /opt/configs/certs/ arm1:/opt/ > /dev/null &
scp -r /opt/configs/certs/ arm2:/opt/ > /dev/null &
scp -r /opt/configs/certs/ amd:/opt/ > /dev/null &
wait

cd /opt && t=`date +%Y%m%dT%H%M%S`
rsync --delete-after -avz apps configs logs scripts websites wd/72-Backups/VPS/ > /opt/logs/rsync/rsync_${t}.log
rsync --delete-after -avz /opt/apps/syncthing/data/* wd/72-Backups/Syncthing/ > /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`
    tar -I pixz -cf /opt/wd/72-Backups/vps-${t}.tar.gz /opt/wd/72-Backups/VPS > /dev/null 2>&1
fi
set +x
echo End   Time: `date`
echo "========================================================================="
echo
echo

