Tạo scripts backup dữ liệu
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
const mongoose = require('mongoose');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const AdmZip = require('adm-zip');
|
||||
const connectDB = require('../config/db');
|
||||
const User = require('../models/User');
|
||||
const Asset = require('../models/Asset');
|
||||
const Scene = require('../models/Scene');
|
||||
const Hotspot = require('../models/Hotspot');
|
||||
const Setting = require('../models/Setting');
|
||||
|
||||
const backup = async () => {
|
||||
await connectDB();
|
||||
const zip = new AdmZip();
|
||||
const uploadDir = path.join(__dirname, '../uploads');
|
||||
const backupPath = path.join(__dirname, `../backups/backup_${Date.now()}.zip`);
|
||||
|
||||
if (!fs.existsSync(path.join(__dirname, '../backups'))) fs.mkdirSync(path.join(__dirname, '../backups'));
|
||||
|
||||
console.log('Exporting Database...');
|
||||
const dbData = {
|
||||
users: await User.find().lean(),
|
||||
assets: await Asset.find().lean(),
|
||||
scenes: await Scene.find().lean(),
|
||||
hotspots: await Hotspot.find().lean(),
|
||||
settings: await Setting.find().lean()
|
||||
};
|
||||
zip.addFile("database.json", Buffer.from(JSON.stringify(dbData, null, 2), "utf8"));
|
||||
|
||||
if (fs.existsSync(uploadDir)) {
|
||||
console.log('Adding Uploads...');
|
||||
zip.addLocalFolder(uploadDir, "uploads");
|
||||
}
|
||||
|
||||
zip.writeZip(backupPath);
|
||||
console.log(`Backup completed: ${backupPath}`);
|
||||
mongoose.connection.close();
|
||||
};
|
||||
|
||||
backup();
|
||||
@@ -0,0 +1,42 @@
|
||||
const mongoose = require('mongoose');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const AdmZip = require('adm-zip');
|
||||
const connectDB = require('../config/db');
|
||||
const User = require('../models/User');
|
||||
const Asset = require('../models/Asset');
|
||||
const Scene = require('../models/Scene');
|
||||
const Hotspot = require('../models/Hotspot');
|
||||
const Setting = require('../models/Setting');
|
||||
|
||||
const restore = async () => {
|
||||
const zipPath = process.argv[2];
|
||||
if (!zipPath) return console.error('Please provide zip file path: node restoreData.js <path>');
|
||||
|
||||
await connectDB();
|
||||
const zip = new AdmZip(zipPath);
|
||||
const dbEntry = zip.getEntry("database.json");
|
||||
const uploadDir = path.join(__dirname, '../uploads');
|
||||
|
||||
console.log('Restoring Database...');
|
||||
const dbData = JSON.parse(dbEntry.getData().toString('utf8'));
|
||||
|
||||
await Promise.all([
|
||||
User.deleteMany({}), Asset.deleteMany({}), Scene.deleteMany({}),
|
||||
Hotspot.deleteMany({}), Setting.deleteMany({})
|
||||
]);
|
||||
|
||||
await Promise.all([
|
||||
User.insertMany(dbData.users), Asset.insertMany(dbData.assets),
|
||||
Scene.insertMany(dbData.scenes), Hotspot.insertMany(dbData.hotspots),
|
||||
Setting.insertMany(dbData.settings)
|
||||
]);
|
||||
|
||||
console.log('Restoring Files...');
|
||||
zip.extractEntryTo("uploads/", uploadDir, false, true);
|
||||
|
||||
console.log('Restore completed successfully!');
|
||||
mongoose.connection.close();
|
||||
};
|
||||
|
||||
restore();
|
||||
Reference in New Issue
Block a user