hybridBackup.go 14 KB

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