数据库备份操作

/**
     * 数据表备份
     * @param $form
     * @return array|int
     */
    public function backUpTable($form)
    {
        function_exists('set_time_limit') && set_time_limit(0);
        $s_time = time();
        $tablePath = base_path('database/backup');
        if (!is_dir($tablePath)) {
            mkdir($tablePath);
        }
        $savaFilePath = $tablePath . DIRECTORY_SEPARATOR . date('Y_m_d') . '_create_table_' . $form['name'] . '.sql';
        $content = '';
        if ($form['form'] === 'table') {
            $content = $this->createTable($form['name']);
        }
        if ($form['form'] === 'source') {
            $content = $this->sourceTable($form['name']);
        }
        if ($form['form'] === 'all') {
            $content = $this->createTable($form['name']) . $this->sourceTable($form['name']);
        }
        $result = writeFile($savaFilePath, $content);
        $form['time'] = time() - $s_time;
        return !empty($result['code']) ? $result : ['code' => Code::SUCCESS, 'message' => 'backup table successfully', 'lists' => $form];
    }

    /**
     * 创建数据表SQL
     * @param string $tableName
     * @return string
     */
    protected function createTable(string $tableName): string
    {
        $result = DB::select(sprintf('SHOW CREATE TABLE %s', $tableName));
        $sql = "-- ----------------------------\n-- " . date('Y-m-d H:i:s') . " backup table start\n-- ----------------------------\n";
        foreach ($result as $item) {
            $item = (array)$item;
            $sql .= "-- ----------------------------\n-- Table structure for {$item['Table']}\n-- ----------------------------\n";
            $sql .= sprintf("DROP TABLE IF EXISTS `%s`", $item["Table"]) . ';';
            $sql .= "\n" . $item['Create Table'];
            $sql .= ";\n -- ";
        }
        return $sql;
    }

    /**
     * 数据表数据SQL
     * @param string $tableName
     * @return string
     */
    protected function sourceTable(string $tableName): string
    {
        $result = DB::select(sprintf('SELECT * FROM %s', $tableName));
        $sql = ";\n-- ----------------------------\n-- Records of $tableName\n-- ----------------------------\n";
        if (empty($result)) {
            $sql .= "-- ----------------------------\n-- " . date('Y-m-d H:i:s') . " backup table end\n-- ----------------------------\n";
            return $sql;
        }
        foreach ($result as $item) {
            $sql .= sprintf('INSERT INTO %s VALUES %s', $tableName, '(');
            foreach ($item as $rows) {
                $sql .= "'$rows',";
            }
            $sql .= ');';
            //删除最后三个字符串
            $sql = substr($sql, 0, strlen($sql) - 3);
            $sql .= ");\n";
        }
        $sql .= "-- ----------------------------\n-- " . date('Y-m-d H:i:s') . " backup table end\n-- ----------------------------\n";
        return rtrim($sql, ',');
    }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值