<?php
/********************************************************************************
 * (c)1337 aheadware.com - All rights reserved
 ********************************************************************************/

require_once __DIR__ . '/../../config/boot_global.php';
require_once __DIR__ . '/../../config/boot_local.php';

// DB Core
$db_name = Francis_Utils_Config::get( 'db.tbcore.name'  );
$db_passwd = Francis_Utils_Config::get( 'db.tbcore.pass' );

$sql_file = Francis_Utils_Config::get( 'path.backup_folder' ) . "/tbcore.sql";
exec("mysqldump -u $db_name -p'$db_passwd' --allow-keywords --add-drop-table --complete-insert --quote-names --routines $db_name > $sql_file");

// DB Teamdata
$db_name = Francis_Utils_Config::get( 'db.tbteamdata.name'  );
$db_passwd = Francis_Utils_Config::get( 'db.tbteamdata.pass' );

$sql_file = Francis_Utils_Config::get( 'path.backup_folder' ) . "/tbteamdata.sql";
exec("mysqldump -u $db_name -p'$db_passwd' --allow-keywords --add-drop-table --complete-insert --quote-names --routines $db_name > $sql_file");

// Zip
$zipFilename = Francis_Utils_Config::get( 'path.backup_folder' ) . '/dbdump' . date( 'Y-m-d' ) . '.zip';
$zip = new ZipArchive();
if ( !$zip->open( $zipFilename, ZipArchive::CREATE ) ) {
	TB_Server_Utils_Log::get()->log( "Cannot zip db dump." );
	return;
}
$zip->addFile( Francis_Utils_Config::get( 'path.backup_folder' ) . '/tbcore.sql' , 'tbcore.sql' );
$zip->addFile( Francis_Utils_Config::get( 'path.backup_folder' ) . '/tbteamdata.sql' , 'tbteamdata.sql' );
$zip->close();

// Cleanup
$sql_file = Francis_Utils_Config::get( 'path.backup_folder' ) . "/tbcore.sql";
unlink( $sql_file );
$sql_file = Francis_Utils_Config::get( 'path.backup_folder' ) . "/tbteamdata.sql";
unlink( $sql_file );