HowTo

7-Zip Backup Script For Windows

Backup Windows Files and Directories with 7-Zip - For Free!

Why 7-Zip?

The main reason you would want to choose 7-Zip over a commercial package like Symantec's Backup Exec is because 7-zip is Free Open Source Software (FOSS). 7-Zip can achieve amazing compression when the native 7Z format is used. 7-Zip is very easy to script and schedule and can easily be run from a command prompt. 7-Zip allows a Sys Admin to easily include files in a backup or exclude files from a backup. Because it is FOSS, you won't ever encounter an error like "Your license has expired, please contact support." 7-Zip works on Linux, FreeBSD, Windows and Mac.

History

I received error messages about expired licenses or unlicensed tape devices too many times, so I decided to take matters into my own hands one Friday night at work because I needed to get some data backed up. Using 7-Zip, I decided to use the concept of Virtual Tape (VTAPE) and worked around the licensing issues by dumping the archives to a backup directory on my SAN. I used Windows backup to backup the VTAPE data using the "unlicensed" tape drive. Problem solved. Needless to say, the Backup Exec salesman wasn't very excited ;-)

Shouldn't I Use Tape?

Yes and no. If you are a home user, you probably don't have a tape drive handy and you probably don't want to invest in one if your searching the Internet for free backup software. As a home user, you will want to archive your 7-Zip backup files to a DVD or USB hard drive. If you are thinking about using this script at work, you can simplify your backup schedule and most likely speed up your nightly backup jobs, but you will want to dump your VTAPE data to tape on Friday at the very least. VTAPE does not protect you against corrupted data or failed disks; tapes and DVDs come in very handy after a disk failure and yes, SANs do fail.

Many companies are moving away from tape backup and moving to mirrored SAN and NAS for redundancy. At work, I use a version of this script to backup select directories at remote sites and store them on our SAN in a virtual tape (VTAPE) directory. I use Windows backup to dump the VTAPE backups to tape once a week. I will get around to setting up Bacula some day, but the solution I have now works and it works really well.

The Solution

IMHO, a daily full backup is the only way to go. Restoring from multiple sources is a pain in the butt whether you're restoring from disk or tape. You will be less likely to be missing data when you go to restore if you do a full backup every day.

This script is can be customized to your preference. It will backup the data you list in the backup-list.txt file, but won't backup data listed in theexclude-list.txt file. You can customize the naming convention any way you like and I have even included 8 different naming conventions in the script. This script will automatically rotate the backup files based on the number of days you set in the variables section.

Instructions

1.) Create two directories named

C:\bin\ and C:\bin\backup

2.) Copy the script below and save it as a .bat file under the C:\bin\backup directory you just created.

3.) Edit the backup.cmd file and customize the options as needed; such as, destination and naming convention.

4.) Edit the include and exclude files as needed.

5.) Schedule the task with Windows task manager.

That's all there is to it. You now have a self-managing backup schedule. See the sample code below. 

The Script


:: WHAT: 7-Zip backup script
:: REQUIRES: 7-zip and forfiles.exe (from the Windows 2K/23 Server resource kit)
:: LICENSE : NONE, do what you want with it
:: ----------------------------------------
@echo off
path=C:\windows;C:\windows\system32;C:\progra~1\7-zip;C:\bin;C:\bin\backup
:: Sample of command = 7z u -t7z -mx=7 F:\backup.7z @C:\backup\backup-list.txt
:: ---------------------------------------------------
:: START VARIABLES SECTION
:: THE ZIP COMMAND
set ZIP=C:\progra~1\7-zip\7z.exe a -t7z -mx=6
::
:: THE DESTINATION DIRECTORY (VIRTUAL TAPE)
set VTAPE=C:\backup
::
:: THE LOCATION OF THE LIST OF FILES YOU WANT TO BACKUP
set SOURCE=@C:\bin\backup\backup-list.txt
::
:: THE LOCATION OF THE LIST OF FILES YOU DON'T WANT TO BACKUP
set EXCLUDE=@C:\bin\backup\exclude-list.txt
::
:: THE EXTENSION FOR YOUR ARCHIVE FILE ( YOU COULD USE .BKP OR SOMETHING TO MAKE IT OBVIOUS )
set EXT=.7z
::
:: The number of days to keep your backups
set DAYS=-8
::
::
:: Choose one of the name options below, just uncomment the REM for the format you want
::
:: The command below creates a file that looks like this: 2008_11-16_1757
:: THE FORMAT IS: YEAR_MONTH_DATE_TIME
set NAME=%date:~10,4%_%date:~4,2%-%date:~7,2%_%time:~0,2%%time:~3,2%
::
:: The command below creates a file that looks like this: Sun-11-16_2008_28321
:: THE FORMAT IS: DAY-MONTH-DATE_YEAR_RANDOMNUMBER
REM set NAME=%date:~0,3%-%date:~4,2%-%date:~7,2%_%date:~10,4%_%RANDOM%
::
:: The command below creates a file that looks like this: Sun-11-16_2008_1751
:: THE FORMAT IS: DAY-MONTH-DATE_YEAR_TIME
REM set NAME=%date:~0,3%-%date:~4,2%-%date:~7,2%_%date:~10,4%_%time:~0,2%%time:~3,2%
::
:: The command below creates a file that looks like this: 2008_Sun_11-16_1756
:: THE FORMAT IS: YEAR_DAY_MONTH_DATE_TIME
REM set NAME=%date:~10,4%_%date:~0,3%_%date:~4,2%-%date:~7,2%_%time:~0,2%%time:~3,2%
::
::
:: The command below creates a file that looks like this: Backup_Sun-11-16_2008_28321
:: THE FORMAT IS: NAME_DAY-MONTH-DATE_YEAR_RANDOMNUMBER
REM set NAME=Backup_%date:~0,3%-%date:~4,2%-%date:~7,2%_%date:~10,4%_%RANDOM%
::
:: The command below creates a file that looks like this: Backup_Sun-11-16_2008_1751
:: THE FORMAT IS: NAME_DAY-MONTH-DATE_YEAR_TIME
REM set NAME=Backup_%date:~0,3%-%date:~4,2%-%date:~7,2%_%date:~10,4%_%time:~0,2%%time:~3,2%
::
:: The command below creates a file that looks like this: BACKUP_2008_Sun_11-16_1756
:: THE FORMAT IS: NAME_YEAR_DAY_MONTH_DATE_TIME
REM set NAME=BACKUP_%date:~10,4%_%date:~0,3%_%date:~4,2%-%date:~7,2%_%time:~0,2%%time:~3,2%
::
:: The command below creates a file that looks like this: BACKUP_2008_11-16_1757
:: THE FORMAT IS: NAME_YEAR_MONTH_DATE_TIME
REM set NAME=BACKUP_%date:~10,4%_%date:~4,2%-%date:~7,2%_%time:~0,2%%time:~3,2%
::
::
::END VARIABLES SECTION
::
:: ---------------------------------------------------
:: CLENUP OLD BACKUPS BEFORE WE GET STARTED
:: SET THE NUMBER OF DAYS ABOVE. EXT should be the same as your backup extension; 7z, zip, bkp, etc
echo cleaning up backups older than %DAYS% days
forfiles.exe /m *%EXT% /d %DAYS% /C "cmd /c echo @file identified for deleting" /p %VTAPE%
forfiles.exe /m *%EXT% /d %DAYS% /C "cmd /c del @file" /p %VTAPE%
::
::
:: run the backup using 7zip and appropriate switches including shameless self promotion :-)
echo ********************************************
echo *                                          *
echo * 7-zip backup script by Chris Updegrove   *
echo *                                          *
echo ********************************************
::
:: The backup command with variables
echo %ZIP% -xr%EXCLUDE% %VTAPE%\%NAME%%EXT% %SOURCE%
%ZIP% -xr%EXCLUDE% %VTAPE%\%NAME%%EXT% %SOURCE%
::
echo DONE

exit

 

Back To Articles