84 lines
2.6 KiB
JavaScript
84 lines
2.6 KiB
JavaScript
import mysql from 'mysql2/promise';
|
|
import dotenv from 'dotenv';
|
|
import fs from 'fs';
|
|
import path from 'path';
|
|
|
|
dotenv.config();
|
|
|
|
const { DB_HOST, DB_USER, DB_PASS, DB_NAME, DB_PORT } = process.env;
|
|
|
|
async function restoreRealData() {
|
|
const connection = await mysql.createConnection({
|
|
host: DB_HOST,
|
|
user: DB_USER,
|
|
password: DB_PASS,
|
|
database: DB_NAME,
|
|
port: parseInt(DB_PORT || '3306')
|
|
});
|
|
|
|
console.log('📖 realServerData.ts 읽는 중...');
|
|
const filePath = path.join(process.cwd(), 'src/core/realServerData.ts');
|
|
const fileContent = fs.readFileSync(filePath, 'utf8');
|
|
|
|
// TypeScript 파일에서 JSON 배열 부분만 추출
|
|
const jsonMatch = fileContent.match(/\[\s*\{[\s\S]*\}\s*\]/);
|
|
if (!jsonMatch) {
|
|
throw new Error('데이터 형식을 찾을 수 없습니다.');
|
|
}
|
|
const realData = JSON.parse(jsonMatch[0]);
|
|
|
|
console.log(`🚀 ${realData.length}개의 실제 데이터 복구 시작...`);
|
|
|
|
for (const item of realData) {
|
|
const type = item.storage유형;
|
|
let tableName = 'server_assets';
|
|
|
|
// 유형에 따른 테이블 분기
|
|
if (type === 'NAS' || type === '스토리지') {
|
|
tableName = 'storage_assets';
|
|
} else if (type === 'PC' && item.용도.includes('서버')) {
|
|
tableName = 'server_assets'; // 서버 역할을 하는 PC
|
|
} else if (type === 'PC') {
|
|
tableName = 'pc_assets';
|
|
}
|
|
|
|
// DB 스키마 매핑
|
|
const filteredRow = {
|
|
id: Math.random().toString(36).substr(2, 9),
|
|
corp: item.법인 || '',
|
|
asset_code: item.자산코드 || '',
|
|
type: type === 'NAS' ? '스토리지' : (type === 'PC' ? '서버(PC)' : type),
|
|
purpose: item.용도 || '',
|
|
details: item.상세 || '',
|
|
location: item.위치 || '',
|
|
manager_main: item.담당자_정 || '',
|
|
manager_sub: item.담당자_부 || '',
|
|
ip_address: item.IP주소 || '',
|
|
remote_tool: item.원격접속 || '',
|
|
server_id: item.서버ID || '',
|
|
server_pw: item.서버PW || '',
|
|
model_name: item.모델명 || '',
|
|
os: item.OS || '',
|
|
cpu: item.CPU || '',
|
|
ram: item.RAM || '',
|
|
storage1: item.SSD1 || '',
|
|
storage2: item.SSD2 || '',
|
|
remarks: item.IP2 ? `보조IP: ${item.IP2}` : ''
|
|
};
|
|
|
|
try {
|
|
await connection.query(`INSERT INTO ${tableName} SET ?`, filteredRow);
|
|
} catch (err) {
|
|
console.error(`❌ [${tableName}] 삽입 실패 (${item.자산코드}):`, err.message);
|
|
}
|
|
}
|
|
|
|
console.log('✨ 실제 운영 데이터 복구가 완료되었습니다.');
|
|
await connection.end();
|
|
}
|
|
|
|
restoreRealData().catch(err => {
|
|
console.error('❌ 복구 실패:', err);
|
|
process.exit(1);
|
|
});
|