backup_nginx_ui.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package backup
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "github.com/0xJacky/Nginx-UI/settings"
  7. "github.com/uozi-tech/cosy"
  8. "github.com/uozi-tech/cosy/logger"
  9. cosysettings "github.com/uozi-tech/cosy/settings"
  10. )
  11. // backupNginxUIFiles backs up the nginx-ui configuration and database files
  12. func backupNginxUIFiles(destDir string) error {
  13. // Get config file path
  14. configPath := cosysettings.ConfPath
  15. if configPath == "" {
  16. return ErrConfigPathEmpty
  17. }
  18. // Always save the config file as app.ini, regardless of its original name
  19. destConfigPath := filepath.Join(destDir, "app.ini")
  20. if err := copyFile(configPath, destConfigPath); err != nil {
  21. return cosy.WrapErrorWithParams(ErrCopyConfigFile, err.Error())
  22. }
  23. // Get database file name and path
  24. dbName := settings.DatabaseSettings.GetName()
  25. dbFile := dbName + ".db"
  26. // Database directory is the same as config file directory
  27. dbDir := filepath.Dir(configPath)
  28. dbPath := filepath.Join(dbDir, dbFile)
  29. // Copy database file
  30. if _, err := os.Stat(dbPath); err == nil {
  31. // Database exists as file
  32. destDBPath := filepath.Join(destDir, dbFile)
  33. if err := copyFile(dbPath, destDBPath); err != nil {
  34. return cosy.WrapErrorWithParams(ErrCopyDBFile, err.Error())
  35. }
  36. } else {
  37. logger.Warn("Database file not found: %s", dbPath)
  38. }
  39. return nil
  40. }
  41. // backupNginxFiles backs up the nginx configuration directory
  42. func backupNginxFiles(destDir string) error {
  43. // Get nginx config directory
  44. nginxConfigDir := settings.NginxSettings.ConfigDir
  45. if nginxConfigDir == "" {
  46. return ErrNginxConfigDirEmpty
  47. }
  48. // Copy nginx config directory
  49. if err := copyDirectory(nginxConfigDir, destDir); err != nil {
  50. return cosy.WrapErrorWithParams(ErrCopyNginxConfigDir, err.Error())
  51. }
  52. return nil
  53. }
  54. // writeHashInfoFile creates a hash information file for verification
  55. func writeHashInfoFile(hashFilePath string, info HashInfo) error {
  56. content := fmt.Sprintf("nginx-ui_hash: %s\nnginx_hash: %s\ntimestamp: %s\nversion: %s\n",
  57. info.NginxUIHash, info.NginxHash, info.Timestamp, info.Version)
  58. if err := os.WriteFile(hashFilePath, []byte(content), 0644); err != nil {
  59. return cosy.WrapErrorWithParams(ErrCreateHashFile, err.Error())
  60. }
  61. return nil
  62. }