#!/bin/bash

set -o errexit

# This is to help with disk space monitoring - run "df" before and after
echo "Disk space when RunExport starts:" ; df -m
trap 'echo "Disk space when RunExport ends:" ; df -m' 0

mb_server=`dirname $0`/..
eval `$mb_server/admin/ShowDBDefs`
cd $mb_server

. ./admin/functions.sh
make_temp_dir

set +o errexit
WITH_FULL_EXPORT=$1 ; shift
set -o errexit

# Make a date/time stamp for the full export
if [ "$WITH_FULL_EXPORT" ]
then
    EXPORT_STAMP=`TZ=UTC date +'%Y%m%d-%H%M%S'`
    EXPORT_ARG="--with-full-export"
else
    EXPORT_ARG="--without-full-export"
fi

# Create necessary directories and set permissions.  If we're not doing a full
# export then $EXPORT_STAMP is empty, so that argument is ignored.

mkdir -m "$BACKUP_DIR_MODE" -p \
    "$BACKUP_DIR"/fullexport/ \
    "$BACKUP_DIR"/fullexport/"$EXPORT_STAMP" \
    "$BACKUP_DIR"/replication
chown "$BACKUP_USER:$BACKUP_GROUP" \
    "$BACKUP_DIR"/fullexport/ \
    "$BACKUP_DIR"/fullexport/"$EXPORT_STAMP" \
    "$BACKUP_DIR"/replication

mkdir -m "$FTP_DIR_MODE" -p \
    "$FTP_DATA_DIR"/fullexport/ \
    "$FTP_DATA_DIR"/fullexport/"$EXPORT_STAMP" \
    "$FTP_DATA_DIR"/replication
chown "$FTP_USER:$FTP_GROUP" \
    "$FTP_DATA_DIR"/fullexport/ \
    "$FTP_DATA_DIR"/fullexport/"$EXPORT_STAMP" \
    "$FTP_DATA_DIR"/replication

echo `date`" : Making database snapshot"
./admin/ExportAllTables \
    $EXPORT_ARG \
    --with-replication \
    --output-dir "$TEMP_DIR" \
    --compress \
    --replication-callback "$mb_server/admin/CopyReplication $TEMP_DIR" \
    || exit $?

if [ "$WITH_FULL_EXPORT" ]
then
    # The remaining files are the full database export, including the
    # private data.
    # Copy the full export to the backup directory
    chown "$BACKUP_USER:$BACKUP_GROUP" "$TEMP_DIR"/*
    chmod "$BACKUP_FILE_MODE" "$TEMP_DIR"/*
    cp -a "$TEMP_DIR"/* "$BACKUP_DIR"/fullexport/"$EXPORT_STAMP"/

    # Now remove the "private" file(s)
    rm -rf "$TEMP_DIR"/mbdump-private.tar.bz2
    rm -rf "$TEMP_DIR"/mbdump-private.tar.bz2.asc
    rm -rf "$TEMP_DIR"/mbdump-private.tar.bz2.gpg

    # and move the rest to the FTP directory
    chown "$FTP_USER:$FTP_GROUP" "$TEMP_DIR"/*
    chmod "$FTP_FILE_MODE" "$TEMP_DIR"/*
    mv "$TEMP_DIR"/* "$FTP_DATA_DIR"/fullexport/"$EXPORT_STAMP"/

    # Finally create a "latest-is" file, indicating the export we just did.
    rm -rf "$FTP_DATA_DIR"/fullexport/latest-is-*
    > "$FTP_DATA_DIR"/fullexport/latest-is-"$EXPORT_STAMP"
    chmod "$FTP_FILE_MODE" "$FTP_DATA_DIR"/fullexport/latest-is-"$EXPORT_STAMP"
    chown "$FTP_USER:$FTP_GROUP" "$FTP_DATA_DIR"/fullexport/latest-is-"$EXPORT_STAMP"

    # Finally finally, create a LATEST file whose *contents* are this export's tag
    rm -rf "$FTP_DATA_DIR"/fullexport/LATEST
    echo "$EXPORT_STAMP" > "$FTP_DATA_DIR"/fullexport/LATEST
    chmod "$FTP_FILE_MODE" "$FTP_DATA_DIR"/fullexport/LATEST
    chown "$FTP_USER:$FTP_GROUP" "$FTP_DATA_DIR"/fullexport/LATEST

    $mb_server/bin/delete-old-fullexports -k -r "$BACKUP_DIR"/fullexport "$FTP_DATA_DIR"/fullexport
    $mb_server/bin/rsync-fullexport-files
fi

$mb_server/bin/rsync-replication-files

# eof
