backup_nginx_ui.go 2.2 KB

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