Rotating Backups

This article is a follow-on to ZIP Up Files from the Command Line. In that article, I showed you how to program a batch file to make a quick backup of your most critical files, storing the backups in a ZIP file (or whatever type of archive file you prefer). The example I gave maintains a single ZIP archive, that is constantly updated every time you run the batch file. Instead, you may prefer to have the batch file retain distinct copies of the backups going back a day or two.

Here is where we left off. (Note: this is a Windows batch file example, but the same principal could easily apply to a Linux shell script. If anyone needs help translating, just post a comment.)

C:
CD backups
7z.exe u -tzip research.zip C:research -r

This batch file maintains a backup archive (research.zip) in the C:\backups folder. The “u” command tells 7-Zip to update the zip file, replacing any files that already exist within the zip file, and adding new ones.

This is slightly risky, however. For one thing, if something were to happen in the middle of the backup process, the zip file could become corrupted (albeit a tiny risk, barely worth mentioning). Another, more substantial risk, is that you could be backing up bad data. If one of your files is corrupted on Tuesday, but you don’t notice the corruption until Thursday, after having made a backup on Wednesday, then the Wednesday backup will be of a corrupted file. (Note: Here, when I say “corrupted,” I don’t necessarily mean as a result of a software glitch. It could also be due to human error.) Therefore, it’s a good idea to use a scheme that keeps older backups lying around for a certain period of time.

A Poor Man’s Backup Rotation: Here is a simple method for keeping the one prior backup on hand. First, insert a REN command into the batch file before calling the archiver.

C:
CD backups
REN research.zip research_latest.zip
7z.exe a -tzip research.zip C:research -r

This tells DOS to rename the existing research.zip archive, as research_latest.zip. (If no such file exists, then nothing happens.) Also, note that we changed the 7z command from “u” (update) to “a” (add). Actually, “u” would still work just as well, but changing it to “a” demonstrates more clearly that we expect to be creating a new archive every time now (for anyone who looks over the code of this batch file in the future).

One problem, though, is that the second time we run this batch file, a research_latest.zip file will already exist, and the REN command won’t like that. So, we also need to insert a DEL command into the batch file, to delete that (very) old archive, before we can rename the current archive as the “new old one.”

C:
CD backups
DEL /F research_latest.zip
REN research.zip research_latest.zip
7z.exe a -tzip research.zip C:research -r

(The /F switch tells DOS to force-delete the file, even if the file was set to read-only.)

Maintaining a Longer History: Going a step further, we could enhance the batch file to maintain more than one prior backup.

C:
CD backups
DEL /F research_oldest.zip
REN research_older.zip research_oldest.zip
REN research_latest.zip research_older.zip
REN research.zip research_latest.zip
7z.exe a -tzip research.zip C:research -r

This will rotate the backups through a sequence of (current) -> latest -> older -> oldest. Notice how we only need one DEL command to kick off the cascade.

If your batch file creates several different archive files, and you want to use this rotation scheme on all of them, you have a couple of choices. The most obvious is to merely copy and paste the sequence of commands, and then do a search and replace on the base filename.

C:
CD backups

DEL /F research_oldest.zip
REN research_older.zip research_oldest.zip
REN research_latest.zip research_older.zip
REN research.zip research_latest.zip
7z.exe a -tzip research.zip C:research -r

DEL /F papers_oldest.zip
REN papers_older.zip papers_oldest.zip
REN papers_latest.zip papers_older.zip
REN papers.zip papers_latest.zip
7z.exe a -tzip papers.zip C:research -r

A second approach would be to consolidate everything into a single backup archive.

C:
CD backups
DEL /F all_files_oldest.zip
REN all_files_older.zip all_files_oldest.zip
REN all_files_latest.zip all_files_older.zip
REN all_files.zip all_files_latest.zip
7z.exe a -tzip all_files.zip C:research C:papers -r

As you can see, the 7-Zip “a” command can accept multiple source file specifications. You could also repeat the 7z.exe line, so that the 7-Zip adds files to the existing archive.

...
7z.exe a -tzip all_files.zip C:research  -r
7z.exe a -tzip all_files.zip C:papers -r

(Again, you may find that to be a more expressive way of writing the batch file.)

It is also possible to write a “sub” batch file (e.g. rotate.bat) that dynamically rotates any archive file of an arbitrary name. I will write up the details for that in a future article, but just to give you an idea, it would make your main batch file look something like this:

C:
CD backups

CALL rotate.bat research
7z.exe a -tzip research.zip C:research -r

CALL rotate.bat papers
7z.exe a -tzip papers.zip C:research -r

Post a Comment


Your email is never published nor shared. Required fields are marked *



© 2006-2007 Maxim Software Corp.  All rights reserved.