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

标签 php ubuntu yii cron backup

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

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

我写的代码是:-

    /*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");
            }
        }
    }

上面的代码给我错误“无法打开流:没有这样的文件或目录”。我认为复制不会创建文件夹。请大家帮忙。

最佳答案

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

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");
            }
        }
    }
}

谢谢..

关于php - 想要备份 PHP 中的代码,其中要作为备份的文件是从另一个指定的(补丁)文件夹中读取的。?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40217319/

相关文章:

php - FOSUserBundle 'Class ' FOS\UserBundle\FOSUserBundle' 在 app/AppKernel.php 中找不到的 Symfony 2.1 配置

linux - Ubuntu 17.10 上不存在 OpenJDK-9 JDK

yii - 使用 CDbCriteria 搜索

php - 如何在 Laravel 中动态创建子域?

php - 如何整合PHP和C程序?

user-interface - 如何在 Ubuntu 中使用 g++ 编译 fltk 程序?

php - Yii 类未找到,命名空间使用

php - 在 yii 模型操作中运行 for 循环创建并保存每条记录,尽管 for 循环结束

PHP 使用命名空间时需要文件

ubuntu - Nginx 重定向 301 非 ssl www 到 ssl www 无法正常工作