Laravel 数据库及项目文件自动备份指北 (spatie/Laravel-backup) 
                                                    
                        
                    
                    
  
                    
                    源码和数据库备份功能
安装:
composer require spatie/laravel-backup
tips:
- 
报错:
Installation failed, reverting ./composer.json to its original content. - 
原因:
spatie/laravel-backup 最新版需要php7.1 - 
解决:
根据不同的PHP版本下载不同版本的spatie/laravel-backup比如我的项目PHP要求大于5.6 而v3.x-dev支持 PHP 5.5 则composer require spatie/laravel-backup v3.x-dev 
发布配置文件 config/backup.php
php artisan vendor:publish --provider="Spatie\Backup\BackupServiceProvider"
出现 config/backup.php 文件说明安装成功
执行  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:run 开始备份
tips:
因为备份时默认会发送邮件通知,所以如果你的项目还没有配置邮件会导致如下报错:
Copying zip failed because: Expected response code 250 but got code "530", with message "530 5.7.1 Authentication required
".
Backup failed because Connection to smtp.mailtrap.io:2525 Timed Out.
#0 /home/vagrant/Code/larabbs/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/AbstractSmtpTransport.php(339): Swift_Transport_AbstractSmtpTransport->getFullResponse(14)
#1 /home/vagrant/Code/larabbs/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/EsmtpTransport.php(305): Swift_Transport_AbstractSmtpTransport->executeCommand('HELO [127.0.0.1...', Array, Array, false, NULL)
.
.
.
#38 /home/vagrant/Code/larabbs/artisan(37): Illuminate\Foundation\Console\Kernel->handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#39 {main}
Backup failed because: Expected response code 250 but got code "530", with message "530 5.7.1 Authentication required
".
In AbstractSmtpTransport.php line 473:
  Connection to smtp.mailtrap.io:2525 Timed Out
In StreamBuffer.php line 166:
  Connection to smtp.mailtrap.io:2525 Timed Out
很简单我们在命令行中再执行一下 php artisan backup:run -h  可以看到提示:
Usage:
  backup:run [options]
Options:
      --filename[=FILENAME]
      --only-db
      --db-name[=DB-NAME]             (multiple values allowed)
      --only-files
      --only-to-disk[=ONLY-TO-DISK]
      --disable-notifications // 在当前命令中忽略邮件提醒
  -h, --help                         Display this help message
  -q, --quiet                        Do not output any message
  -V, --version                      Display this application version
      --ansi                         Force ANSI output
      --no-ansi                      Disable ANSI output
  -n, --no-interaction               Do not ask any interactive question
      --env[=ENV]                    The environment the command should run under
  -v|vv|vvv, --verbose               Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
Help:
  Run the backup.
接着: php artisan backup:run --disable-notifications
如果项目文件比较多的话则需要一会时间来备份,我们可以先只备份数据库试一下:php artisan backup:run --disable-notifications --only-db
备份成功可以在 'storage/app/' 中看到 .zip 后缀的压缩文件,这就是备份文件了。(你可以解压出来看看)
config/laravel-backup.php :
'destination' => [
            /*
             * The disk names on which the backups will be stored.
             */
            'disks' => [
                'local',
            ],
 ],
config/filesystems.php:
'disks' => [
    'local' => [
        'driver' => 'local',
        'root' => storage_path('app'),
    ],
],
从以上可以 看出 备份的.zip文件 在 storge/app/http---localhost/文件中
只备份文件
php artisan backup:run --only-files
只备份数据库
php artisan backup:run --only-db
自动备份(linux自带 定时任务工具crontab 无需安装)
vagrant provision
配置需要的定时任务
./app/Console/Kernel.php:
  protected function schedule(Schedule $schedule)
    {
        // $schedule->command('inspire')
        //          ->hourly();
        $schedule->command('backup:clean')->daily()->at('10:00');
        $schedule->command('backup:run --disable-notifications')->->everyThirtyMinutes();;//每过30分钟备份一次
    }
调度频率设置
当然,你可以为你的任务分配多种调度计划:
| 方法 | 描述 | 
|---|---|
->cron('* * * * * *'); | 
在自定义的 Cron 时间表上执行该任务 | 
->everyMinute(); | 
每分钟执行一次任务 | 
->everyFiveMinutes(); | 
每五分钟执行一次任务 | 
->everyTenMinutes(); | 
每十分钟执行一次任务 | 
->everyFifteenMinutes(); | 
每十五分钟执行一次任务 | 
->everyThirtyMinutes(); | 
每半小时执行一次任务 | 
->hourly(); | 
每小时执行一次任务 | 
->hourlyAt(17); | 
每小时的第 17 分钟执行一次任务 | 
->daily(); | 
每天午夜执行一次任务 | 
->dailyAt('13:00'); | 
每天的 13:00 执行一次任务 | 
->twiceDaily(1, 13); | 
每天的 1:00 和 13:00 分别执行一次任务 | 
->weekly(); | 
每周执行一次任务 | 
->monthly(); | 
每月执行一次任务 | 
->monthlyOn(4, '15:00'); | 
在每个月的第四天的 15:00 执行一次任务 | 
->quarterly(); | 
每季度执行一次任务 | 
->yearly(); | 
每年执行一次任务 | 
->timezone('America/New_York'); | 
设置时区 | 
你也可以直接前往 任务调度文档 复习一下;
hometead 可自动生成cronta定时任务文件:
tips: schedule: true 冒号后面必须空格,否则语法错误
Homestead.yaml:
  sites:
    - map: wangj_task.io
      to: /home/vagrant/code/Task/public
      schedule: true #务必空格!!!
vagrant@homestead:/etc/cron.d$ ls
mdadm php popularity-contest sysstat wangjtaskiovagrant@homestead:/etc/cron.d$ cat wangjtaskio
#每分钟执行一次  artisan schedule:run
  * * * * * vagrant  . /home/vagrant/.profile;
  /usr/bin/php/home/vagrant/code/Task/public/../artisan schedule:run >> /dev/null 2>&1
备份文件默认放在该目录: ./storage/app/ 备份文件格式为 .zip
本作品采用《CC 协议》,转载必须注明作者和本文链接
          
                    
                    

            
            
          
          
                关于 LearnKu
              
                    
                    
                    
 
推荐文章: