free-backup.png (7632 bytes)

Home
Back

DelayCMD
MKDate
MailCMD

7-zip

 


How to Write Your Own Backup Software

It is sometimes preferable to adopt a "do it yourself" approach to solving particular problems. Having looked at several backup packages I never really found one with was both 1) free and 2) able to do exactly what I wanted it to.

Fortunately the judicious use of some software utilities I have already written together with the excellent and free ZIP-ping package "7-Zip" cab provide a useful and fully-functional backup package capable of automatically backing up either development code or a small website.


What You WIll Need

You will need to obtain copies of the following free software:

  • A copy of 7-Zip from www.7-zip.org to compress the data into ZIP-compatible archives
  • Any scheduler including Windows Scheduler, the free one which came with older copies of Traveling Software's Laplink.
    Alternatively you can use delaycmd although it may be a little too basic for all but dedicated server use
  • You can also launch the backup manually from the console
  • MKDate to help you to create date and time related directories and control date/time related operations on your PC.
  • My MailCMD application - if you want to receive email notifications, say from an automated process
  • If you want to have a local mail server collect your notifications you can use MailQ
  • A free unzipping utility for restoration such as 7-zip

How It Works

Task Scheduler--> Driver Batch File --> mkdate.exe --> Slave Batch File--> Zip-Utility--> ZIP Archive

Your scheduler kicks off a "driver" batch file which calls MKDate to set an environment variable with the current date and time. This, in turn needs to call a child or "stub" batch file since any EXE files called by the parent won't inherit the new environment table and won't see the new date. This batch file calls say 7Zip which, using a configured script file then produces the final ZIP archive. All of this can be done automatically at periodic intervals.

A limitation with the current version of 7-zip means it cannot use absolute paths (those which include drive letters). This means that if you want to source data from separate disk drives your batch file needs to make a copy of it on a single drive or you need to schedule two backups - one for each drive. I simply copy over a few config and log files to the one drive and that strategy works just fine. Future versions of 7-zip should rectify this problem.


Scheduler

You can use the Laplink Scheduler to set up an item, DELAYCMD or use the Windows 2000/XP "AT" command.


Driver Batch File

The process is "launched" using this batch file. It copies over any config files etc. from other drive letters to work around the limitations of 7-zip then launches MKDATE which performs date-related operations, sets environment variables and then calls the "Engine" batch file with the environment set. Note that it has to be done this way since a program cannot modify the parent environment table within a "DOS" session in Windows 2000/XP. Only child processes can inherit any "set" variables. This applies even if you change the master environment table - it fails because the current table isn't changed and any child processes inherit the same table. Catch 22!!. What works fine in DOS (since you can change the parent environment) doesn't work in 2000 or XP!.

@echo off
REM //////////////////////////////////////////////////////////////////
REM File:   _backup.bat
REM Deal with 7zip not handling absolute paths (which means it can only refer to one drive)
REM //////////////////////////////////////////////////////////////////
REM
REM Make backup directory
MD d:\backup\configs
REM Copy configs over from C to D
ECHO Backing up server configuration to D:\backup\configs...
XCOPY "c:\program files\apache group\apache2\conf\*.conf" d:\backup\configs\conf\ /y /c /s
ECHO Starting backup of all required files ...
REM Create directory under d:\backups with current date/time and run Engine Batch File.
mkdate.exe d:\backup /mkdir /run=d:\_backup.bat /title:Weekly Backup

Alternative example MKDATE command. This keeps the backup (ZIP) window open on completion so you can inspect the results. Not suited to remote/unmonitored server use. Other date formats can be specified with MKDATE but the deffault is usually the best for backups - this creates folders in the format YYYY-MM-DD@HH.MM.SS.

MKDATE d:\backup /md /run=c:\bats\_vbbackup.bat /keep /p "/title=VB Weekly Backup"


Slave Batch File

@echo off
REM //////////////////////////////////////////////////////////////////
REM File:   _backup.bat
REM This file must be run in the ROOT directory and must be
REM called indirectly from mkdate.exe in order to set the environment
REM //////////////////////////////////////////////////////////////////
REM
ECHO Backing up HTM, HTML and JS ...
REM cd \webroot
C:
CD \
REM Attempt to recreate each time - doesn't matter if it fails after first time
MD \_backup > nul
CD \_backup
REM %mkdate% and %mkname% are provided by MKDATE.EXE
ECHO Backing up to %mkdate%\%mkname%-html.zip ...
C:\_backup\7za.exe a -r -tzip %mkdate%\%mkname%-html.zip @c:\_backup\backup.lst
ECHO Sending SMTP email...
IF exist %mkdate%\%mkname%-html.zip mailcmd.exe smtp.yourisp.com _
servername-from@yourisp.com your-email@yourisp.com _
"Backup successful on $date at $time" "/name=Daily Archiver" _
/log /r=10
  • Note that the last line isn't split on more than one line!   The red "_" character indicates where the line continues...

7-ZIP Control File List

BACKUP.LST - This is specific to 7-zip but may also be needed by any other ZIP utility which can use command line options. Again, 7-zip prefers relative paths (those which use "..\"  rather than "\")

..\_backup\configs\*.*

..\www\site1\*.htm
..\www\site1\*.html 
..\www\site1\*.js
..\www\site1\*.php

..\www\site2\*.htm
..\www\site2\*.html
..\www\site2\*.js 
..\www\site2\*.php

..\www\site3\*.htm
..\www\site3\*.html
..\www\site3\*.js 
..\www\site3\*.php

The Result

You should end up with a regular sequence of backup directories as follows.

C:\>dir \_backup

Directory of C:\_backup

02/01/2025 03:00 <DIR> 2025-01-02@03.00.04
05/01/2025 06:00 <DIR> 2025-01-05@06.00.06
07/01/2025 02:30 <DIR> 2025-01-07@02.30.05
09/01/2025 03:00 <DIR> 2025-01-09@03.00.04
12/01/2025 06:00 <DIR> 2025-01-12@06.00.06
14/01/2025 02:30 <DIR> 2025-01-14@02.30.05
16/01/2025 03:00 <DIR> 2025-01-16@03.00.05

Restoring Files

Just open the relevant ZIP file and choose to restore either all or part to the original or new location. I make it a matter of rule NEVER to restore to original locaitons and always restore to a "\_Restored" directory in case a mistake is made during the unzipping operation!.


This page and all software, unless otherwise stated, is Copyright (c) M Shaw 
Last updated on 22 February 2021 - This page is designed for 1024 x760 and higher resolution displays