Skip to content

Commit

Permalink
Add spatie/laravel-backup package
Browse files Browse the repository at this point in the history
  • Loading branch information
curder committed Feb 2, 2024
1 parent 3087491 commit 4509ac7
Show file tree
Hide file tree
Showing 3 changed files with 208 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/.vitepress/sidebar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ export default {
{text: "Laravel Login Link", link: "/packages/laravel-login-link"},
{text: "Sushi 🍣", link: "/packages/sushi"},
{text: "Livewire Missing Assertions", link: "/packages/missing-livewire-assertions"},
{text: "Laravel Backup", link: "/packages/backup"},
],
}
],
Expand Down
3 changes: 2 additions & 1 deletion docs/packages/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
- [Laravel Pail 日志](pail.md)
- [Laravel pint 代码格式化](pint.md)
- [Laravel Login Link 快速登录](laravel-login-link.md)
- [Livewire 缺少的测试断言](missing-livewire-assertions.md)
- [Livewire 缺少的测试断言](missing-livewire-assertions.md)
- [Laravel Backup 数据备份](backup.md)
205 changes: 205 additions & 0 deletions docs/packages/backup.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
# spatie/laravel-backup

[Laravel Backup](https://github.com/spatie/laravel-backup) 允许创建应用程序的备份,包括数据库、源代码或用户上传的文件。

备份可以存储在配置的自定义文件系统上,比如 S3、七牛云或者 ftp 等。

当备份出现问题时,该扩展包还可以通过邮件等发送通知。

## 安装

通过 Composer 安装该软件包:

```bash
composer require spatie/laravel-backup
```

## 配置

使用下面的命令发布配置文件 `config/backup.php`

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

建议修改配置文件内容:

```php
<?php

'backup' => [
// ...
'name' => env('APP_NAME', 'laravel-backup'), // [!code --]
'name' => env('BACKUP_NAME', 'laravel-backup'), // [!code ++]
'destination' => [
// ...
'disks' => [ // [!code --]
'local', // [!code --]
], // [!code --]
'disks' => explode(',', env('BACKUP_DISKS', 'local')), // [!code ++]
],
],
'monitor_backups' => [
[
'name' => env('APP_NAME', 'laravel-backup'), // [!code --]
'disks' => ['local'], // [!code --]
'name' => env('BACKUP_NAME', 'laravel-backup'), // [!code ++]
'disks' => explode(',', env('BACKUP_DISKS', 'local')), // [!code ++]
'health_checks' => [
\Spatie\Backup\Tasks\Monitor\HealthChecks\MaximumAgeInDays::class => 1,
\Spatie\Backup\Tasks\Monitor\HealthChecks\MaximumStorageInMegabytes::class => 5000,
],
],
],
```

然后可以在 `.env` 配置文件中添加两个自定义环境变量 `BACKUP_NAME` 备份名称和 `BACKUP_DISKS` 需要备份到的磁盘。

## 使用

### 常用命令

- 备份所有文件
```bash
php artisan backup:run
```

- 仅备份数据库
```bash
php artisan backup:run --only-db
```

- 列出所有备份文件
```bash
php artisan backup:list
```

- 清空旧备份文件
```bash
php artisan backup:clean
```

### 通知

默认情况下,备份任务完成后会通过发送通知到 `[email protected]` 用户。

可以通过 `config/backup.php` 文件中的 `config('backup.notifications.mail')` 配置项来修改通知配置。

或者通过 `config/backup.php` 文件中的 `config('backup.notifications.notifications')` 配置项移除 `mail` 来关闭邮件通知。



### 备份到七牛云

默认数据的备份文件放在本地,可以配置一些其他存储方式,比如七牛云存储。

1. 安装驱动依赖
```bash
composer require overtrue/laravel-filesystem-qiniu
```

2. 配置 `config/filesystems.php` 文件
```php
<?php
return [
// ...
'disks' => [
// ...
'qiniu-backup' => [ // [!code ++]
'driver' => 'qiniu', // [!code ++]
'access_key' => env('BACKUP_QINIU_ACCESS_KEY', ''), // [!code ++]
'secret_key' => env('BACKUP_QINIU_SECRET_KEY', ''), // [!code ++]
'bucket' => env('BACKUP_QINIU_BUCKET', ''), // [!code ++]
'domain' => env('BACKUP_QINIU_DOMAIN', ''), // [!code ++]
], // [!code ++]
]
// ...
];
```

3. 修改 `config/backup.php` 文件中关于 `disks` 的配置

```php
<?php
return [
'backup' => [
// ...
'destination' => [
// ...
'disks' => [ // [!code --]
'local', // [!code --]
], // [!code --]
'disks' => explode(',', env('BACKUP_DISKS', 'local')), [ // [!code ++]
],
// ...
],
'monitor_backups' => [
[
// ...
'disks' => ['local'],// [!code --]
'disks' => explode(',', env('BACKUP_DISKS', 'local')), // [!code ++]
// ...
],
// ...
],
];
```
4. 本地 `.env` 文件新增环境变量
```dotenv
# Backup
BACKUP_QINIU_ACCESS_KEY=
BACKUP_QINIU_SECRET_KEY=
BACKUP_QINIU_BUCKET=my-backup
BACKUP_QINIU_DOMAIN=https://backup.domain.com
# support local、qiniu-backup
BACKUP_DISKS=local,qiniu-backup
```
### [备份定时任务](https://spatie.be/docs/laravel-backup/v8/installation-and-setup#content-scheduling)
```php
<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
protected function schedule(Schedule $schedule): void
{
$schedule->command('backup:run --only-db')->daily()->at('02:05'); // 凌晨2点5分备份数据库// [!code ++]
$schedule->command('backup:clean')->daily()->at('02:05'); // 凌晨2点5分清理旧的备份// [!code ++]
$schedule->command('backup:monitor')->daily()->at('10:05'); // 早上10点05分检查数据库备份// [!code ++]
}
// ...
}
```
### [mysql-dump 命令路径](https://spatie.be/docs/laravel-backup/v8/installation-and-setup#content-dumping-the-database)
1. 在 `config/database.php` 中添加自定义配置
```php
<?php
use Illuminate\Support\Str;
return [
'connections' => [
'mysql' => [
// ...
'dump' => [ // [!code ++]
'dump_binary_path' => env('DUMP_BINARY_PATH', '/usr/local/mysql/bin'), // [!code ++]
], // [!code ++]
],
],
];
```
2. 在本地环境文件 `.env` 中添加
```dotenv
# For Apple M1
DUMP_BINARY_PATH=/opt/homebrew/bin
```

0 comments on commit 4509ac7

Please sign in to comment.