backup_archive.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package service
  2. import (
  3. "crypto/sha256"
  4. "encoding/hex"
  5. "errors"
  6. "fmt"
  7. "io"
  8. "os"
  9. )
  10. const defaultBackupPartSizeBytes int64 = 4 * 1024 * 1024 * 1024
  11. // BackupPart 描述一个 gzip 字节分卷。
  12. type BackupPart struct {
  13. Index int `json:"index"`
  14. S3Key string `json:"s3_key"`
  15. SizeBytes int64 `json:"size_bytes"`
  16. SHA256 string `json:"sha256,omitempty"`
  17. }
  18. type localBackupPart struct {
  19. Index int
  20. Path string
  21. SizeBytes int64
  22. SHA256 string
  23. }
  24. func splitBackupFile(srcPath string, partSize int64) (parts []localBackupPart, err error) {
  25. if partSize <= 0 {
  26. return nil, fmt.Errorf("backup part size must be positive")
  27. }
  28. src, err := os.Open(srcPath)
  29. if err != nil {
  30. return nil, fmt.Errorf("open backup archive: %w", err)
  31. }
  32. defer func() {
  33. if closeErr := src.Close(); err == nil && closeErr != nil {
  34. err = fmt.Errorf("close backup archive: %w", closeErr)
  35. }
  36. if err != nil {
  37. paths := make([]string, 0, len(parts))
  38. for _, part := range parts {
  39. paths = append(paths, part.Path)
  40. }
  41. _ = cleanupBackupFiles(paths...)
  42. }
  43. }()
  44. info, err := src.Stat()
  45. if err != nil {
  46. return nil, fmt.Errorf("stat backup archive: %w", err)
  47. }
  48. if info.Size() <= 0 {
  49. return nil, errors.New("backup archive is empty")
  50. }
  51. remaining := info.Size()
  52. for index := 1; remaining > 0; index++ {
  53. partFile, createErr := os.CreateTemp("", "sub2api-backup-part-*")
  54. if createErr != nil {
  55. return nil, fmt.Errorf("create backup part: %w", createErr)
  56. }
  57. partPath := partFile.Name()
  58. partBytes := partSize
  59. if remaining < partBytes {
  60. partBytes = remaining
  61. }
  62. hash := sha256.New()
  63. written, copyErr := io.CopyN(io.MultiWriter(partFile, hash), src, partBytes)
  64. closeErr := partFile.Close()
  65. if copyErr != nil {
  66. _ = os.Remove(partPath)
  67. return nil, fmt.Errorf("write backup part %d: %w", index, copyErr)
  68. }
  69. if closeErr != nil {
  70. _ = os.Remove(partPath)
  71. return nil, fmt.Errorf("close backup part %d: %w", index, closeErr)
  72. }
  73. parts = append(parts, localBackupPart{
  74. Index: index,
  75. Path: partPath,
  76. SizeBytes: written,
  77. SHA256: hex.EncodeToString(hash.Sum(nil)),
  78. })
  79. remaining -= written
  80. }
  81. return parts, nil
  82. }
  83. func cleanupBackupFiles(paths ...string) error {
  84. var errs []error
  85. for _, path := range paths {
  86. if path == "" {
  87. continue
  88. }
  89. if err := os.Remove(path); err != nil && !errors.Is(err, os.ErrNotExist) {
  90. errs = append(errs, fmt.Errorf("remove %s: %w", path, err))
  91. }
  92. }
  93. return errors.Join(errs...)
  94. }