記事
0 comment

Laravel Backupを使う

Laravel Backup
https://docs.spatie.be/laravel-backup/v4/introduction
ファイルとDBのダンプを保存してくれる

1. Laravelのインストール

$ laravel new DbBackupSample

2. Laravel Backupのインストール

$ composer require spatie/laravel-backup

設定の記述

// config/app.php

'providers' => [
    // ...
    // spatie/laravel-backup
    Spatie\Backup\BackupServiceProvider::class,
];

設定ファイルの生成(config/laravel-backup.phpにファイルが生成される)

$ php artisan vendor:publish --provider="Spatie\Backup\BackupServiceProvider"

3. コマンドの実行と確認

コマンドの一覧

$ php artisan
// ...
 backup
  backup:clean         Remove all backups older than specified number of days in config.
  backup:list          Display a list of all backups.
  backup:monitor       Monitor the health of all backups.
  backup:run           Run the backup.
  // ...

バックアップのリスト

$ php artisan backup:list
+------------------+-------+-----------+---------+--------------+--------------------+--------------+
| Name             | Disk  | Reachable | Healthy | # of backups | Youngest backup    | Used storage |
+------------------+-------+-----------+---------+--------------+--------------------+--------------+
| http---localhost | local | ✅         | ❌       | 0            | No backups present | 0 KB         |
+------------------+-------+-----------+---------+--------------+--------------------+--------------+

バックアップの作成

$ php artisan backup:run 
Starting backup...
Dumping database DbBackupSample...
Determining files to backup...
Zipped 80 files...
Copying 2017-08-11-084251.zip (size: 238.35 KB) to disk named local...
Successfully copied .zip file to disk named local.
Backup completed!

バックアップのリスト

$ php artisan backup:list
+------------------+-------+-----------+---------+--------------+----------------------+--------------+
| Name             | Disk  | Reachable | Healthy | # of backups | Youngest backup      | Used storage |
+------------------+-------+-----------+---------+--------------+----------------------+--------------+
| http---localhost | local | ✅         | ✅       | 1            | 0.00 (7 seconds ago) | 238.35 KB    |
+------------------+-------+-----------+---------+--------------+----------------------+--------------+

4. S3へバックアップ

League\Flysystem\AwsS3v3のインストール
https://github.com/thephpleague/flysystem-aws-s3-v3

$ composer require league/flysystem-aws-s3-v3

AWSにbackup用のバケットを作成後、
.envに追記

AWS_KEY=xxxxx
AWS_SECRET=xxxxx
AWS_REGION=ap-northeast-1
AWS_BUCKET=dbbackupsample

config/filesystems.phpを編集

's3bkup' => [
            'driver' => 's3',
            'key' => env('AWS_KEY'),
            'secret' => env('AWS_SECRET'),
            'region' => env('AWS_REGION'),
            'bucket' => env('AWS_BUCKET'),
        ],

config/laravel-backup.phpを編集

'destination' => [

            /*
             * The disk names on which the backups will be stored.
             */
            'disks' => [
                's3bkup',
            ],
        ],

バックアップの実行

$ php artisan backup:run                     
Starting backup...
Dumping database DbBackupSample...
Determining files to backup...
Zipped 80 files...
Copying 2017-08-11-092403.zip (size: 240.46 KB) to disk named s3bkup...
Successfully copied .zip file to disk named s3bkup.
Backup completed!

5. スケジューリングの追加

app/Console/Kernel.phpに追加

protected function schedule(Schedule $schedule)
    {
        // $schedule->command('inspire')
        //          ->hourly();
        $schedule->command('backup:clean')->daily()->at('01:00');
        $schedule->command('backup:run')->daily()->at('02:00');
    }

コメントを残す

Required fields are marked *.