Laravel数据库备份定时任务代码示例(打包到ZIP)


原文地址:Laravel数据库备份定时任务代码示例(打包到ZIP)

本篇文章分享Laravel数据库备份定时任务示例代码,并且自动打包到ZIP。

根据需求可自行修改,Laravel数据库备份自动打包ZIP文件。

<?php

namespace App\Console\Commands;

use Chumper\Zipper\Zipper;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use PhpParser\Node\Scalar\MagicConst\Dir;
use Storage;

class BackupData extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'backupData';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '数据备份命令';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    const DIR_NAME = 'database_backup';

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //
        $dirName = self::DIR_NAME . DIRECTORY_SEPARATOR . date('Y-m-d');

        //今天的已经存在,则删除里面的备份数据
        if(is_dir(storage_path('app'. DIRECTORY_SEPARATOR . $dirName))){
            Storage::deleteDirectory($dirName);
        }
        //重新创建目录
        Storage::makeDirectory($dirName);

        $mysqlDumpBin = config('database.mysqldump');

        if(!is_executable($mysqlDumpBin)){
            logger("数据备份:mysqldump不存在");
            echo "mysqldump不存在\n";

            return 0;
        }

        $dbUsername = config('database.connections.mysql.username');
        $dbPassword = config('database.connections.mysql.password');
        $dbHost = config('database.connections.mysql.host');
        $dbPort = config('database.connections.mysql.port');
        $db = config('database.connections.mysql.database');

        // 获取tables
        $tables = DB::select('show tables');
        $key = 'Tables_in_' . $db;
        $tableArray = [];
        foreach ($tables as $table) {
            $tableArray[] = $table->$key;
        }
        //-c 完整带表字段的
        $dbInfo = "-u$dbUsername -h$dbHost -c -P$dbPort -p$dbPassword";

        // 导出sql
        foreach ($tableArray as $tableIndex => $theTable) {
            $command = "$mysqlDumpBin $dbInfo -t $db $theTable > "
                . storage_path('app' . DIRECTORY_SEPARATOR . $dirName) . "/$theTable.sql";
            shell_exec($command);
        }

        // sql打包zip
        $Zipper = new Zipper();
        $files = glob(storage_path('app' . DIRECTORY_SEPARATOR . $dirName) . DIRECTORY_SEPARATOR . '*');
        $Zipper->make(storage_path('app' . DIRECTORY_SEPARATOR . self::DIR_NAME) . DIRECTORY_SEPARATOR . date("Y-m-d_H-i-s") . '.sql.zip')
            ->add($files)
            ->close();

        //清理数据
        Storage::deleteDirectory($dirName);

        //清理30天前的打包数据
        $files = Storage::files(self::DIR_NAME);

        foreach ($files as $v) {
            $filename = basename($v);
            $date = explode('_', $filename)[0];
            if (strtotime($date) < strtotime('-30 days')){
                Storage::delete($v);
            }
        }

        return '已完成备份';
    }
}

评论

登录后评论

服务器优惠活动

Top