如何使用Laravel以编程方式生成表备份? [英] How to generate a table backup programatically with Laravel?

查看:46
本文介绍了如何使用Laravel以编程方式生成表备份?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Laravel中工作,这将截断数据库并重新填充它.但是,我想确保首先备份表,并且不确定如何从同一作业中创建该表的所有行的文件.我尝试使用csv文件(使用LaraCSV)

 公共函数respaldaDevices(){$ model =设备:: get();$ csvExport = new \ Laracsv \ Export();$ campos = ['serial','code','rotulo','model_id','location_id','fecha_recepcion','guia_recepcion','fecha_instalacion','fecha_reversa','guia_reversa','pep','status_id','serial_prev','reversa_prev','str_id','customer_id','technician_id','provider_id','obs'];$ content = $ csvExport-> build($ model,$ campos)-> getWriter()-> getContent();$ date = date('d-m-Y H-i-s');存储:: disk('public')-> put('respaldo-devices-'.$ date.'.csv',$ content);} 

但我希望有插入语句.

到目前为止,我已经尝试过:

已安装的Symfony进程和

 使用Symfony \ Component \ Process \ Process;$ process = new Process(array('mysqldump',sprintf('-u%s',getenv('DB_USERNAME')),'reportes','devices> devices-'.time().'.sql')); 

Mysqldump返回错误6.

使它与不推荐使用的选项一起工作:

  $ process = new Process("mysqldump".sprintf('-u%s',getenv('DB_USERNAME')).".".报告".设备> storage \ app \ devices-".time()..sql --no-create-info");$ process-> run(); 

我想知道如何使用数组来做同样的事情.

解决方案

您可以利用特定于OS的工具,例如过程来调用它..>

流程用法示例:

  $ process = new Process(['mysqldump',sprintf('-u%s','my-username'),sprintf('-p%s','my-password'),'my-database my-table',//'my-table'可以省去整个数据库的备份'--add-drop-database',//可选]);$ process-> run();如果(!$ process-> isSuccessful()){抛出新的\ Exception('无法创建备份');}$ date = date('d-m-Y H-i-s');存储:: disk('public')-> put('respaldo-devices-'.$ date.'.csv',$ process-> getOutput()); 

I wanna make a job in Laravel which will truncate the database and refill it. However, I wanna make sure I have a backup of the table first and I'm not sure how to create a file with all rows from that table from the same job. I tried using a csv file (using LaraCSV)

public function respaldaDevices() {
        $model = Device::get();
        $csvExport = new \Laracsv\Export();
        $campos = ['serial', 'code', 'rotulo', 'model_id', 
        'location_id', 'fecha_recepcion', 'guia_recepcion', 'fecha_instalacion',
        'fecha_reversa', 'guia_reversa', 'pep', 'status_id', 'serial_prev', 
        'reversa_prev', 'str_id', 'customer_id', 'technician_id', 'provider_id', 'obs'];
        $content = $csvExport->build($model,$campos)->getWriter()->getContent();
        $date = date('d-m-Y H-i-s');
        Storage::disk('public')->put('respaldo-devices-' . $date . '.csv', $content);
    }

but I'd rather have the insert statements.

EDIT: What I've tried so far:

Installed Symfony Process and

use Symfony\Component\Process\Process;

$process = new Process(array('mysqldump', sprintf('-u%s', getenv('DB_USERNAME')),'reportes','devices > devices-'.time().'.sql'));

Mysqldump returns an error 6.

EDIT 2:

Made it work with a deprecated option:

$process = new Process(
                    "mysqldump " . sprintf('-u%s', getenv('DB_USERNAME')) . " ".  " " . "reportes " . "devices > storage\app\devices-".time().".sql --no-create-info"
        );
        $process->run();

I'd like to know how to do the same but using the array.

解决方案

You could leverage OS-specific tools like mysqldump instead. You can call this using a process.

Example usage of Process:

$process = new Process([
    'mysqldump',
    sprintf('-u%s', 'my-username'),
    sprintf('-p%s', 'my-password'),
    'my-database my-table', // 'my-table' can be left out for a backup of the entire database
    '--add-drop-database', // optional
]);

$process->run();

if (!$process->isSuccessful()) {
    throw new \Exception('Failed to create backup');
}

$date = date('d-m-Y H-i-s');
Storage::disk('public')->put('respaldo-devices-' . $date . '.csv', $process->getOutput());

这篇关于如何使用Laravel以编程方式生成表备份?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆