hybridBackup.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. Ticker *time.Ticker //The main ticker
  33. StopTicker chan bool //Channel for stopping the backup
  34. Tasks []*BackupTask //The backup tasks that is running under this manager
  35. }
  36. type BackupTask struct {
  37. JobName string //The name used by the scheduler for executing this config
  38. CycleCounter int64 //The number of backup executed in the background
  39. LastCycleTime int64 //The execution time of the last cycle
  40. DiskUID string //The UID of the target fsandlr
  41. DiskPath string //The mount point for the disk
  42. ParentUID string //Parent virtal disk UUID
  43. ParentPath string //Parent disk path
  44. DeleteFileMarkers map[string]int64 //Markers for those files delete pending, [file path (relative)] time
  45. Mode string //Backup mode
  46. }
  47. //A file in the backup drive that is restorable
  48. type RestorableFile struct {
  49. Filename string //Filename of this restorable object
  50. RelpathOnDisk string //Relative path of this file to the root
  51. Deleteime int64 //Delete remaining time
  52. }
  53. //The restorable report
  54. type RestorableReport struct {
  55. ParentUID string //The Disk ID to be restored to
  56. DiskUID string //The Backup disk UID
  57. RestorableFiles []RestorableFile //A list of restorable files
  58. }
  59. var (
  60. internalTickerTime time.Duration = 60
  61. )
  62. func NewHyperBackupManager() *Manager {
  63. //Create a new minute ticker
  64. ticker := time.NewTicker(internalTickerTime * time.Second)
  65. stopper := make(chan bool, 1)
  66. newManager := &Manager{
  67. Ticker: ticker,
  68. StopTicker: stopper,
  69. Tasks: []*BackupTask{},
  70. }
  71. ///Create task executor
  72. go func() {
  73. defer log.Println("[HybridBackup] Ticker Stopped")
  74. for {
  75. select {
  76. case <-ticker.C:
  77. for _, task := range newManager.Tasks {
  78. task.HandleBackupProcess()
  79. }
  80. case <-stopper:
  81. return
  82. }
  83. }
  84. }()
  85. //Return the manager
  86. return newManager
  87. }
  88. func (m *Manager) AddTask(newtask *BackupTask) error {
  89. //Create a job for this
  90. newtask.JobName = "backup-[" + newtask.DiskUID + "]"
  91. //Check if the same job name exists
  92. for _, task := range m.Tasks {
  93. if task.JobName == newtask.JobName {
  94. return errors.New("Task already exists")
  95. }
  96. }
  97. m.Tasks = append(m.Tasks, newtask)
  98. log.Println(">>>> [Debug] New Backup Tasks added: ", newtask.JobName, newtask)
  99. return nil
  100. }
  101. func (m *Manager) StopTask(jobname string) error {
  102. return nil
  103. }
  104. //Stop all managed handlers
  105. func (m *Manager) Close() error {
  106. m.StopTicker <- true
  107. return nil
  108. }
  109. func executeBackup(backupConfig *BackupTask, deepBackup bool) (string, error) {
  110. copiedFileList := []string{}
  111. rootPath := filepath.ToSlash(filepath.Clean(backupConfig.ParentPath))
  112. //Check if the backup parent root is identical / within backup disk
  113. parentRootAbs, err := filepath.Abs(backupConfig.ParentPath)
  114. if err != nil {
  115. return "", errors.New("Unable to resolve parent disk path")
  116. }
  117. backupRootAbs, err := filepath.Abs(backupConfig.DiskPath)
  118. if err != nil {
  119. return "", errors.New("Unable to resolve backup disk path")
  120. }
  121. if len(parentRootAbs) >= len(backupRootAbs) {
  122. if parentRootAbs[:len(backupRootAbs)] == backupRootAbs {
  123. //parent root is within backup root. Raise configuration error
  124. log.Println("*HyperBackup* Invalid backup cycle: Parent drive is located inside backup drive")
  125. return "", errors.New("Configuration Error. Skipping backup cycle.")
  126. }
  127. }
  128. //Add file cycles
  129. fastWalk(rootPath, func(filename string) error {
  130. if filepath.Base(filename) == "aofs.db" || filepath.Base(filename) == "aofs.db.lock" {
  131. //Reserved filename, skipping
  132. return nil
  133. }
  134. //Get the target paste location
  135. rootAbs, _ := filepath.Abs(rootPath)
  136. fileAbs, _ := filepath.Abs(filename)
  137. rootAbs = filepath.ToSlash(filepath.Clean(rootAbs))
  138. fileAbs = filepath.ToSlash(filepath.Clean(fileAbs))
  139. relPath := strings.ReplaceAll(fileAbs, rootAbs, "")
  140. assumedTargetPosition := filepath.Join(backupConfig.DiskPath, relPath)
  141. if !deepBackup {
  142. //Shallow copy. Only do copy base on file exists or not
  143. //This is used to reduce the time for reading the file metatag
  144. if !fileExists(assumedTargetPosition) {
  145. //Target file not exists in backup disk. Make a copy
  146. if !fileExists(filepath.Dir(assumedTargetPosition)) {
  147. //Folder containing this file not exists. Create it
  148. os.MkdirAll(filepath.Dir(assumedTargetPosition), 0755)
  149. }
  150. //Copy the file to target
  151. err := BufferedLargeFileCopy(fileAbs, assumedTargetPosition, 1024)
  152. if err != nil {
  153. log.Println("[HybridBackup] Copy Failed for file "+filepath.Base(fileAbs), err.Error(), " Skipping.")
  154. } else {
  155. //No problem. Add this filepath into the list
  156. copiedFileList = append(copiedFileList, assumedTargetPosition)
  157. }
  158. }
  159. } else {
  160. //Deep copy. Check and match the modtime of each file
  161. if !fileExists(assumedTargetPosition) {
  162. if !fileExists(filepath.Dir(assumedTargetPosition)) {
  163. //Folder containing this file not exists. Create it
  164. os.MkdirAll(filepath.Dir(assumedTargetPosition), 0755)
  165. }
  166. //Copy the file to target
  167. err := BufferedLargeFileCopy(fileAbs, assumedTargetPosition, 1024)
  168. if err != nil {
  169. log.Println("[HybridBackup] Copy Failed for file "+filepath.Base(fileAbs), err.Error(), " Skipping.")
  170. return nil
  171. } else {
  172. //No problem. Add this filepath into the list
  173. copiedFileList = append(copiedFileList, assumedTargetPosition)
  174. }
  175. } else {
  176. //Target file already exists. Check if their hash matches
  177. srcHash, err := getFileHash(fileAbs)
  178. if err != nil {
  179. log.Println("[HybridBackup] Hash calculation failed for file "+filepath.Base(fileAbs), err.Error(), " Skipping.")
  180. return nil
  181. }
  182. targetHash, err := getFileHash(assumedTargetPosition)
  183. if err != nil {
  184. log.Println("[HybridBackup] Hash calculation failed for file "+filepath.Base(assumedTargetPosition), err.Error(), " Skipping.")
  185. return nil
  186. }
  187. if srcHash != targetHash {
  188. log.Println("[Debug] Hash mismatch. Copying ", fileAbs)
  189. //This file has been recently changed. Copy it to new location
  190. err = BufferedLargeFileCopy(fileAbs, assumedTargetPosition, 1024)
  191. if err != nil {
  192. log.Println("[HybridBackup] Copy Failed for file "+filepath.Base(fileAbs), err.Error(), " Skipping.")
  193. } else {
  194. //No problem. Add this filepath into the list
  195. copiedFileList = append(copiedFileList, assumedTargetPosition)
  196. }
  197. }
  198. }
  199. }
  200. ///Remove file cycle
  201. backupDriveRootPath := filepath.ToSlash(filepath.Clean(backupConfig.DiskPath))
  202. fastWalk(backupConfig.DiskPath, func(filename string) error {
  203. if filepath.Base(filename) == "aofs.db" || filepath.Base(filename) == "aofs.db.lock" {
  204. //Reserved filename, skipping
  205. return nil
  206. }
  207. //Get the target paste location
  208. rootAbs, _ := filepath.Abs(backupDriveRootPath)
  209. fileAbs, _ := filepath.Abs(filename)
  210. rootAbs = filepath.ToSlash(filepath.Clean(rootAbs))
  211. fileAbs = filepath.ToSlash(filepath.Clean(fileAbs))
  212. thisFileRel := filename[len(backupDriveRootPath):]
  213. originalFileOnDiskPath := filepath.ToSlash(filepath.Clean(filepath.Join(backupConfig.ParentPath, thisFileRel)))
  214. //Check if the taget file not exists and this file has been here for more than 24h
  215. if !fileExists(originalFileOnDiskPath) {
  216. //This file not exists. Check if it is in the delete file marker for more than 24 hours
  217. val, ok := backupConfig.DeleteFileMarkers[thisFileRel]
  218. if !ok {
  219. //This file is newly deleted. Push into the marker map
  220. backupConfig.DeleteFileMarkers[thisFileRel] = time.Now().Unix()
  221. log.Println("[Debug] Adding " + filename + " to delete marker")
  222. } else {
  223. //This file has been marked. Check if it is time to delete
  224. if time.Now().Unix()-val > 3600*24 {
  225. log.Println("[Debug] Deleting " + filename)
  226. //Remove the backup file
  227. os.RemoveAll(filename)
  228. //Remove file from delete file markers
  229. delete(backupConfig.DeleteFileMarkers, thisFileRel)
  230. }
  231. }
  232. }
  233. return nil
  234. })
  235. return nil
  236. })
  237. return "", nil
  238. }
  239. //Main handler function for hybrid backup
  240. func (backupConfig *BackupTask) HandleBackupProcess() (string, error) {
  241. log.Println(">>>>>> [Debug] Running backup process: ", backupConfig)
  242. //Check if the target disk is writable and mounted
  243. if fileExists(filepath.Join(backupConfig.ParentPath, "aofs.db")) && fileExists(filepath.Join(backupConfig.ParentPath, "aofs.db.lock")) {
  244. //This parent filesystem is mounted
  245. } else {
  246. //File system not mounted even after 3 backup cycle. Terminate backup scheduler
  247. log.Println("[HybridBackup] Skipping backup cycle for " + backupConfig.ParentUID + ":/")
  248. return "Parent drive (" + backupConfig.ParentUID + ":/) not mounted", nil
  249. }
  250. //Check if the backup disk is mounted. If no, stop the scheulder
  251. if backupConfig.CycleCounter > 3 && !(fileExists(filepath.Join(backupConfig.DiskPath, "aofs.db")) && fileExists(filepath.Join(backupConfig.DiskPath, "aofs.db.lock"))) {
  252. log.Println("[HybridBackup] Backup schedule stopped for " + backupConfig.DiskUID + ":/")
  253. return "Backup drive (" + backupConfig.DiskUID + ":/) not mounted", errors.New("Backup File System Handler not mounted")
  254. }
  255. deepBackup := true //Default perform deep backup
  256. if backupConfig.Mode == "basic" {
  257. if backupConfig.CycleCounter%30 == 0 {
  258. //Perform deep backup, use walk function
  259. deepBackup = true
  260. } else {
  261. deepBackup = false
  262. }
  263. backupConfig.LastCycleTime = time.Now().Unix()
  264. return executeBackup(backupConfig, deepBackup)
  265. } else if backupConfig.Mode == "nightly" {
  266. if time.Now().Unix()-backupConfig.LastCycleTime >= 86400 {
  267. //24 hours from last backup. Execute deep backup now
  268. executeBackup(backupConfig, true)
  269. backupConfig.LastCycleTime = time.Now().Unix()
  270. }
  271. } else if backupConfig.Mode == "version" {
  272. //Do a versioning backup
  273. log.Println("[WIP] This function is still work in progress. Please do not use version backup for now.")
  274. //WIP
  275. }
  276. //Add one to the cycle counter
  277. backupConfig.CycleCounter++
  278. //Return the log information
  279. return "", nil
  280. }
  281. //Restore accidentailly removed file from backup
  282. func HandleRestore(parentDiskID string, restoreDiskID string, targetFileRelpath string) error {
  283. return nil
  284. }
  285. //List the file that is restorable from the given disk
  286. func (m *Manager) ListRestorable(parentDiskID string) RestorableReport {
  287. return RestorableReport{}
  288. }
  289. //Get and return the file hash for a file
  290. func getFileHash(filename string) (string, error) {
  291. f, err := os.Open(filename)
  292. if err != nil {
  293. return "", err
  294. }
  295. defer f.Close()
  296. h := sha256.New()
  297. if _, err := io.Copy(h, f); err != nil {
  298. return "", err
  299. }
  300. return hex.EncodeToString(h.Sum(nil)), nil
  301. }