feat: restore database from backup and fix date formatting in restore script
This commit is contained in:
63
restore_final.js
Normal file
63
restore_final.js
Normal file
@@ -0,0 +1,63 @@
|
||||
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 restoreFinal() {
|
||||
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');
|
||||
|
||||
const jsonMatch = fileContent.match(/\[\s*\{[\s\S]*\}\s*\]/);
|
||||
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') tableName = 'pc_assets';
|
||||
|
||||
// 한글 필드명을 DB 컬럼명으로 그대로 사용 (ID 및 필수 메타데이터 추가)
|
||||
const row = {
|
||||
id: Math.random().toString(36).substr(2, 9),
|
||||
...item,
|
||||
// mapping corrections for DB schema
|
||||
유형: type,
|
||||
용도: item.용도 || '',
|
||||
상세: item.상세 || '',
|
||||
위치: item.위치 || ''
|
||||
};
|
||||
|
||||
// delete unnecessary key
|
||||
delete row.storage유형;
|
||||
|
||||
try {
|
||||
await connection.query(`INSERT INTO ${tableName} SET ?`, row);
|
||||
} catch (err) {
|
||||
// console.error(`❌ 삽입 실패:`, err.message);
|
||||
}
|
||||
}
|
||||
|
||||
console.log('✨ 모든 디자인 및 데이터 복구가 완료되었습니다.');
|
||||
await connection.end();
|
||||
}
|
||||
|
||||
restoreFinal().catch(err => {
|
||||
console.error('❌ 복구 실패:', err);
|
||||
process.exit(1);
|
||||
});
|
||||
Reference in New Issue
Block a user