# Date format
NOW=$(date +"%Y-%m-%d")

OVERLEAF_DIRECTORY="/home/debian/docker/compose/project/service/overleaf-toolkit"

# Backup config
$OVERLEAF_DIRECTORY/bin/backup-config -m tar $OVERLEAF_DIRECTORY/backup/$NOW-overleaf-config-backup.tar
# Backup mongo
tar --create --file $OVERLEAF_DIRECTORY/backup/$NOW-overleaf-mongo-backup.tar $OVERLEAF_DIRECTORY/data/mongo
# Backup sharelatex
tar --create --file $OVERLEAF_DIRECTORY/backup/$NOW-overleaf-sharelatex-backup.tar $OVERLEAF_DIRECTORY/data/sharelatex

# Pruning
clean_up_backups() {

    # list all files older than 3 days
    FILE_TO_DELETE=$(find $OVERLEAF_DIRECTORY/backup/ -type f -mtime 3)

    # delete such files
    for i in $FILE_TO_DELETE; do
        rm $i
    done
}

clean_up_backups