Commit c305821b authored by platyhouse's avatar platyhouse

# MySQL 도구 추가 및 ptyMysqlBackup 리팩토링

## 신규 파일 추가

### MySQL 설정 로더 (ptyLibrary_PHP/mysql/ptyMysqlConfig.php)
- MySQL 공통 설정 로더 클래스 추가
- ~/.ptyMysqlConfig.ini 파일 기반 설정 관리
- getSections(), load(), connect() 메서드 제공
- 설정 파일 예시를 반환하는 getConfigExample() 메서드 포함

### MySQL 정보 조회 도구 (ptyMysqlInfo.php)
- MySQL 서버 정보, 상태, 데이터베이스 목록, 사용자 목록 등 조회
- ANSI 색상 코드를 활용한 가독성 높은 출력
- formatBytes(), formatUptime(), padLabel() 유틸리티 함수 포함
- 시스템 DB와 사용자 DB 구분 표시

### MySQL 복원 도구 (ptyMysqlRestore.php)
- SQL 백업 파일을 MySQL로 복원하는 CLI 도구
- --dry-run 옵션으로 실제 실행 전 미리보기 지원
- --force 옵션으로 확인 없이 바로 실행 가능
- 대화형 실행 확인 기능 포함

## 기존 파일 수정

### ptyMysqlBackup.php 리팩토링
- platyFramework 네임스페이스 적용
- ptyCliOptionParser, ptyMysqlConfig 라이브러리 사용으로 전환
- 기존 하드코딩된 설정을 INI 파일 기반으로 변경
- 데이터베이스/테이블 선택적 백업 지원 (* 와일드카드)
- --output 옵션으로 출력 디렉토리 지정 가능
- 백업 파일에 실행 정보 주석 추가
- mysqldump 에러와 stdout 분리 처리
parent acc2c1ef
<?php
/**
* ptyMysqlConfig
*
* MySQL 공통 설정 로더
* 설정 파일: ~/.ptyMysqlConfig.ini
*/
namespace platyFramework;
/**
* MySQL 설정 로더 클래스
*/
class ptyMysqlConfig
{
private static $configPath = null;
/**
* 설정 파일 경로 반환
*/
public static function getConfigPath()
{
if (self::$configPath === null) {
self::$configPath = getenv('HOME') . '/.ptyMysqlConfig.ini';
}
return self::$configPath;
}
/**
* 설정 파일에서 섹션 목록 조회
*/
public static function getSections()
{
$configPath = self::getConfigPath();
if (!file_exists($configPath)) {
return [];
}
$config = parse_ini_file($configPath, true);
return $config ? array_keys($config) : [];
}
/**
* MySQL 설정 로드
*
* @param string $section INI 파일 섹션명 (기본값: default)
* @return array 설정 배열 [host, username, password, database, charset]
* @throws \Exception 설정 파일이나 섹션이 없을 경우
*/
public static function load($section = 'default')
{
$configPath = self::getConfigPath();
if (!file_exists($configPath)) {
throw new \Exception("MySQL 설정 파일을 찾을 수 없습니다: {$configPath}\n\n" . self::getConfigExample());
}
$config = parse_ini_file($configPath, true);
if ($config === false) {
throw new \Exception("MySQL 설정 파일을 파싱할 수 없습니다: {$configPath}");
}
if (!isset($config[$section])) {
$availableSections = implode(', ', array_keys($config));
throw new \Exception("MySQL 설정에서 [{$section}] 섹션을 찾을 수 없습니다.\n사용 가능한 섹션: {$availableSections}");
}
$sectionConfig = $config[$section];
// 필수 필드 검증
if (!isset($sectionConfig['host']) || empty($sectionConfig['host'])) {
throw new \Exception("MySQL 설정 [{$section}] 섹션에 필수 필드 'host'가 없습니다.");
}
return [
'host' => $sectionConfig['host'],
'username' => $sectionConfig['username'] ?? 'root',
'password' => isset($sectionConfig['password']) ? trim($sectionConfig['password'], '"\'') : null,
'database' => $sectionConfig['database'] ?? null,
'charset' => $sectionConfig['charset'] ?? 'utf8mb4',
];
}
/**
* mysqli 연결 생성
*
* @param string $section INI 파일 섹션명 (기본값: default)
* @return array [connection => mysqli, config => array]
* @throws \Exception 연결 실패 시
*/
public static function connect($section = 'default')
{
$config = self::load($section);
$connection = mysqli_connect(
$config['host'],
$config['username'],
$config['password'],
$config['database']
);
if (!$connection) {
throw new \Exception("MySQL 연결 실패: " . mysqli_connect_error());
}
if (!empty($config['charset'])) {
mysqli_set_charset($connection, $config['charset']);
}
return [
'connection' => $connection,
'config' => $config,
];
}
/**
* 설정 파일 예시 반환
*/
public static function getConfigExample()
{
return <<<EOT
설정 파일 예시 (~/.ptyMysqlConfig.ini):
[default]
host=localhost
username=root
password="your_password"
database=your_db
charset=utf8mb4
[production]
host=prod-mysql.example.com
username=admin
password="production_password"
database=prod_db
charset=utf8mb4
EOT;
}
}
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment