hybridBackup.go 13 KB

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