GLBasic forum

Codesnippets => Macros => Topic started by: Moru on 2010-Jan-22

Title: Backups - keep x backups of your code
Post by: Moru on 2010-Jan-22
This is for you, Bigsofty :-)

Keeps backups to as many generations as you want. Just change the variable "backups" to how many backups you want. I hope this works for you too, seems to work well enough for me so far but isn't overly tested since it was a 30 min job before bed... :-)

It will create a subdirectory in your project folder called "backups" and in this one there will be even more subfolders, as many as you specify in the variable. The highest number will always be filled with the latest backup, all others move one step down for each backup-run. 5 becomes 4 and so on. It's done with the move command so should be almost instant no matter how many files are in those folders.

Put this on a macro and call it before every compile. Would be handy with a macro that always runs automaticly before and after a compile. I thought there was such a thing but I can't find it?

Code: (glbasic) [Select]
:: Manage backups
:: By Moru
:: ---------------------------

:: How many backups do you want:
SET backups=7

:: Create the folders
FOR /l %%x IN (1, 1, %backups%) DO IF NOT EXIST "%GLB_PROJ_PATH%\backups\%%x" MKDIR "%GLB_PROJ_PATH%\backups\%%x"

:: Remove first
RMDIR /S "%GLB_PROJ_PATH%\backups\1" /Q

:: Rename the rest to replace the first moving the backups one point down the ladder
FOR /l %%x IN (2, 1, %backups%) DO CALL :do_move %%x

:: Create the last folder again
IF NOT EXIST "%GLB_PROJ_PATH%\backups\%backups%" MKDIR "%GLB_PROJ_PATH%\backups\%backups%"

:: Copy all sourcefiles to this last folder
:: change .gbas to "*" if you want backup of all files, not just the .gbas
:: add /S to copy subfolders too (this will make it a lot slower but keep backup of the exe-file and all media too)
:: Add /A to only copy changed files. I don't recommend this since it will move out the file from the backup-set sooner or later and there won't be more backups of this file)
XCOPY "%GLB_PROJ_PATH%\*.gbas" "%GLB_PROJ_PATH%\backups\%backups%" /Q


:: -------------------------
:: Do the moving of a folder
:: -------------------------

    :: The ID of the target folder (move each folder down one step, 2 becomes 1)
    SET /a ny=%1-1

    MOVE "%GLB_PROJ_PATH%\backups\%1" "%GLB_PROJ_PATH%\backups\%ny%"

Title: Re: Backups - keep x backups of your code
Post by: doimus on 2010-Jan-23
Great stuff!

One thing I would advise is going against this "ladder" system as it only adds confusion: "Was this feature in backup 1 or 2, or maybe 5?"

Just stamp year_date_hour data to folder name and it'll be perfect. That way you can precisely find something you backed up yesterday at 23:11...

BTW, these macros (I've never actually used those buttons in the IDE  :-[) - they are Windows batch scrips, right? Certainly look so.
Title: Re: Backups - keep x backups of your code
Post by: bigsofty on 2010-Jan-23
Very impressive Moru, thank you!   :good:
Title: Re: Backups - keep x backups of your code
Post by: Moru on 2010-Jan-23
Yes they are normal windows .bat files. The reason they are numbered is because Bigsofty wanted it to automaticly keep a set number of backups and I don't know how to delete the oldest one so I set numbers instead and always delete #1.

Script is easy to change into a date & timestamp instead if that is wanted.