Laravel实现备份数据库以及下载和删除备份文件
程序员文章站
2022-04-28 22:34:21
...
话不多说,直接上代码
1 备份,定好路径,这里定义的路径是项目的storage/app/public/backup,然后直接调用下面方法即可
$dirPath = storage_path('app/public/backup/');
if (!file_exists($dirPath)) mkdir($dirPath);
$filePath = $dirPath.date('Y-m-d').'_'.date('H:i:s').'.sql';
$database = sprintf(
'mysqldump -h%s -p%s -u%s -p%s %s > %s',
env('DB_HOST'),
env('DB_PORT'),
env('DB_USERNAME'),
env('DB_PASSWORD'),
env('DB_DATABASE'),
$filePath
);
$process = new Process($database);
$process->mustRun();
2 查询,下载,删除
Storage::disk('public')->allFiles('/backup');
Storage::disk('public')->download('/backup/xxxx.sql');
Storage::disk('public')->delete('/backup/xxxx.sql');
完
下一篇: foreach循环中需要引用时用闭包替代