backup.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import type { ModelBase } from '@/api/curd'
  2. import { http, useCurdApi } from '@uozi-admin/request'
  3. /**
  4. * Interface for restore backup response
  5. */
  6. export interface RestoreResponse {
  7. restore_dir: string
  8. nginx_ui_restored: boolean
  9. nginx_restored: boolean
  10. hash_match: boolean
  11. }
  12. /**
  13. * Interface for restore backup options
  14. */
  15. export interface RestoreOptions {
  16. backup_file: File
  17. security_token: string
  18. restore_nginx: boolean
  19. restore_nginx_ui: boolean
  20. verify_hash: boolean
  21. }
  22. /**
  23. * Interface for auto backup configuration
  24. */
  25. export interface AutoBackup extends ModelBase {
  26. name: string
  27. backup_type: 'nginx_config' | 'nginx_ui_config' | 'both_config' | 'custom_dir'
  28. storage_type: 'local' | 's3'
  29. backup_path?: string
  30. storage_path: string
  31. cron_expression: string
  32. enabled: boolean
  33. last_backup_time?: string
  34. last_backup_status: 'pending' | 'success' | 'failed'
  35. last_backup_error?: string
  36. s3_endpoint?: string
  37. s3_access_key_id?: string
  38. s3_secret_access_key?: string
  39. s3_bucket?: string
  40. s3_region?: string
  41. }
  42. const backup = {
  43. /**
  44. * Create and download a backup of nginx-ui and nginx configurations
  45. * Use http module with returnFullResponse option to access headers
  46. */
  47. createBackup() {
  48. return http.get('/backup', {
  49. responseType: 'blob',
  50. returnFullResponse: true,
  51. })
  52. },
  53. /**
  54. * Restore from a backup file
  55. * @param options RestoreOptions
  56. */
  57. restoreBackup(options: RestoreOptions) {
  58. const formData = new FormData()
  59. formData.append('backup_file', options.backup_file)
  60. formData.append('security_token', options.security_token)
  61. formData.append('restore_nginx', options.restore_nginx.toString())
  62. formData.append('restore_nginx_ui', options.restore_nginx_ui.toString())
  63. formData.append('verify_hash', options.verify_hash.toString())
  64. return http.post('/restore', formData, {
  65. headers: {
  66. 'Content-Type': 'multipart/form-data;charset=UTF-8',
  67. },
  68. crypto: true,
  69. })
  70. },
  71. }
  72. /**
  73. * Test S3 connection for auto backup configuration
  74. * @param config AutoBackup configuration with S3 settings
  75. */
  76. export function testS3Connection(config: AutoBackup) {
  77. return http.post('/auto_backup/test_s3', config)
  78. }
  79. // Auto backup CRUD API
  80. export const autoBackup = useCurdApi<AutoBackup>('/auto_backup')
  81. export default backup