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


class TB_Server_Job_DailyDbBackup extends TB_Server_Job_Base
{
    public function __construct( TB_Shared_Ent_TeamData_Job $jobEntity )
    {
        $this->entity = $jobEntity;
    }

    public function execute()
    {
        // Remove old DB dumps
        $dayInSeconds = 60 * 60 * 24;
        $pastDays = 7;
        $dumpFile = Francis_Utils_Config::get( 'path.backup_folder' ) . '/dbdump';
        while ( file_exists( $dumpFile . date( 'Y-m-d', ( time() - ( $pastDays * $dayInSeconds ) ) ) . '.zip' ) ) {
            unlink( $dumpFile . date( 'Y-m-d', ( time() - ( $pastDays * $dayInSeconds ) ) ) . '.zip' );
            $pastDays++;
        }

        // DB Core
        $db_host = Francis_Utils_Config::get( 'db.tbcore.host' );
        $db_user = Francis_Utils_Config::get( 'db.tbcore.user' );
        $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("mysql -u $db_name -p'$db_passwd' --allow-keywords --add-drop-table --complete-insert --quote-names --routines $db_name > $sql_file");
        exec("mysqldump -h $db_host -u $db_user -p'$db_passwd' $db_name > $sql_file");

        // DB Teamdata
        $db_host = Francis_Utils_Config::get( 'db.tbteamdata.host' );
        $db_user = Francis_Utils_Config::get( 'db.tbteamdata.user' );
        $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 -h $db_host -u $db_user -p'$db_passwd' $db_name > $sql_file");
        //exec("mysql -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 );
        echo 'bla';
    }

    public function onDone()
    {
        // Reset event to start the next day
        $nowDt = new DateTime( 'now',  new \DateTimeZone( "UTC" )  );
        $nowDt->setTime( 2, 0, 0 );
        $nowDt->modify( '+1 day' );
        $this->entity->trigger_dt = $nowDt;
        $this->entity->job_status = TB_Shared_Ent_TeamData_Job::JOB_STATUS_READY;
        $this->entity->touch( 'job_status' );
        $this->entity->save();
    }

    public static function getRefId()
    {
        return 'system-daily-db-backup';
    }
}