hybridBackup.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package hybridBackup
  2. import (
  3. "log"
  4. "path/filepath"
  5. "strings"
  6. "time"
  7. )
  8. /*
  9. Hybrid Backup
  10. This module handle backup functions from the drive with Hieracchy labeled as "backup"
  11. Backup modes suport in this module currently consists of
  12. Denote P drive as parent drive and B drive as backup drive.
  13. 1. Smart (smart):
  14. - Any new file created in P will be copied to B within 5 minutes
  15. - Any file removed in P will be delete from backup after 24 hours
  16. 2. Nightly (nightly):
  17. - The whole P drive will be copied to N drive every night
  18. 3. Append Only (append)
  19. - Any new file created in P will be copied to B within 5 minutes
  20. - No file will be removed from B unless drive is fulled (Similar to CCTV recorder)
  21. */
  22. type BackupConfig struct {
  23. CycleCounter int64 //The number of backup executed in the background
  24. LastCycleTime int64 //The execution time of the last cycle
  25. DiskUID string //The UID of the target fsandlr
  26. DiskPath string //The mount point for the disk
  27. ParentUID string //Parent virtal disk UUID
  28. ParentPath string //Parent disk path
  29. Mode string //Backup mode
  30. }
  31. //Main handler function for hybrid backup
  32. func HandleBackupProcess(backupConfig *BackupConfig) (string, error) {
  33. log.Println(">>>>>> Running backup process: ", backupConfig)
  34. rootPath := filepath.ToSlash(filepath.Clean(backupConfig.ParentPath))
  35. //Check if the target disk is writable and mounted
  36. if fileExists(filepath.Join(backupConfig.DiskPath, "aofs.db")) && fileExists(filepath.Join(backupConfig.DiskPath, "aofs.db.lock")) {
  37. //This filesystem is mounted
  38. } else {
  39. //File system not mounted. Skipping
  40. log.Println("*HybridBackup* Skipping backup cycle for " + backupConfig.ParentUID + ":/")
  41. return "Parent drive (" + backupConfig.ParentUID + ":/) not mounted. Skipping", nil
  42. }
  43. if backupConfig.Mode == "smart" {
  44. //Smart backup mode. Scan new files every minutes and compare creation time scan every hour minutes
  45. if backupConfig.CycleCounter%5 == 0 {
  46. //Perform deep backup, use walk function
  47. } else {
  48. //Perform shallow backup
  49. fastWalk(rootPath, func(filename string) error {
  50. rootAbs, _ := filepath.Abs(rootPath)
  51. fileAbs, _ := filepath.Abs(filename)
  52. rootAbs = filepath.ToSlash(filepath.Clean(rootAbs))
  53. fileAbs = filepath.ToSlash(filepath.Clean(fileAbs))
  54. relPath := strings.ReplaceAll(fileAbs, rootAbs, "")
  55. assumedTargetPosition := filepath.Join(backupConfig.DiskPath, relPath)
  56. if !fileExists(assumedTargetPosition) {
  57. //Target file not exists in backup disk. Make a copy
  58. //WIP
  59. log.Println("Copying ", assumedTargetPosition)
  60. }
  61. return nil
  62. })
  63. }
  64. } else if backupConfig.Mode == "nightly" {
  65. } else if backupConfig.Mode == "append" {
  66. }
  67. //Add one to the cycle counter
  68. backupConfig.CycleCounter++
  69. backupConfig.LastCycleTime = time.Now().Unix()
  70. //Return the log information
  71. return "", nil
  72. }