hybridBackup.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. package hybridBackup
  2. import (
  3. "crypto/sha256"
  4. "encoding/hex"
  5. "encoding/json"
  6. "errors"
  7. "io"
  8. "log"
  9. "os"
  10. "path/filepath"
  11. "strings"
  12. "time"
  13. "imuslab.com/arozos/mod/database"
  14. )
  15. /*
  16. Hybrid Backup
  17. This module handle backup functions from the drive with Hieracchy labeled as "backup"
  18. Backup modes suport in this module currently consists of
  19. Denote P drive as parent drive and B drive as backup drive.
  20. 1. Basic (basic):
  21. - Any new file created in P will be copied to B within 1 minutes
  22. - Any file change will be copied to B within 30 minutes
  23. - Any file removed in P will be delete from backup if it is > 24 hours old
  24. 2. Nightly (nightly):
  25. - The whole P drive will be copied to N drive every night
  26. 3. Versioning (version)
  27. - A versioning system will be introduce to this backup drive
  28. - Just like the time machine
  29. Tips when developing this module
  30. - This is a sub-module of the current file system. Do not import from arozos file system module
  31. - If you need any function from the file system, copy and paste it in this module
  32. */
  33. type Manager struct {
  34. Ticker *time.Ticker `json:"-"` //The main ticker
  35. StopTicker chan bool `json:"-"` //Channel for stopping the backup
  36. Tasks []*BackupTask //The backup tasks that is running under this manager
  37. }
  38. type BackupTask struct {
  39. JobName string //The name used by the scheduler for executing this config
  40. CycleCounter int64 //The number of backup executed in the background
  41. LastCycleTime int64 //The execution time of the last cycle
  42. Enabled bool //Check if the task is enabled. Will not execute if this is set to false
  43. DiskUID string //The UID of the target fsandlr
  44. DiskPath string //The mount point for the disk
  45. ParentUID string //Parent virtal disk UUID
  46. ParentPath string //Parent disk path
  47. DeleteFileMarkers map[string]int64 //Markers for those files delete pending, [file path (relative)] time
  48. Database *database.Database //The database for storing requried data
  49. Mode string //Backup mode
  50. }
  51. //A snapshot summary
  52. type SnapshotSummary struct {
  53. ChangedFiles map[string]string
  54. UnchangedFiles map[string]string
  55. DeletedFiles map[string]string
  56. }
  57. //A file in the backup drive that is restorable
  58. type RestorableFile struct {
  59. Filename string //Filename of this restorable object
  60. IsHidden bool //Check if the file is hidden or located in a path within hidden folder
  61. Filesize int64 //The file size to be restorable
  62. RelpathOnDisk string //Relative path of this file to the root
  63. RestorePoint string //The location this file should restore to
  64. BackupDiskUID string //The UID of disk that is hold the backup of this file
  65. RemainingTime int64 //Remaining time till auto remove
  66. DeleteTime int64 //Delete time
  67. IsSnapshot bool //Define is this restorable file point to a snapshot instead
  68. }
  69. //The restorable report
  70. type RestorableReport struct {
  71. ParentUID string //The Disk ID to be restored to
  72. RestorableFiles []*RestorableFile //A list of restorable files
  73. }
  74. var (
  75. internalTickerTime time.Duration = 60
  76. )
  77. func NewHyperBackupManager() *Manager {
  78. //Create a new minute ticker
  79. ticker := time.NewTicker(internalTickerTime * time.Second)
  80. stopper := make(chan bool, 1)
  81. newManager := &Manager{
  82. Ticker: ticker,
  83. StopTicker: stopper,
  84. Tasks: []*BackupTask{},
  85. }
  86. ///Create task executor
  87. go func() {
  88. defer log.Println("[HybridBackup] Ticker Stopped")
  89. for {
  90. select {
  91. case <-ticker.C:
  92. for _, task := range newManager.Tasks {
  93. if task.Enabled == true {
  94. task.HandleBackupProcess()
  95. }
  96. }
  97. case <-stopper:
  98. return
  99. }
  100. }
  101. }()
  102. //Return the manager
  103. return newManager
  104. }
  105. func (m *Manager) AddTask(newtask *BackupTask) error {
  106. //Create a job for this
  107. newtask.JobName = "backup-" + newtask.DiskUID + ""
  108. //Check if the same job name exists
  109. for _, task := range m.Tasks {
  110. if task.JobName == newtask.JobName {
  111. return errors.New("Task already exists")
  112. }
  113. }
  114. //Create / Load a backup database for the task
  115. dbPath := filepath.Join(newtask.DiskPath, newtask.JobName+".db")
  116. thisdb, err := database.NewDatabase(dbPath, false)
  117. if err != nil {
  118. log.Println("[HybridBackup] Failed to create database for backup tasks. Running without one.")
  119. } else {
  120. newtask.Database = thisdb
  121. thisdb.NewTable("DeleteMarkers")
  122. }
  123. if newtask.Mode == "basic" || newtask.Mode == "nightly" {
  124. //Load the delete marker from the database if exists
  125. if thisdb.TableExists("DeleteMarkers") {
  126. //Table exists. Read all its content to delete markers
  127. entries, _ := thisdb.ListTable("DeleteMarkers")
  128. for _, keypairs := range entries {
  129. relPath := string(keypairs[0])
  130. delTime := int64(0)
  131. json.Unmarshal(keypairs[1], &delTime)
  132. //Add this to delete marker
  133. newtask.DeleteFileMarkers[relPath] = delTime
  134. }
  135. }
  136. }
  137. //Add task to list
  138. m.Tasks = append(m.Tasks, newtask)
  139. //Start the task
  140. m.StartTask(newtask.JobName)
  141. //log.Println(">>>> [Debug] New Backup Tasks added: ", newtask.JobName, newtask)
  142. return nil
  143. }
  144. //Start a given task given name
  145. func (m *Manager) StartTask(jobname string) {
  146. for _, task := range m.Tasks {
  147. if task.JobName == jobname {
  148. //Enable to job
  149. task.Enabled = true
  150. //Run it once
  151. task.HandleBackupProcess()
  152. }
  153. }
  154. }
  155. //Stop a given task given its job name
  156. func (m *Manager) StopTask(jobname string) {
  157. for _, task := range m.Tasks {
  158. if task.JobName == jobname {
  159. task.Enabled = false
  160. }
  161. }
  162. }
  163. //Stop all managed handlers
  164. func (m *Manager) Close() error {
  165. //Stop the schedule
  166. if m != nil {
  167. m.StopTicker <- true
  168. }
  169. //Close all database opened by backup task
  170. for _, task := range m.Tasks {
  171. task.Database.Close()
  172. }
  173. return nil
  174. }
  175. //Main handler function for hybrid backup
  176. func (backupConfig *BackupTask) HandleBackupProcess() (string, error) {
  177. //Check if the target disk is writable and mounted
  178. if fileExists(filepath.Join(backupConfig.ParentPath, "aofs.db")) && fileExists(filepath.Join(backupConfig.ParentPath, "aofs.db.lock")) {
  179. //This parent filesystem is mounted
  180. } else {
  181. //File system not mounted even after 3 backup cycle. Terminate backup scheduler
  182. log.Println("[HybridBackup] Skipping backup cycle for " + backupConfig.ParentUID + ":/")
  183. return "Parent drive (" + backupConfig.ParentUID + ":/) not mounted", nil
  184. }
  185. //Check if the backup disk is mounted. If no, stop the scheulder
  186. if backupConfig.CycleCounter > 3 && !(fileExists(filepath.Join(backupConfig.DiskPath, "aofs.db")) && fileExists(filepath.Join(backupConfig.DiskPath, "aofs.db.lock"))) {
  187. log.Println("[HybridBackup] Backup schedule stopped for " + backupConfig.DiskUID + ":/")
  188. return "Backup drive (" + backupConfig.DiskUID + ":/) not mounted", errors.New("Backup File System Handler not mounted")
  189. }
  190. deepBackup := true //Default perform deep backup
  191. if backupConfig.Mode == "basic" {
  192. if backupConfig.CycleCounter%3 == 0 {
  193. //Perform deep backup, use walk function
  194. deepBackup = true
  195. log.Println("[HybridBackup] Basic backup executed: " + backupConfig.ParentUID + ":/ -> " + backupConfig.DiskUID + ":/")
  196. backupConfig.LastCycleTime = time.Now().Unix()
  197. } else {
  198. deepBackup = false
  199. }
  200. //Add one to the cycle counter
  201. backupConfig.CycleCounter++
  202. _, err := executeBackup(backupConfig, deepBackup)
  203. if err != nil {
  204. log.Println("[HybridBackup] Backup failed: " + err.Error())
  205. }
  206. } else if backupConfig.Mode == "nightly" {
  207. if time.Now().Unix()-backupConfig.LastCycleTime >= 86400 {
  208. //24 hours from last backup. Execute deep backup now
  209. executeBackup(backupConfig, true)
  210. backupConfig.LastCycleTime = time.Now().Unix()
  211. log.Println("[HybridBackup] Executing nightly backup: " + backupConfig.ParentUID + ":/ -> " + backupConfig.DiskUID + ":/")
  212. //Add one to the cycle counter
  213. backupConfig.CycleCounter++
  214. }
  215. } else if backupConfig.Mode == "version" {
  216. //Do a versioning backup every 6 hours
  217. if time.Now().Unix()-backupConfig.LastCycleTime >= 21600 || backupConfig.CycleCounter == 0 {
  218. //Scheduled backup or initial backup
  219. executeVersionBackup(backupConfig)
  220. backupConfig.LastCycleTime = time.Now().Unix()
  221. log.Println("[HybridBackup] Executing backup schedule: " + backupConfig.ParentUID + ":/ -> " + backupConfig.DiskUID + ":/")
  222. //Add one to the cycle counter
  223. backupConfig.CycleCounter++
  224. }
  225. }
  226. //Return the log information
  227. return "", nil
  228. }
  229. //Get the restore parent disk ID by backup disk ID
  230. func (m *Manager) GetParentDiskIDByRestoreDiskID(restoreDiskID string) (string, error) {
  231. backupTask := m.getTaskByBackupDiskID(restoreDiskID)
  232. if backupTask == nil {
  233. return "", errors.New("This disk do not have a backup task in this backup maanger")
  234. }
  235. return backupTask.ParentUID, nil
  236. }
  237. //Restore accidentailly removed file from backup
  238. func (m *Manager) HandleRestore(restoreDiskID string, targetFileRelpath string, username *string) error {
  239. //Get the backup task from backup disk id
  240. backupTask := m.getTaskByBackupDiskID(restoreDiskID)
  241. if backupTask == nil {
  242. return errors.New("Target disk is not a backup disk")
  243. }
  244. //Check if source exists and target not exists
  245. //log.Println("[debug]", backupTask)
  246. restoreSource := filepath.Join(backupTask.DiskPath, targetFileRelpath)
  247. if backupTask.Mode == "basic" || backupTask.Mode == "nightly" {
  248. restoreSource = filepath.Join(backupTask.DiskPath, "/backup/", targetFileRelpath)
  249. restoreTarget := filepath.Join(backupTask.ParentPath, targetFileRelpath)
  250. if !fileExists(restoreSource) {
  251. //Restore source not exists
  252. return errors.New("Restore source file not exists")
  253. }
  254. if fileExists(restoreTarget) {
  255. //Restore target already exists.
  256. return errors.New("Restore target already exists. Cannot overwrite.")
  257. }
  258. //Check if the restore target parent folder exists. If not, create it
  259. if !fileExists(filepath.Dir(restoreTarget)) {
  260. os.MkdirAll(filepath.Dir(restoreTarget), 0755)
  261. }
  262. //Ready to move it back
  263. err := BufferedLargeFileCopy(restoreSource, restoreTarget, 4086)
  264. if err != nil {
  265. return errors.New("Restore failed: " + err.Error())
  266. }
  267. } else if backupTask.Mode == "version" {
  268. //Check if username is set
  269. if username == nil {
  270. return errors.New("Snapshot mode backup require username to restore")
  271. }
  272. //Restore the snapshot
  273. err := restoreSnapshotByName(backupTask, targetFileRelpath, username)
  274. if err != nil {
  275. return errors.New("Restore failed: " + err.Error())
  276. }
  277. }
  278. //Restore completed
  279. return nil
  280. }
  281. //List the file that is restorable from the given disk
  282. func (m *Manager) ListRestorable(parentDiskID string) (RestorableReport, error) {
  283. //List all the backup process that is mirroring this parent disk
  284. tasks := m.getTaskByParentDiskID(parentDiskID)
  285. if len(tasks) == 0 {
  286. return RestorableReport{}, errors.New("No backup root found for this " + parentDiskID + ":/ virtual root.")
  287. }
  288. diffFiles := []*RestorableFile{}
  289. //Extract all comparasion
  290. for _, task := range tasks {
  291. if task.Mode == "basic" || task.Mode == "nightly" {
  292. restorableFiles, err := listBasicRestorables(task)
  293. if err != nil {
  294. //Something went wrong. Skip this
  295. continue
  296. }
  297. for _, restorable := range restorableFiles {
  298. diffFiles = append(diffFiles, restorable)
  299. }
  300. } else if task.Mode == "version" {
  301. restorableFiles, err := listVersionRestorables(task)
  302. if err != nil {
  303. //Something went wrong. Skip this
  304. continue
  305. }
  306. for _, restorable := range restorableFiles {
  307. diffFiles = append(diffFiles, restorable)
  308. }
  309. } else {
  310. //Unknown mode. Skip it
  311. }
  312. }
  313. //Create a Restorable Report
  314. thisReport := RestorableReport{
  315. ParentUID: parentDiskID,
  316. RestorableFiles: diffFiles,
  317. }
  318. return thisReport, nil
  319. }
  320. //Get tasks from parent disk id, might return multiple task or no tasks
  321. func (m *Manager) getTaskByParentDiskID(parentDiskID string) []*BackupTask {
  322. //Convert ID:/ format to ID
  323. if strings.Contains(parentDiskID, ":") {
  324. parentDiskID = strings.Split(parentDiskID, ":")[0]
  325. }
  326. possibleTask := []*BackupTask{}
  327. for _, task := range m.Tasks {
  328. if task.ParentUID == parentDiskID {
  329. //This task parent is the target disk. push this to list
  330. possibleTask = append(possibleTask, task)
  331. }
  332. }
  333. return possibleTask
  334. }
  335. //Get task by backup Disk ID, only return 1 task
  336. func (m *Manager) getTaskByBackupDiskID(backupDiskID string) *BackupTask {
  337. //Trim the :/ parts
  338. if strings.Contains(backupDiskID, ":") {
  339. backupDiskID = strings.Split(backupDiskID, ":")[0]
  340. }
  341. for _, task := range m.Tasks {
  342. if task.DiskUID == backupDiskID {
  343. return task
  344. }
  345. }
  346. return nil
  347. }
  348. //Get and return the file hash for a file
  349. func getFileHash(filename string) (string, error) {
  350. f, err := os.Open(filename)
  351. if err != nil {
  352. return "", err
  353. }
  354. defer f.Close()
  355. h := sha256.New()
  356. if _, err := io.Copy(h, f); err != nil {
  357. return "", err
  358. }
  359. return hex.EncodeToString(h.Sum(nil)), nil
  360. }
  361. func (m *Manager) GetTaskByBackupDiskID(backupDiskID string) (*BackupTask, error) {
  362. targetTask := m.getTaskByBackupDiskID(backupDiskID)
  363. if targetTask == nil {
  364. return nil, errors.New("Task not found")
  365. }
  366. return targetTask, nil
  367. }