hybridBackup.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. package hybridBackup
  2. import (
  3. "crypto/sha256"
  4. "encoding/hex"
  5. "errors"
  6. "io"
  7. "log"
  8. "os"
  9. "path/filepath"
  10. "strings"
  11. "time"
  12. )
  13. /*
  14. Hybrid Backup
  15. This module handle backup functions from the drive with Hieracchy labeled as "backup"
  16. Backup modes suport in this module currently consists of
  17. Denote P drive as parent drive and B drive as backup drive.
  18. 1. Basic (basic):
  19. - Any new file created in P will be copied to B within 1 minutes
  20. - Any file change will be copied to B within 30 minutes
  21. - Any file removed in P will be delete from backup if it is > 24 hours old
  22. 2. Nightly (nightly):
  23. - The whole P drive will be copied to N drive every night
  24. 3. Versioning (version)
  25. - A versioning system will be introduce to this backup drive
  26. - Just like the time machine
  27. Tips when developing this module
  28. - This is a sub-module of the current file system. Do not import from arozos file system module
  29. - If you need any function from the file system, copy and paste it in this module
  30. */
  31. type Manager struct {
  32. Tasks []*BackupTask
  33. }
  34. type BackupTask struct {
  35. JobName string //The name used by the scheduler for executing this config
  36. CycleCounter int64 //The number of backup executed in the background
  37. LastCycleTime int64 //The execution time of the last cycle
  38. DiskUID string //The UID of the target fsandlr
  39. DiskPath string //The mount point for the disk
  40. ParentUID string //Parent virtal disk UUID
  41. ParentPath string //Parent disk path
  42. DeleteFileMarkers map[string]int64 //Markers for those files delete pending, [file path (relative)] time
  43. Mode string //Backup mode
  44. }
  45. //A file in the backup drive that is restorable
  46. type RestorableFile struct {
  47. Filename string //Filename of this restorable object
  48. RelpathOnDisk string //Relative path of this file to the root
  49. Deleteime int64 //Delete remaining time
  50. }
  51. //The restorable report
  52. type RestorableReport struct {
  53. ParentUID string //The Disk ID to be restored to
  54. DiskUID string //The Backup disk UID
  55. RestorableFiles []RestorableFile //A list of restorable files
  56. }
  57. func NewHyperBackupManager() *Manager {
  58. return &Manager{
  59. Tasks: []*BackupTask{},
  60. }
  61. }
  62. func (m *Manager) AddTask(newtask *BackupTask) error {
  63. log.Println(">>>> [Debug] New Backup Tasks added: ", newtask)
  64. /*for _, thisHandler := range fsHandlers {
  65. if thisHandler.Hierarchy == "backup" {
  66. //This is a backup drive. Generate it handler
  67. backupConfig := thisHandler.HierarchyConfig.(hybridBackup.BackupTask)
  68. //Get its parent mount point for backup
  69. parentFileSystemHandler, err := GetFsHandlerByUUID(backupConfig.ParentUID)
  70. if err != nil {
  71. log.Println("Virtual Root with UUID: " + backupConfig.ParentUID + " not loaded. Unable to start backup process.")
  72. break
  73. }
  74. backupConfig.JobName = "backup-daemon [" + thisHandler.UUID + "]"
  75. backupConfig.ParentPath = parentFileSystemHandler.Path
  76. backupConfig.CycleCounter = 1
  77. //Debug backup execution
  78. hybridBackup.HandleBackupProcess(&backupConfig)
  79. //Remove the previous job if it exists
  80. if systemScheduler.JobExists(backupConfig.JobName) {
  81. systemScheduler.RemoveJobFromScheduleList(backupConfig.JobName)
  82. }
  83. //Create a scheudler for this disk
  84. systemScheduler.CreateNewScheduledFunctionJob(backupConfig.JobName,
  85. "Backup daemon from "+backupConfig.ParentUID+":/ to "+backupConfig.DiskUID+":/",
  86. 60,
  87. func() (string, error) {
  88. return hybridBackup.HandleBackupProcess(&backupConfig)
  89. },
  90. )
  91. }
  92. }*/
  93. return nil
  94. }
  95. func executeBackup(backupConfig *BackupTask, deepBackup bool) (string, error) {
  96. copiedFileList := []string{}
  97. rootPath := filepath.ToSlash(filepath.Clean(backupConfig.ParentPath))
  98. //Check if the backup parent root is identical / within backup disk
  99. parentRootAbs, err := filepath.Abs(backupConfig.ParentPath)
  100. if err != nil {
  101. return "", errors.New("Unable to resolve parent disk path")
  102. }
  103. backupRootAbs, err := filepath.Abs(backupConfig.DiskPath)
  104. if err != nil {
  105. return "", errors.New("Unable to resolve backup disk path")
  106. }
  107. if len(parentRootAbs) >= len(backupRootAbs) {
  108. if parentRootAbs[:len(backupRootAbs)] == backupRootAbs {
  109. //parent root is within backup root. Raise configuration error
  110. log.Println("*HyperBackup* Invalid backup cycle: Parent drive is located inside backup drive")
  111. return "", errors.New("Configuration Error. Skipping backup cycle.")
  112. }
  113. }
  114. //Add file cycles
  115. fastWalk(rootPath, func(filename string) error {
  116. if filepath.Base(filename) == "aofs.db" || filepath.Base(filename) == "aofs.db.lock" {
  117. //Reserved filename, skipping
  118. return nil
  119. }
  120. //Get the target paste location
  121. rootAbs, _ := filepath.Abs(rootPath)
  122. fileAbs, _ := filepath.Abs(filename)
  123. rootAbs = filepath.ToSlash(filepath.Clean(rootAbs))
  124. fileAbs = filepath.ToSlash(filepath.Clean(fileAbs))
  125. relPath := strings.ReplaceAll(fileAbs, rootAbs, "")
  126. assumedTargetPosition := filepath.Join(backupConfig.DiskPath, relPath)
  127. if !deepBackup {
  128. //Shallow copy. Only do copy base on file exists or not
  129. //This is used to reduce the time for reading the file metatag
  130. if !fileExists(assumedTargetPosition) {
  131. //Target file not exists in backup disk. Make a copy
  132. if !fileExists(filepath.Dir(assumedTargetPosition)) {
  133. //Folder containing this file not exists. Create it
  134. os.MkdirAll(filepath.Dir(assumedTargetPosition), 0755)
  135. }
  136. //Copy the file to target
  137. err := BufferedLargeFileCopy(fileAbs, assumedTargetPosition, 1024)
  138. if err != nil {
  139. log.Println("*Hybrid Backup* Copy Failed for file "+filepath.Base(fileAbs), err.Error(), " Skipping.")
  140. } else {
  141. //No problem. Add this filepath into the list
  142. copiedFileList = append(copiedFileList, assumedTargetPosition)
  143. }
  144. }
  145. } else {
  146. //Deep copy. Check and match the modtime of each file
  147. if !fileExists(assumedTargetPosition) {
  148. //Copy the file to target
  149. err := BufferedLargeFileCopy(fileAbs, assumedTargetPosition, 1024)
  150. if err != nil {
  151. log.Println("*Hybrid Backup* Copy Failed for file "+filepath.Base(fileAbs), err.Error(), " Skipping.")
  152. return nil
  153. } else {
  154. //No problem. Add this filepath into the list
  155. copiedFileList = append(copiedFileList, assumedTargetPosition)
  156. }
  157. } else {
  158. //Target file already exists. Check if their hash matches
  159. srcHash, err := getFileHash(fileAbs)
  160. if err != nil {
  161. log.Println("*Hybrid Backup* Hash calculation failed for file "+filepath.Base(fileAbs), err.Error(), " Skipping.")
  162. return nil
  163. }
  164. targetHash, err := getFileHash(assumedTargetPosition)
  165. if err != nil {
  166. log.Println("*Hybrid Backup* Hash calculation failed for file "+filepath.Base(assumedTargetPosition), err.Error(), " Skipping.")
  167. return nil
  168. }
  169. if srcHash != targetHash {
  170. log.Println("[Debug] Hash mismatch. Copying ", fileAbs)
  171. //This file has been recently changed. Copy it to new location
  172. err = BufferedLargeFileCopy(fileAbs, assumedTargetPosition, 1024)
  173. if err != nil {
  174. log.Println("*Hybrid Backup* Copy Failed for file "+filepath.Base(fileAbs), err.Error(), " Skipping.")
  175. } else {
  176. //No problem. Add this filepath into the list
  177. copiedFileList = append(copiedFileList, assumedTargetPosition)
  178. }
  179. }
  180. }
  181. }
  182. ///Remove file cycle
  183. backupDriveRootPath := filepath.ToSlash(filepath.Clean(backupConfig.DiskPath))
  184. fastWalk(backupConfig.DiskPath, func(filename string) error {
  185. if filepath.Base(filename) == "aofs.db" || filepath.Base(filename) == "aofs.db.lock" {
  186. //Reserved filename, skipping
  187. return nil
  188. }
  189. //Get the target paste location
  190. rootAbs, _ := filepath.Abs(backupDriveRootPath)
  191. fileAbs, _ := filepath.Abs(filename)
  192. rootAbs = filepath.ToSlash(filepath.Clean(rootAbs))
  193. fileAbs = filepath.ToSlash(filepath.Clean(fileAbs))
  194. thisFileRel := filename[len(backupDriveRootPath):]
  195. originalFileOnDiskPath := filepath.ToSlash(filepath.Clean(filepath.Join(backupConfig.ParentPath, thisFileRel)))
  196. //Check if the taget file not exists and this file has been here for more than 24h
  197. if !fileExists(originalFileOnDiskPath) {
  198. //This file not exists. Check if it is in the delete file marker for more than 24 hours
  199. val, ok := backupConfig.DeleteFileMarkers[thisFileRel]
  200. if !ok {
  201. //This file is newly deleted. Push into the marker map
  202. backupConfig.DeleteFileMarkers[thisFileRel] = time.Now().Unix()
  203. log.Println("[Debug] Adding " + filename + " to delete marker")
  204. } else {
  205. //This file has been marked. Check if it is time to delete
  206. if time.Now().Unix()-val > 3600*24 {
  207. log.Println("[Debug] Deleting " + filename)
  208. //Remove the backup file
  209. os.RemoveAll(filename)
  210. //Remove file from delete file markers
  211. delete(backupConfig.DeleteFileMarkers, thisFileRel)
  212. }
  213. }
  214. }
  215. return nil
  216. })
  217. return nil
  218. })
  219. return "", nil
  220. }
  221. //Main handler function for hybrid backup
  222. func HandleBackupProcess(backupConfig *BackupTask) (string, error) {
  223. log.Println(">>>>>> [Debug] Running backup process: ", backupConfig)
  224. //Check if the target disk is writable and mounted
  225. if fileExists(filepath.Join(backupConfig.ParentPath, "aofs.db")) && fileExists(filepath.Join(backupConfig.ParentPath, "aofs.db.lock")) {
  226. //This parent filesystem is mounted
  227. } else {
  228. //File system not mounted even after 3 backup cycle. Terminate backup scheduler
  229. log.Println("*HybridBackup* Skipping backup cycle for " + backupConfig.ParentUID + ":/")
  230. return "Parent drive (" + backupConfig.ParentUID + ":/) not mounted", nil
  231. }
  232. //Check if the backup disk is mounted. If no, stop the scheulder
  233. if backupConfig.CycleCounter > 3 && !(fileExists(filepath.Join(backupConfig.DiskPath, "aofs.db")) && fileExists(filepath.Join(backupConfig.DiskPath, "aofs.db.lock"))) {
  234. log.Println("*HybridBackup* Backup schedule stopped for " + backupConfig.DiskUID + ":/")
  235. return "Backup drive (" + backupConfig.DiskUID + ":/) not mounted", errors.New("Backup File System Handler not mounted")
  236. }
  237. deepBackup := true //Default perform deep backup
  238. if backupConfig.Mode == "basic" {
  239. if backupConfig.CycleCounter%30 == 0 {
  240. //Perform deep backup, use walk function
  241. deepBackup = true
  242. } else {
  243. deepBackup = false
  244. }
  245. backupConfig.LastCycleTime = time.Now().Unix()
  246. return executeBackup(backupConfig, deepBackup)
  247. } else if backupConfig.Mode == "nightly" {
  248. if time.Now().Unix()-backupConfig.LastCycleTime >= 86400 {
  249. //24 hours from last backup. Execute deep backup now
  250. executeBackup(backupConfig, true)
  251. backupConfig.LastCycleTime = time.Now().Unix()
  252. }
  253. } else if backupConfig.Mode == "version" {
  254. //Do a versioning backup
  255. log.Println("[WIP] This function is still work in progress. Please do not use version backup for now.")
  256. //WIP
  257. }
  258. //Add one to the cycle counter
  259. backupConfig.CycleCounter++
  260. //Return the log information
  261. return "", nil
  262. }
  263. //Restore accidentailly removed file from backup
  264. func HandleRestore(backupConfig *BackupTask, targetFile string) error {
  265. return nil
  266. }
  267. //List the file that is restorable from the given disk
  268. func ListRestorable(backupConfig *BackupTask) {
  269. }
  270. //Get and return the file hash for a file
  271. func getFileHash(filename string) (string, error) {
  272. f, err := os.Open(filename)
  273. if err != nil {
  274. return "", err
  275. }
  276. defer f.Close()
  277. h := sha256.New()
  278. if _, err := io.Copy(h, f); err != nil {
  279. return "", err
  280. }
  281. return hex.EncodeToString(h.Sum(nil)), nil
  282. }