Untitled

mail@pastecode.io avatar
unknown
php
3 years ago
3.6 kB
6
Indexable
Never

<?php


/**
 * create db backup on remote server
 * copy db remote to local
 * create db backup on local an archive
 * import dumped db to local instance
 */
task('magento:database:pull', function () {

    $backupFileName = 'dbbackup.sql';

    /** Generate Backup file on remote server */
    run("cd {{release_path}} && {{bin/php}} {{release_path}}/n98-magerun2.phar db:dump --compression=\"gzip\" --strip=\"catalogsearch_* core_cache* index*event enterprise_logging_event* *_cl @log @stripped\" var/$backupFileName");
    writeln("<info>✔</info> Uzak sunucuda veritabani yedegi olusturuldu.");

    $defaultHost = 'test';
    $host = null;
    if(input()->hasArgument('stage')) {
        $host = input()->getArgument('stage');
    }
    $host = $host ?? $defaultHost;

    /** Copy db backup from remote server $localPath */
    $localPath = runLocally('pwd');
    $sshRemotePath = \sprintf('%s@%s:%s/var/%s.gz',
                    host($host)->getUser(),
                    host($host)->getRealHostname(),
                    host($host)->get('current_path'),
                    $backupFileName
                    );
    runLocally("scp -v $sshRemotePath $localPath/$backupFileName.gz");
    writeln("<info>✔</info> Veritabani yedegi lokal diske kopyalandi.");

    /** Delete backup file from remote */
    run("rm {{release_path}}/var/$backupFileName.gz");
    writeln("<info>✔</info> Uzak sunucudaki olusturulan veritabani yedegi silindi.");

    /** backup current local db */
    $dateString = date('Y-m-d-H-i-s');
    $archivedBackupFileName = $dateString.'_'.$backupFileName;
    runLocally("mkdir -p var/dbbackup/local && ./n98-magerun2.phar db:dump --compression='gzip' --strip='catalogsearch_* core_cache* index*event enterprise_logging_event* *_cl @log @stripped' var/dbbackup/local/$archivedBackupFileName");
    writeln("<info>✔</info> Lokal ortam veritabani yedegi alindi. => var/dbbackup/local/$archivedBackupFileName");

    /** import db */
    runLocally("gunzip $backupFileName.gz && ./n98-magerun2.phar db:import $backupFileName");
    /** move remote db backup */
    runLocally("mkdir -p var/dbbackup/remote && mv $backupFileName var/dbbackup/remote/$archivedBackupFileName");
    writeln("<info>✔</info> Veritabani lokal ortama import edildi.");
});



/**
 * pull media data from remote host
 */
task('magento:media:pull', function() {

    $defaultHost = 'test';
    $host = null;
    if(input()->hasArgument('stage')) {
        $host = input()->getArgument('stage');
    }
    $host = $host ?? $defaultHost;

    $zipFolder = 'pub';
    $zipFileBasename = 'pubmediabackup.zip';
    $acceptedFiles = [
        'jpg', 'png', 'jpeg', 'svg', 'pdf'
    ];
    $excludingDirs = [
        'pub/media/catalog/product/cache/*',
        'pub/media/captcha/*',
        'pub/tmp/*',
        'pub/static/*',
        'pub/.*',
        'pub/.*/.*',
        'pub/.*/.*/.*',
    ];

    $zipCommand = "cd {{release_path}} && rm -f {{release_path}}/$zipFileBasename && zip -r $zipFileBasename $zipFolder -i '*.".implode("' '*.", $acceptedFiles).
                   "' -x '".implode("' '", $excludingDirs)."'";
    run($zipCommand);

    /** Copy pub media backup from remote server $localPath */
    $localPath = runLocally('pwd');
    $sshRemotePath = \sprintf('%s@%s:%s/%s',
        host($host)->getUser(),
        host($host)->getRealHostname(),
        host($host)->get('current_path'),
        $zipFileBasename
    );
    runLocally("scp $sshRemotePath $localPath/$zipFileBasename && unzip -o $zipFileBasename");
    /** remove remote backup file */
    run("rm -f {{release_path}}/$zipFileBasename");
});