feat: 完善安装部署与数字溯源码
This commit is contained in:
@@ -14,6 +14,7 @@ import { buildOrderBy } from '../common/utils/sort.util';
|
||||
import { AuthUser } from '../common/decorators/current-user.decorator';
|
||||
import { QueryBackupDto } from './dto/backup.dto';
|
||||
import { SettingsService } from '../settings/settings.service';
|
||||
import { StorageService } from '../storage/storage.service';
|
||||
|
||||
const MODELS = [
|
||||
'sysUser',
|
||||
@@ -26,6 +27,7 @@ const MODELS = [
|
||||
'traceCodeGroup',
|
||||
'batch',
|
||||
'traceCode',
|
||||
'traceCodeDailySequence',
|
||||
'blockchainProof',
|
||||
'queryLog',
|
||||
'job',
|
||||
@@ -48,6 +50,7 @@ const PRISMA_MODEL_BY_DELEGATE: Record<(typeof MODELS)[number], string> = {
|
||||
traceCodeGroup: 'TraceCodeGroup',
|
||||
batch: 'Batch',
|
||||
traceCode: 'TraceCode',
|
||||
traceCodeDailySequence: 'TraceCodeDailySequence',
|
||||
blockchainProof: 'BlockchainProof',
|
||||
queryLog: 'QueryLog',
|
||||
job: 'Job',
|
||||
@@ -112,6 +115,7 @@ export class BackupService {
|
||||
private readonly prisma: PrismaService,
|
||||
private readonly config: ConfigService,
|
||||
private readonly settings: SettingsService,
|
||||
private readonly storage: StorageService,
|
||||
) {
|
||||
this.storageRoot = resolve(this.config.get<string>('storage.localDir') || './storage');
|
||||
this.backupRoot = resolve(process.env.BACKUP_DIR || join(dirname(this.storageRoot), 'backups'));
|
||||
@@ -141,7 +145,7 @@ export class BackupService {
|
||||
try {
|
||||
const db = await this.dumpDatabase();
|
||||
const zip = new AdmZip();
|
||||
const fileList = this.addStorageFiles(zip);
|
||||
const fileList = await this.addStorageFiles(zip);
|
||||
const manifest = {
|
||||
app: 'artveris',
|
||||
version: '0.1.0',
|
||||
@@ -221,7 +225,7 @@ export class BackupService {
|
||||
try {
|
||||
const zip = new AdmZip(readFileSync(record.filePath));
|
||||
await this.restoreDatabase(zip);
|
||||
this.restoreStorage(zip);
|
||||
await this.restoreStorage(zip);
|
||||
return this.prisma.backupRecord.update({
|
||||
where: { id: restoreId },
|
||||
data: { status: BackupStatus.SUCCESS, error: null },
|
||||
@@ -256,7 +260,7 @@ export class BackupService {
|
||||
private async dumpEffectiveSettings() {
|
||||
const rows = await this.prisma.systemSetting.findMany();
|
||||
const byKey = new Map(rows.map((row) => [row.key, row]));
|
||||
const all = await this.settings.getAll();
|
||||
const all = await this.settings.getAllRaw();
|
||||
let nextId = rows.reduce((max, row) => Math.max(max, row.id), 0) + 1;
|
||||
const now = new Date();
|
||||
for (const [key, value] of Object.entries(all)) {
|
||||
@@ -267,8 +271,17 @@ export class BackupService {
|
||||
return [...byKey.values()];
|
||||
}
|
||||
|
||||
private addStorageFiles(zip: AdmZip) {
|
||||
private async addStorageFiles(zip: AdmZip) {
|
||||
const files: string[] = [];
|
||||
const storageKeys = await this.storage.listKeys();
|
||||
if (storageKeys.length) {
|
||||
for (const key of storageKeys) {
|
||||
if (!isSafeZipPath(key)) continue;
|
||||
files.push(key);
|
||||
zip.addFile(`storage/${key}`, await this.storage.read(key));
|
||||
}
|
||||
return files;
|
||||
}
|
||||
if (!existsSync(this.storageRoot)) return files;
|
||||
const walk = (dir: string) => {
|
||||
for (const name of readdirSync(dir)) {
|
||||
@@ -304,20 +317,25 @@ export class BackupService {
|
||||
}, { timeout: 120000 });
|
||||
}
|
||||
|
||||
private restoreStorage(zip: AdmZip) {
|
||||
mkdirSync(this.storageRoot, { recursive: true });
|
||||
for (const name of readdirSync(this.storageRoot)) {
|
||||
const full = join(this.storageRoot, name);
|
||||
if (isSameOrChildPath(this.backupRoot, full)) continue;
|
||||
rmSync(full, { recursive: true, force: true });
|
||||
private async restoreStorage(zip: AdmZip) {
|
||||
const currentKeys = await this.storage.listKeys();
|
||||
if (currentKeys.length) {
|
||||
for (const key of currentKeys) {
|
||||
if (!key.startsWith('.health/')) await this.storage.remove(key);
|
||||
}
|
||||
} else {
|
||||
mkdirSync(this.storageRoot, { recursive: true });
|
||||
for (const name of readdirSync(this.storageRoot)) {
|
||||
const full = join(this.storageRoot, name);
|
||||
if (isSameOrChildPath(this.backupRoot, full)) continue;
|
||||
rmSync(full, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
for (const entry of zip.getEntries()) {
|
||||
if (entry.isDirectory || !entry.entryName.startsWith('storage/')) continue;
|
||||
const rel = entry.entryName.slice('storage/'.length);
|
||||
if (!isSafeZipPath(rel)) throw new Error(`备份包包含不安全文件路径:${entry.entryName}`);
|
||||
const full = join(this.storageRoot, rel);
|
||||
mkdirSync(dirname(full), { recursive: true });
|
||||
writeFileSync(full, entry.getData());
|
||||
await this.storage.putKey(rel, entry.getData());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user