49 lines
1.4 KiB
TypeScript
49 lines
1.4 KiB
TypeScript
import { exec } from 'child_process'
|
|
import path from 'path'
|
|
import { promisify } from 'util'
|
|
|
|
const execAsync = promisify(exec)
|
|
|
|
export async function createDatabaseBackup(): Promise<string> {
|
|
const dbPath = path.join(process.cwd(), 'database', 'antihoax.db')
|
|
const backupPath = path.join(process.cwd(), 'backups', `backup_${Date.now()}.db`)
|
|
|
|
try {
|
|
// Ensure backup directory exists
|
|
await execAsync('mkdir -p backups')
|
|
|
|
// Create SQLite backup using .backup command
|
|
await execAsync(`sqlite3 "${dbPath}" ".backup '${backupPath}'"`)
|
|
|
|
return backupPath
|
|
} catch (error) {
|
|
console.error('Backup failed:', error)
|
|
throw new Error('Database backup failed')
|
|
}
|
|
}
|
|
|
|
export async function restoreDatabase(backupPath: string): Promise<void> {
|
|
const dbPath = path.join(process.cwd(), 'database', 'antihoax.db')
|
|
|
|
try {
|
|
// Create backup of current DB first
|
|
await createDatabaseBackup()
|
|
|
|
// Copy backup to main location
|
|
await execAsync(`cp "${backupPath}" "${dbPath}"`)
|
|
|
|
console.log('Database restored successfully')
|
|
} catch (error) {
|
|
console.error('Restore failed:', error)
|
|
throw new Error('Database restore failed')
|
|
}
|
|
}
|
|
|
|
export async function listBackups(): Promise<string[]> {
|
|
try {
|
|
const { stdout } = await execAsync('ls -la backups/*.db 2>/dev/null || true')
|
|
return stdout.trim() ? stdout.trim().split('\n') : []
|
|
} catch {
|
|
return []
|
|
}
|
|
} |