I thought quite a while about the best structure to arrange the backup files.
This solution is also build into my history viewer and doing very well there.
Backup files will get arranged like this:

- 7 different days will get stored
- Day1 will always be the newest backup
- backups will have at least a time difference of 24h
- a monthly backup will also be created (and never will be deleted by this procedure)
For anything else, please see comments in code:
Code: Select all
Prototype.i Proto_BackupProcedure(FileName.s)
; You need a Procedure like this:
; Procedure DoMyBackup(FileName.s)
; ;Create a backup and store it in FileName
; ;If anything worked as expected, return #True
;
; ProcedureReturn #True
; EndProcedure
;
; At the end of your program call the following Procedure like this:
; CheckForBackup("D:/MyBackups/", @DoMyBackup(), ".bak")
Procedure CheckForBackup(PathToBackup.s, BackupProcedure.Proto_BackupProcedure, Extension.s = ".zip")
;This procedure will help you handling backup files for your programs.
;It will check the existing backup files and if the newest one is older than 24h it will call BackupProcedure.
;There you should create your backup file and return #True if anything is o.k.
;Then it will start to rearrange the old backups:
;Day7 will be deleted, Day6 will be Day7, Day5 will be Day6...
;The newest backup file is allways called Day1
;It will also create monthly backup files, which never gets (automatically) erased.
Protected i = #True, D
Dim BackupFileSizes.q(6)
D = Date()
If PathToBackup And BackupProcedure
;First check if there is a slash at the end
CompilerIf #PB_Compiler_OS = #PB_OS_Windows
If Right(PathToBackup, 1) <> "\" And Right(PathToBackup, 1) <> "/"
PathToBackup + "\"
EndIf
CompilerElse
If Right(PathToBackup, 1) <> "/"
PathToBackup + "/"
EndIf
CompilerEndIf
If FileSize(PathToBackup) <> -2
;Backup directory not yet existing, so create it
i = CreateDirectory(PathToBackup)
EndIf
If i
;Check which backup files (Day1 - Day7) are already there
For i = 0 To 6
BackupFileSizes(i) = FileSize(PathToBackup + "Day" + Str(i + 1) + Extension)
Next i
If BackupFileSizes(0) = -1 Or GetFileDate(PathToBackup + "Day1" + Extension, #PB_Date_Modified) < D - 86400
;o.k. last update is older then 24h, user should create a new one
If BackupProcedure(PathToBackup + "Day1_" + Extension)
;Check if we need a monthly backup
If FileSize(PathToBackup + FormatDate("%yyyy.%mm", D) + Extension) = -1
;yes, create one.
CopyFile(PathToBackup + "Day1_" + Extension, PathToBackup + FormatDate("%yyyy.%mm", D) + Extension)
EndIf
If BackupFileSizes(0) > 0
;O.k., we have at least one, so rename all Dayx-Backups!
If BackupFileSizes(6) > 0
;we have 7 backups, now delete the oldest one
DeleteFile(PathToBackup + "Day7" + Extension)
EndIf
;rename all backup files, Day1 will be Day2, Day2 will be Day3, ...
For i = 5 To 0 Step - 1
If BackupFileSizes(i) > 0
RenameFile(PathToBackup + "Day" + Str(i + 1) + Extension, PathToBackup + "Day" + Str(i + 2) + Extension)
EndIf
Next i
;now rename the new one
RenameFile(PathToBackup + "Day1_" + Extension, PathToBackup + "Day1" + Extension)
EndIf
EndIf
EndIf
EndIf
EndIf
EndProcedure
And a tiny example (just to show you how to use it):
Code: Select all
Procedure DoMyBackup(FileName.s)
Protected FID, Result
FID = CreateFile(#PB_Any, FileName)
If FID
WriteStringN(FID, "This is an example Backupfile!")
CloseFile(FID)
Result = #True
EndIf
ProcedureReturn Result
EndProcedure
CheckForBackup("D:\", @DoMyBackup(), ".txt")
But I am adding lots of my code snippets to the templates at the moment (never used them until now D'OH) and thought this might be useful for someone.