{"id":24825,"date":"2024-11-21T13:30:36","date_gmt":"2024-11-21T05:30:36","guid":{"rendered":"https:\/\/fwq.ai\/blog\/24825\/"},"modified":"2024-11-21T13:30:36","modified_gmt":"2024-11-21T05:30:36","slug":"%e5%a6%82%e4%bd%95%e5%88%a9%e7%94%a8laravel%e5%ae%9e%e7%8e%b0%e6%95%b0%e6%8d%ae%e5%a4%87%e4%bb%bd%e5%92%8c%e6%81%a2%e5%a4%8d%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/24825\/","title":{"rendered":"\u5982\u4f55\u5229\u7528Laravel\u5b9e\u73b0\u6570\u636e\u5907\u4efd\u548c\u6062\u590d\u529f\u80fd"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/000\/164\/169890232534697.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5229\u7528Laravel\u5b9e\u73b0\u6570\u636e\u5907\u4efd\u548c\u6062\u590d\u529f\u80fd\u63d2\u56fe\" alt=\"\u5982\u4f55\u5229\u7528Laravel\u5b9e\u73b0\u6570\u636e\u5907\u4efd\u548c\u6062\u590d\u529f\u80fd\u63d2\u56fe\" \/><\/p>\n<p>\u5982\u4f55\u5229\u7528Laravel\u5b9e\u73b0\u6570\u636e\u5907\u4efd\u548c\u6062\u590d\u529f\u80fd<\/p>\n<p>\u968f\u7740\u4e92\u8054\u7f51\u7684\u53d1\u5c55\uff0c\u6570\u636e\u5907\u4efd\u548c\u6062\u590d\u529f\u80fd\u6210\u4e3a\u4e86\u91cd\u8981\u7684\u9700\u6c42\u3002\u5728web\u5e94\u7528\u4e2d\uff0c\u6570\u636e\u5907\u4efd\u548c\u6062\u590d\u529f\u80fd\u53ef\u4ee5\u4fdd\u8bc1\u6570\u636e\u7684\u5b89\u5168\u6027\u548c\u53ef\u9760\u6027\uff0c\u540c\u65f6\u4e5f\u63d0\u4f9b\u4e86\u4e00\u79cd\u5e94\u5bf9\u7a81\u53d1\u60c5\u51b5\u7684\u5e94\u6025\u624b\u6bb5\u3002Laravel\u4f5c\u4e3a\u4e00\u79cd\u6d41\u884c\u7684PHP\u6846\u67b6\uff0c\u5177\u5907\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u548c\u6570\u636e\u5e93\u64cd\u4f5c\u80fd\u529b\uff0c\u56e0\u6b64\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5b9e\u73b0\u6570\u636e\u5907\u4efd\u548c\u6062\u590d\u529f\u80fd\u3002<\/p>\n<p>\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5229\u7528Laravel\u5b9e\u73b0\u6570\u636e\u5907\u4efd\u548c\u6062\u590d\u529f\u80fd\uff0c\u5e76\u63d0\u4f9b\u5177\u4f53\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<p>\u4e00\u3001\u6570\u636e\u5907\u4efd\u529f\u80fd\u5b9e\u73b0<\/p>\n<p><span>\u70b9\u51fb\u4e0b\u8f7d<\/span>\u201c\u201d\uff1b<\/p>\n<ol>\n<li>\u914d\u7f6e\u6570\u636e\u5e93\u8fde\u63a5<\/li>\n<\/ol>\n<p>\u6253\u5f00\u9879\u76ee\u6839\u76ee\u5f55\u4e0b\u7684.env\u6587\u4ef6\uff0c\u914d\u7f6e\u6570\u636e\u5e93\u8fde\u63a5\u4fe1\u606f\u3002\u4e3b\u8981\u662f\u8bbe\u7f6eDB_CONNECTION\u3001DB_HOST\u3001DB_PORT\u3001DB_DATABASE\u3001DB_USERNAME\u548cDB_PASSWORD\u7b49\u53c2\u6570\u3002<\/p>\n<ol>\n<li>\u521b\u5efa\u5907\u4efd\u6a21\u578b<\/li>\n<\/ol>\n<p>\u5728app\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3aBackup.php\u7684\u6a21\u578b\u6587\u4ef6\u3002\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>&lt;?php namespace App;\n\nuse IlluminateDatabaseEloquentModel;\n\nclass Backup extends Model\n{\n    protected $table = 'backups';\n    protected $fillable = ['name', 'path'];\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8be5\u6a21\u578b\u5bf9\u5e94\u5907\u4efd\u6570\u636e\u7684\u8868\uff0c\u8868\u540d\u4e3abackups\uff0c\u5305\u542b\u4e24\u4e2a\u5b57\u6bb5name\u548cpath\uff0c\u5206\u522b\u7528\u4e8e\u5b58\u50a8\u5907\u4efd\u7684\u6587\u4ef6\u540d\u548c\u6587\u4ef6\u8def\u5f84\u3002<\/p>\n<ol>\n<li>\u521b\u5efa\u5907\u4efd\u547d\u4ee4<\/li>\n<\/ol>\n<p>\u5728Laravel\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u81ea\u5b9a\u4e49\u547d\u4ee4\u6765\u5b9e\u73b0\u6570\u636e\u5907\u4efd\u529f\u80fd\u3002\u9996\u5148\uff0c\u5728\u547d\u4ee4\u884c\u4e2d\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\uff0c\u751f\u6210\u4e00\u4e2a\u5907\u4efd\u547d\u4ee4\uff1a<\/p>\n<pre>php artisan make:command BackupCommand<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u7136\u540e\uff0c\u7f16\u8f91\u751f\u6210\u7684app\/Console\/Commands\/BackupCommand.php\u6587\u4ef6\uff0c\u7f16\u5199\u5907\u4efd\u903b\u8f91\u3002\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>&lt;?php namespace AppConsoleCommands;\n\nuse IlluminateConsoleCommand;\nuse AppBackup;\n\nclass BackupCommand extends Command\n{\n    protected $signature = 'backup:run';\n    protected $description = 'Run database backup';\n\n    public function __construct()\n    {\n        parent::__construct();\n    }\n\n    public function handle()\n    {\n        $name = 'backup_' . date('Y-m-d_H-i-s') . '.sql';\n        $path = storage_path('app\/backup\/' . $name);\n\n        $command = sprintf('mysqldump -u%s -p%s %s &gt; %s',\n            env('DB_USERNAME'),\n            env('DB_PASSWORD'),\n            env('DB_DATABASE'),\n            $path\n        );\n\n        exec($command);\n\n        Backup::create(['name' =&amp;gt; $name, 'path' =&amp;gt; $path]);\n\n        $this-&amp;gt;info('Database backup success!');\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0cbackup:run\u662f\u547d\u4ee4\u7684\u540d\u79f0\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u81ea\u5b9a\u4e49\u3002$name\u548c$path\u5206\u522b\u7528\u4e8e\u751f\u6210\u6587\u4ef6\u540d\u548c\u6587\u4ef6\u8def\u5f84\u3002mysqldump\u547d\u4ee4\u7528\u4e8e\u5907\u4efd\u6570\u636e\u5e93\uff0c\u5c06\u5907\u4efd\u6570\u636e\u4fdd\u5b58\u5230\u6307\u5b9a\u8def\u5f84\u3002Backup::create()\u65b9\u6cd5\u7528\u4e8e\u521b\u5efa\u5907\u4efd\u8bb0\u5f55\uff0c\u5c06\u5907\u4efd\u4fe1\u606f\u5b58\u50a8\u5230\u6570\u636e\u5e93\u4e2d\u3002<\/p>\n<ol>\n<li>\u6ce8\u518c\u5907\u4efd\u547d\u4ee4<\/li>\n<\/ol>\n<p>\u6253\u5f00app\/Console\/Kernel.php\u6587\u4ef6\uff0c\u5728$commands\u6570\u7ec4\u4e2d\u6dfb\u52a0\u5907\u4efd\u547d\u4ee4\u3002\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>protected $commands = [\n    AppConsoleCommandsBackupCommand::class,\n];<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li>\u8fd0\u884c\u5907\u4efd\u547d\u4ee4<\/li>\n<\/ol>\n<p>\u5728\u547d\u4ee4\u884c\u4e2d\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\uff0c\u8fd0\u884c\u5907\u4efd\u547d\u4ee4\uff1a<\/p>\n<pre>php artisan backup:run<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5907\u4efd\u6210\u529f\u540e\uff0c\u4f1a\u5728storage\/app\/backup\u76ee\u5f55\u4e0b\u751f\u6210\u4e00\u4e2a\u4ee5\u5f53\u524d\u65e5\u671f\u547d\u540d\u7684\u5907\u4efd\u6587\u4ef6\u3002<\/p>\n<p>\u4e8c\u3001\u6570\u636e\u6062\u590d\u529f\u80fd\u5b9e\u73b0<\/p>\n<ol>\n<li>\u521b\u5efa\u6062\u590d\u547d\u4ee4<\/li>\n<\/ol>\n<p>\u5728\u547d\u4ee4\u884c\u4e2d\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\uff0c\u751f\u6210\u4e00\u4e2a\u6062\u590d\u547d\u4ee4\uff1a<\/p>\n<pre>php artisan make:command RestoreCommand<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u7136\u540e\uff0c\u7f16\u8f91\u751f\u6210\u7684app\/Console\/Commands\/RestoreCommand.php\u6587\u4ef6\uff0c\u7f16\u5199\u6062\u590d\u903b\u8f91\u3002\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>&lt;?php namespace AppConsoleCommands;\n\nuse IlluminateConsoleCommand;\nuse AppBackup;\n\nclass RestoreCommand extends Command\n{\n    protected $signature = 'restore:run';\n    protected $description = 'Run database restore';\n\n    public function __construct()\n    {\n        parent::__construct();\n    }\n\n    public function handle()\n    {\n        $backup = Backup::latest()-&gt;first();\n\n        if ($backup) {\n            $command = sprintf('mysql -u%s -p%s %s path\n            );\n\n            exec($command);\n\n            $this-&amp;gt;info('Database restore success!');\n        } else {\n            $this-&amp;gt;error('No backup available!');\n        }\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0crestore:run\u662f\u547d\u4ee4\u7684\u540d\u79f0\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u81ea\u5b9a\u4e49\u3002Backup::latest()-&gt;first()\u65b9\u6cd5\u7528\u4e8e\u83b7\u53d6\u6700\u65b0\u7684\u5907\u4efd\u8bb0\u5f55\u3002<\/p>\n<ol>\n<li>\u6ce8\u518c\u6062\u590d\u547d\u4ee4<\/li>\n<\/ol>\n<p>\u6253\u5f00app\/Console\/Kernel.php\u6587\u4ef6\uff0c\u5728$commands\u6570\u7ec4\u4e2d\u6dfb\u52a0\u6062\u590d\u547d\u4ee4\u3002\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>protected $commands = [\n    AppConsoleCommandsBackupCommand::class,\n    AppConsoleCommandsRestoreCommand::class,\n];<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li>\u8fd0\u884c\u6062\u590d\u547d\u4ee4<\/li>\n<\/ol>\n<p>\u5728\u547d\u4ee4\u884c\u4e2d\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\uff0c\u8fd0\u884c\u6062\u590d\u547d\u4ee4\uff1a<\/p>\n<pre>php artisan restore:run<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6062\u590d\u6210\u529f\u540e\uff0c\u6570\u636e\u5e93\u4e2d\u7684\u6570\u636e\u5c06\u4f1a\u88ab\u6700\u65b0\u7684\u5907\u4efd\u6570\u636e\u66ff\u6362\u3002<\/p>\n<p>\u603b\u7ed3\uff1a<\/p>\n<p>\u672c\u6587\u4ecb\u7ecd\u4e86\u5982\u4f55\u5229\u7528Laravel\u5b9e\u73b0\u6570\u636e\u5907\u4efd\u548c\u6062\u590d\u529f\u80fd\u3002\u901a\u8fc7\u81ea\u5b9a\u4e49\u547d\u4ee4\u3001\u6570\u636e\u5e93\u64cd\u4f5c\u548c\u6587\u4ef6\u64cd\u4f5c\u7b49\u6280\u672f\u624b\u6bb5\uff0c\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5b9e\u73b0\u6570\u636e\u5907\u4efd\u548c\u6062\u590d\u529f\u80fd\u3002\u5f00\u53d1\u8005\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\uff0c\u5bf9\u5907\u4efd\u548c\u6062\u590d\u903b\u8f91\u8fdb\u884c\u6269\u5c55\u548c\u4f18\u5316\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u5927\u5bb6\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5229\u7528Laravel\u5b9e\u73b0\u6570\u636e\u5907\u4efd\u548c\u6062\u590d\u529f\u80fd\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5229\u7528Laravel\u5b9e\u73b0\u6570\u636e\u5907\u4efd\u548c\u6062\u590d\u529f\u80fd \u968f\u7740\u4e92\u8054\u7f51\u7684\u53d1\u5c55\uff0c\u6570\u636e\u5907\u4efd\u548c\u6062\u590d\u529f\u80fd\u6210\u4e3a\u4e86\u91cd\u8981\u7684\u9700\u6c42\u3002\u5728web\u5e94\u7528\u4e2d\uff0c\u6570\u636e\u5907\u4efd\u548c\u6062\u590d\u529f\u80fd\u53ef\u4ee5\u4fdd\u8bc1\u6570\u636e\u7684\u5b89\u5168\u6027\u548c\u53ef\u9760\u6027\uff0c\u540c\u65f6\u4e5f\u63d0\u4f9b\u4e86\u4e00\u79cd\u5e94\u5bf9\u7a81\u53d1\u60c5\u51b5\u7684\u5e94\u6025\u624b\u6bb5\u3002Laravel\u4f5c\u4e3a\u4e00\u79cd\u6d41\u884c\u7684PHP\u6846\u67b6\uff0c\u5177\u5907\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u548c\u6570\u636e\u5e93\u64cd\u4f5c\u80fd\u529b\uff0c\u56e0\u6b64\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5b9e\u73b0\u6570\u636e\u5907\u4efd\u548c\u6062\u590d\u529f\u80fd\u3002 \u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5229\u7528Laravel\u5b9e\u73b0\u6570\u636e\u5907\u4efd\u548c\u6062\u590d\u529f\u80fd\uff0c\u5e76\u63d0\u4f9b\u5177\u4f53\u4ee3\u7801\u793a\u4f8b\u3002 \u4e00\u3001\u6570\u636e\u5907\u4efd\u529f\u80fd\u5b9e\u73b0 \u70b9\u51fb\u4e0b\u8f7d\u201c\u201d\uff1b \u914d\u7f6e\u6570\u636e\u5e93\u8fde\u63a5 \u6253\u5f00\u9879\u76ee\u6839\u76ee\u5f55\u4e0b\u7684.env\u6587\u4ef6\uff0c\u914d\u7f6e\u6570\u636e\u5e93\u8fde\u63a5\u4fe1\u606f\u3002\u4e3b\u8981\u662f\u8bbe\u7f6eDB_CONNECTION\u3001DB_HOST\u3001DB_PORT\u3001DB_DATABASE\u3001DB_USERNAME\u548cDB_PASSWORD\u7b49\u53c2\u6570\u3002 \u521b\u5efa\u5907\u4efd\u6a21\u578b \u5728app\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3aBackup.php\u7684\u6a21\u578b\u6587\u4ef6\u3002\u4ee3\u7801\u5982\u4e0b\uff1a &lt;?php namespace App; use IlluminateDatabaseEloquentModel; class Backup extends Model { protected $table = &#8216;backups&#8217;; protected $fillable = [&#8216;name&#8217;, &#8216;path&#8217;]; } \u767b\u5f55\u540e\u590d\u5236 \u8be5\u6a21\u578b\u5bf9\u5e94\u5907\u4efd\u6570\u636e\u7684\u8868\uff0c\u8868\u540d\u4e3abackups\uff0c\u5305\u542b\u4e24\u4e2a\u5b57\u6bb5name\u548cpath\uff0c\u5206\u522b\u7528\u4e8e\u5b58\u50a8\u5907\u4efd\u7684\u6587\u4ef6\u540d\u548c\u6587\u4ef6\u8def\u5f84\u3002 \u521b\u5efa\u5907\u4efd\u547d\u4ee4 \u5728Laravel\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u81ea\u5b9a\u4e49\u547d\u4ee4\u6765\u5b9e\u73b0\u6570\u636e\u5907\u4efd\u529f\u80fd\u3002\u9996\u5148\uff0c\u5728\u547d\u4ee4\u884c\u4e2d\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\uff0c\u751f\u6210\u4e00\u4e2a\u5907\u4efd\u547d\u4ee4\uff1a php artisan make:command BackupCommand \u767b\u5f55\u540e\u590d\u5236 \u7136\u540e\uff0c\u7f16\u8f91\u751f\u6210\u7684app\/Console\/Commands\/BackupCommand.php\u6587\u4ef6\uff0c\u7f16\u5199\u5907\u4efd\u903b\u8f91\u3002\u4ee3\u7801\u5982\u4e0b\uff1a &lt;?php namespace AppConsoleCommands; use IlluminateConsoleCommand; use AppBackup; class BackupCommand extends Command { protected $signature = &#8216;backup:run&#8217;; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-24825","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24825","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=24825"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24825\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=24825"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=24825"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=24825"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}