想要在PHP中备份代码,其中从另一个指定(补丁)文件夹中读取要作为备份的文件。

时间:2016-10-24 11:32:43

标签: php ubuntu yii cron backup

我需要为我的项目备份代码,但条件是: -

我通过SVN生成一个Patch文件夹,其文件的结构与主Project文件夹中的文件相同(补丁的文件比主项目少)。我想编写一个代码来读取主项目,并只选择补丁中存在的那些文件并将其保存在新的备份文件夹中。

我写的代码是: -

    /*Patch Folder */
     $frmFoldr = 'patch_test/';
     $basepath = '/var/www/html/TestingBackupCron/';


    /*The Folder where files are to be backed up it would use basepath and toFoldr folder name. */
    $toFoldr = 'axisdirectcheck/';

    /* Read/Copy File from this folder. */
    $prodMainFolder = '';
    $prodBasePath = '/var/www/html/TestingBackupCron/sijucheckfiles/';


    $dir_iterator = new RecursiveDirectoryIterator($basepath . $frmFoldr);
    $iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST);

foreach ($iterator as $file) {
        $readDir = strstr($file->getPathname(), $frmFoldr);
        $tofileminpath = str_replace($frmFoldr, "", $readDir);

        $fromExPath = $prodBasePath . $tofileminpath;
        $toExPath = $basepath . $toFoldr . $tofileminpath;

        if (strpos($fromExPath, '.php') || strpos($fromExPath, '.js') || strpos($fromExPath, '.css')) {
            if (file_exists($fromExPath)) {
                copy($fromExPath, $toExPath);
                print_r('Files copied to ' . $toExPath . ".\n");
            }else{
                print_r($fromExPath." ::: Read path not found.\n");
            }
        }
    }

上面的代码给出了错误"无法打开流:没有这样的文件或目录"。我认为副本不会创建文件夹。请帮帮我们。

1 个答案:

答案 0 :(得分:0)

找到解决方案。下面的代码首先创建文件夹,然后在下一个循环中将复制的文件写入这些文件夹。

public function run($args) {
    /*Patch Folder */
    $frmFoldr = 'patch_test/';
    $basepath = '/var/www/html/TestingBackupCron/';

    /*The Folder where files are to be backed up it would use basepath and toFoldr folder name. */
    $toFoldr = 'axisdirectcheck/';

    /* Read/Copy File from this folder. */
    $prodMainFolder = '';
    $prodBasePath = '/var/www/html/TestingBackupCron/sijucheckfiles/';


    if (file_exists($basepath . $frmFoldr)) {
        $dir_iterator = new RecursiveDirectoryIterator($basepath . $frmFoldr);
        $iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST);
    } else{
        print_r("Read Directory '$basepath$frmFoldr' not Found. Please check the read directory filename.\n");
        exit;
    }
    $fromDir = array();

    $sendToArray = array();

    /* this foreach would create folders inside the path ($toFoldr) specified */
    foreach ($iterator as $file) {
        $readDir = strstr($file->getPathname(), $frmFoldr);
        $tofileminpath = str_replace($frmFoldr, "", $readDir);

        $fromExPath = $basepath . $readDir;
        $toExPath = $basepath . $toFoldr . $tofileminpath;
        $sendToArray[] = $tofileminpath;

        if (strpos($file, '/.')) {
            if (!file_exists($toExPath)) {
                $oldmask = umask(0);
                mkdir("$toExPath", 0777, true);
                umask($oldmask);
            }
        }
    }

    /* this foreach would copy files from PROD/UAT and paste inside the path($toExPath) specified */
    foreach ($sendToArray as $fPath) {
        $fromExPath = $prodBasePath . $fPath;
        $toExPath = $basepath . $toFoldr . $fPath;

        if (strpos($fromExPath, '.php') || strpos($fromExPath, '.js') || strpos($fromExPath, '.css')) {
            if (file_exists($fromExPath)) {
                copy($fromExPath, $toExPath);
                print_r('Files copied to ' . $toExPath . ".\n");
            }else{
                print_r($fromExPath." ::: Read path not found.\n");
            }
        }
    }
}

谢谢..