feat: restore database from backup and fix date formatting in restore script
This commit is contained in:
91
restore_db.js
Normal file
91
restore_db.js
Normal file
@@ -0,0 +1,91 @@
|
||||
import mysql from 'mysql2/promise';
|
||||
import dotenv from 'dotenv';
|
||||
import fs from 'fs';
|
||||
|
||||
dotenv.config();
|
||||
|
||||
const { DB_HOST, DB_USER, DB_PASS, DB_NAME, DB_PORT } = process.env;
|
||||
|
||||
async function restoreDB() {
|
||||
const connection = await mysql.createConnection({
|
||||
host: DB_HOST,
|
||||
user: DB_USER,
|
||||
password: DB_PASS,
|
||||
database: DB_NAME,
|
||||
port: parseInt(DB_PORT || '3306')
|
||||
});
|
||||
|
||||
console.log('📖 백업 파일 읽는 중...');
|
||||
const rawData = fs.readFileSync('backup_atam_data.json', 'utf8');
|
||||
const data = JSON.parse(rawData);
|
||||
|
||||
const tables = {
|
||||
pc_assets: data.pc_assets || [],
|
||||
server_assets: data.server_assets || [],
|
||||
storage_assets: data.storage_assets || [],
|
||||
equip_assets: data.equip_assets || [],
|
||||
mobile_assets: data.mobile_assets || [],
|
||||
sw_sub_assets: data.sw_sub_assets || [],
|
||||
sw_perm_assets: data.sw_perm_assets || [],
|
||||
cloud_assets: data.cloud_assets || [],
|
||||
sw_users: data.sw_users || [],
|
||||
asset_logs: data.logs || []
|
||||
};
|
||||
|
||||
console.log('🚀 데이터 복구 시작...');
|
||||
|
||||
for (const [tableName, rows] of Object.entries(tables)) {
|
||||
if (rows.length === 0) {
|
||||
console.log(`⏩ ${tableName}: 데이터 없음, 건너뜀`);
|
||||
continue;
|
||||
}
|
||||
|
||||
console.log(`📦 ${tableName} 복구 중 (${rows.length}개)...`);
|
||||
|
||||
// 테이블 컬럼 정보 조회
|
||||
const [columns] = await connection.query(`SHOW COLUMNS FROM ${tableName}`);
|
||||
const validColumns = columns.map(c => c.Field);
|
||||
|
||||
for (const row of rows) {
|
||||
const filteredRow = {};
|
||||
Object.keys(row).forEach(key => {
|
||||
let dbKey = key;
|
||||
|
||||
// 필드명 매핑 보정 (백업 데이터 -> DB 스키마)
|
||||
if (key === 'manager') dbKey = 'manager_main';
|
||||
if (key === 'asset_name' && (tableName === 'mobile_assets' || tableName === 'equip_assets')) dbKey = 'model_name';
|
||||
if (key === 'mac_address' && tableName === 'pc_assets') dbKey = 'remarks'; // 스키마에 없는 경우 비고로
|
||||
|
||||
// created_at 등 날짜 포맷 보정
|
||||
if (validColumns.includes(dbKey)) {
|
||||
let value = row[key];
|
||||
if (dbKey === 'created_at' && value) {
|
||||
// '2026-04-17T08:52:11.000Z' -> '2026-04-17 08:52:11'
|
||||
value = value.replace('T', ' ').replace(/\..*$/, '');
|
||||
}
|
||||
filteredRow[dbKey] = value;
|
||||
}
|
||||
});
|
||||
|
||||
// 필수값 ID 확인
|
||||
if (!filteredRow.id) {
|
||||
filteredRow.id = Math.random().toString(36).substr(2, 9);
|
||||
}
|
||||
|
||||
try {
|
||||
await connection.query(`INSERT INTO ${tableName} SET ?`, filteredRow);
|
||||
} catch (err) {
|
||||
console.error(`❌ [${tableName}] ID ${filteredRow.id} 삽입 실패: ${err.message}`);
|
||||
}
|
||||
}
|
||||
console.log(`✅ ${tableName} 완료`);
|
||||
}
|
||||
|
||||
console.log('✨ 모든 데이터 복구가 완료되었습니다.');
|
||||
await connection.end();
|
||||
}
|
||||
|
||||
restoreDB().catch(err => {
|
||||
console.error('❌ 복구 실패:', err);
|
||||
process.exit(1);
|
||||
});
|
||||
Reference in New Issue
Block a user