Easy Backups With 7-Zip

After losing most of the contents of a hard drive about a year ago due to a disk failure, I’ve slowly been looking for a realiable yet easy way to backup my system. As always, I first searched out an open source solution to the problem knowing that most commercial backup applications would contain far more than I need to backup my single PC.

Windows Backup

Maybe I didn’t give it enough time and maybe I was biased right off the bat but I just didn’t think the Windows backup software built into XP was my answer. I was looking for a tool that would make it easy for me to restore a single file just as easily as an entire directory. My organization strategy for my drives has been to limit myself to as little top level drives as possible. This was done in the hopes of making it easier to select which directories I wanted to backup. Even with this done, I wasn’t happy with the fact that the XP backup utility compressed all of my files into a single file that was only readable by the backup utility. So on to the next option.

7-Zip

I few months back I stumbled upon the 7-Zip file archiver. I can’t even remember how I found it, but with its amazing compression abilities, I was hoping that I could use this little gem to run my backups.

I first attempted to compress a single directory using the 7-Zip Windows GUI application and saw quite an impessive compression rate. The use of the program to build a backup file is easy enough but I was looking for something I could easily repeat. I also wanted to have the ability to setup my backup on a schedule and backup multiple directories at a time.

7-Zip Command Line

Another feature of 7-Zip that attracted me was the fact that all of its features can be accessed from the windows command line. This meant that I was able to create a windows batch script to setup an easily repeatable backup process. Using another feature of 7-Zip, I created a file with a list of directories I wanted to backup. Once I setup my backup script, I only needed to modify my directory list to add new files to my backup.

Setting up Your Backup System

First, head off to the 7-Zip download page and grab the appropriate version for your OS. There are build for a variety of operating systems in addition to Windows including OS X, various flavors of linux and even BeOS.

After installing 7-Zip, add your install directory to your Windows path so that you can execute the 7z application from anywhere on the command line. For example, I added C:/Program Files/7-Zip to my system path.

Next, create a new file called 7ZipBackup.txt. In this file, list the full path to as many directories as you want, each on a new line. Here’s an examples of a file:

c:/docs/work
c:/personal/documents

Next, create another file called 7ZipBackup.bat. This is the script that will execute your backup. Here are the contents of my backup script file:

7z a c:/backup.7z @c:/7ZipBackup.txt

This command will execute the 7z application with the a command. This is used to add files to an archive. The first file path is the new backup file that will be created. The 7z format offers the best possible compression. You can see the detailed stats on compression ratio on the 7-Zip site. The second file path is to your file that contains a listing of directories and/or files to backup. You can read more about the appropriate syntax in the Windows help file once you install 7-Zip.

Running Your Backup

Once you’ve setup the previous 2 files, just double click your .bat file to execute it. Depending on how many files you have, this could take a while. I was able to compress 350MB worth of files into a 190MB archive in about 5 minutes. If you double click on your new backup file the 7-Zip GUI will fire up and you can browse through your new archive. If you have the need, you can now restore anything from a single file up the the entire archvie.

Scheduling Your Backup

The final step in the process is to schedule your backup to run automatically. Select the Schedule Tasks option from the Windows Control Panel and add a new scheduled task. When prompted to select the application, browser to the .bat script that you create earlier. Continue by selecting how often you want to execute your backup script and save the task. Voila! You’re done.

The Most Important Step

Now that you’ve gone to the trouble of creating a backup, the most important step is to move it off the drive you’re backing up. Whether its to an external drive, a USB key, or some other medium, you can either manually complete this step or add it to the batch script you’ve already created. Once you’ve copied your backup file off, you’re good to go. Just repeat this process as often as you feel necessary and you’ll never have to worry about losing your data again.

Comments

22. June 2005

bq. Windows Backup Even with this done, I wasn’t happy with the fact that the XP backup utility compressed all of my files into a single file that was only readable by the backup utility.

Hmm, fine, but you actually still have everything in one file readible only by the original application, don’t you?

22. June 2005

Sure, if you choose 7z compression you have to use 7-Zip to open it. But you can also choose any of the other compression formats that it supports (ZIP, CAB, RAR, ARJ, GZIP, BZIP2, Z, TAR, CPIO, RPM or DEB) and then use any number of applications to uncompress your backup. 7z just offers the best compression ratio.

Rob

30. June 2005

Pretty Good article that seems to expand more on something I read a few days back on Digital Web:

Digital Web – Web Designer’s FREElance Toolbox for Windows

I use this all the time…for both home and work (when I need to bring stuff back and forth).

Terry Apodaca

27. September 2005

Because I use a dual-boot computer with both WinXP and Linux I have been using rsync on the linux side to perform regular backups. Rsync also can read from a text file of directories.

Now I need to see if I can do this from a Win XP version of rsync as I’m more and more using Windows. Will need to give the 7Zip a looksee – thanks for the tip!

19. October 2005

What about this
7z u c:/backup.7z @c:/7ZipBackup.txt

Using u option, it’s only adding new files or updated files.

Art Saisuphaluck

Art Saisuphaluck

23. November 2005

Nice. How do you delete files from the archive that does not exists in the backup dirs?

Kim Lund Johansen

1. December 2005

To remove your cleared files you have to do something like this:
c:programme\7-zip\7z.exe a c:\tempArchive c:\temp\* -uq3y1

7zip is realy fine.
But how can you keep versions of files in your Backup?

Drilling

19. January 2006

@Drilling
> But how can you keep versions of files in your Backup?

By using the -u switch with the u command.

7. April 2006

How do I zip the single file by follow the filename that i would like to archieve.

For example, MyDoc20060401.doc to MyDoc20060401.7z

The document will create it everyday follow by the date, so we want it zip by follow the filename.

Can this be done in 7zip?

Raymond Tan

2. May 2006

That seems a great solution.

The only problem I see is that manually adding all directories to a text file is a pain. Is there anyone of doing this automatically?

Regards,
Simao

4. June 2006

This is great. Any idea how to archive only files with the archive bit set?

23. June 2006

nice write-up…

Amit Burmee

6. July 2006

this method doesnt work anymore ?? y so ?

jackson

11. July 2006

And how to move files into a zip? That is, delete the source files when they are zipped.

Jimbo

14. July 2006

Thanks for your work for the writeup!

Here is my script for this backup method. I was in the same boat about using ntbackup… only my issue was it did NOT compress, otherwise it did what I wanted it to.

Here is my script, if you improve on it, please send me a copy to jp@jptechnical.com. It is for use on external HDs and thumb drives. It gets the drive path and puts it into a variable and executes everything from there. Just put the 7za.exe (the standalone commandline version available from the 7zip site) and the dirs.txt in the \bin folder (I put the entire 7zip dir in there as well so I can read the .7z files from the drive without installers) and save the script to \backup.bat.

Saves an archive with the abbreviation for today in the file name, that is the “date:~0,3” if you want the full date substitue with “date:~4,2_%date:~7,2%_%date:~10,4%”

(5 line numbers added in case this wraps on you… remove them in the final script)

1.set BACKUPDRIVE=%cd%
2.echo Backup Started on date%time% > ”%BACKUPDRIVE%backup_%date:~0,3%.log”
3.%BACKUPDRIVE%bin\7za a -ms=off -w%temp% ”%BACKUPDRIVE%backup_%date:~0,3%.7z” @%BACKUPDRIVE%bin\dirs.txt >> ”%BACKUPDRIVE%backup_%date:~0,3%.log”
4.echo Backup Completed on date%time% >> ”%BACKUPDRIVE%backup_%date:~0,3%.log”
5.pause *** Please review the backup_%date:~0,3%.log logfile ***

JP

12. August 2006

WOW, 7zip rocks

I have been playing with it to script for my various machines and this is what I made

My parameters are like this

– set the backup.bat – closing a few running task – set a password with the -p… switch – copy the 7-zip to the bin folder (the one that was in Program Files after the insatll) – set the dirs.txt for the list of files to backup – Created a task every night at 4 am with a script user account – set a cleanup.bat for older backups, see delete.bat below – set a task to burn the backup.zip every week with a simple burn.bat, see burn.bat below

And then, the rest is up to you
___________________________________
backup.bat:

@ECHO OFF
CLS
set include=J:/backups/DAILY/
cscript HOMEDRIVE/bin/scripts/closeoutlook.vbs
start /wait HOMEDRIVE/bin/scripts/KILLoutlook.bat
start /wait HOMEDRIVE/bin/scripts/KILLQB.bat
start /wait HOMEDRIVE/bin/scripts/KILLWINWORD.bat
echo Backup Started on date time >> HOMEDRIVE/bin/log/backup_%date:~4,2%_%date:~7,2%_%date:~10,4%.txt
HOMEDRIVE/bin/backup/7-Zip/7z.exe a -ppasswordhere -ms=off -w%temp% include/backup_%date:~4,2%_%date:~7,2%_%date:~10,4%.ZIP @%HOMEDRIVE%/bin/backup/dirs.txt >> HOMEDRIVE/bin/log/backup_%date:~4,2%_%date:~7,2%_%date:~10,4%.txt
echo Backup Completed on date time >> HOMEDRIVE/bin/log/backup_%date:~4,2%_%date:~7,2%_%date:~10,4%.txt
___________________________________
delete.bat:

ECHO 31 is how old you want to keep the files

@ECHO OFF
CLS

set include=J:\backups\DAILY

cscript deleteoldfiles.vbs include 31 > HOMEDRIVE/bin/log/deleted_files_%date:~4,2%_%date:~7,2%_%date:~10,4%.txt

PS you need this script too

deleteoldfiles.vbs:

’ Objective: To delete old files from a given folder and all subfolders below
’ Created by: MAK
’ Created Date: June 21, 2005
’ Modified by: XANDER
’ Modified Date: August 10, 2006

Set objArgs = WScript.Arguments
FolderName =objArgs(0)
Days=objArgs(1)

set fso = createobject(“scripting.filesystemobject”)
set folders = fso.getfolder(FolderName)
datetoday = now()
newdate = dateadd(“d”, Days*-1, datetoday)
wscript.echo “Today ” & now()
wscript.echo “”
wscript.echo “Started time of cleanup of files older than ” & newdate
wscript.echo “”
recurse folders
wscript.echo “”
wscript.echo “”
wscript.echo “Completed the cleanup of files older than ” & newdate
wscript.echo “”

sub recurse( byref folders)

set subfolders = folders.subfolders set files = folders.files wscript.echo “” wscript.echo “List of removed files under: ” & folders.path wscript.echo “” for each file in files if file.datelastmodified < newdate then wscript.echo “Deleting ” & folders.path & ”\” & file.name & ” last modified: ” & file.datelastmodified on error resume next file.delete end if next for each folder in subfolders recurse folder next set subfolders = nothing set files = nothing

end sub
___________________________________
burn.bat:

ECHO OFF
CLS
ECHO. This will SEND your files to be BURN
ECHO. Please insert a Writable CD or DVD in the DRIVE and wait 20 seconds
ECHO.
Pause
ECHO the * before and after the date will take the today files with the extension that you would have set in the backup.bat
ECHO.
Copy J:\backups\DAILY\*%date:~4,2%_%date:~7,2%_%date:~10,4%.* HOMEPROFILE\”Local Settings”\”Application Data”\Microsoft\”CD Burning”
ECHO..
ECHO Your Files are ready to be written unto a CD/DVD
Pause
EXIT

Have fun and if you want to complement the script, feel free to post it

Xander Mcguire

24. August 2006

I have reduced the script and add an exclude forlder and it will email me the log…

1. backup.bat

@ECHO OFF
CLS
set include=J:/backups/DAILY
echo Backup Started on date time >> HOMEDRIVE/bin/log/backup_%date:~4,2%_%date:~7,2%_%date:~10,4%.txt
HOMEDRIVE/bin/backup/7-Zip/7z.exe a -pPASSWORD -ms=off -w%temp% include/backup_%date:~4,2%_%date:~7,2%_%date:~10,4%.ZIP <span>HOMEDRIVE</span>/bin/backup/dirs.txt -xHOMEDRIVE/bin/backup/excludes.txt >> HOMEDRIVE/bin/log/backup_%date:~4,2%_%date:~7,2%_%date:~10,4%.txt
echo Backup Completed on date time >> HOMEDRIVE/bin/log/backup_%date:~4,2%_%date:~7,2%_%date:~10,4%.txt

REM the is an exclude file for folder that I dont want to take

2. email the log based on the error level

@ECHO OFF
CLS

REM ERRORLEVEL 0 = there is a value found
REM ERRORLEVEL 1 = there is no value found

DEL D:\bin\log\results.txt
:1
FINDSTR “WARNING: ERROR: ” D:\bin\log\*_%date:~4,2%_%date:~7,2%_%date:~10,4%.txt >> D:\bin\log\results.txt
IF ERRORLEVEL==0 GOTO :ERROR
IF ERRORLEVEL==1 GOTO :2

:2
FINDSTR /C:”Everything is Ok” D:\bin\log\*_%date:~4,2%_%date:~7,2%_%date:~10,4%.txt >> D:\bin\log\results.txt
IF ERRORLEVEL==0 GOTO :OK
IF ERRORLEVEL==1 GOTO :END

:OK
ECHO The Backup was completed with an—OK—value >> D:\bin\log\results.txt
GOTO EMAIL_OK

:ERROR
ECHO The backup was completed with an—ERROR—value, please investigate the issue >> D:\bin\log\results.txt
GOTO EMAIL_ERROR

:EMAIL_OK
febootimail -to you@yours.com -from backup@yours.com -smtp smtp.isp.com -msg backup logs -subj—ERROR—BACKUP -ATTACH INCLUDE/error_logs_%date:~4,2%_%date:~7,2%_%date:~10,4%.ZIP -auth auto -user email@isp.com -pass password -LOG d:\bin\log\email.log
GOTO END

:EMAIL_ERROR
REM This will email a OK status or an ERROR status with the log for trouble shooting

set include=J:/backups/DAILY/log

HOMEDRIVE/bin/backup/7-Zip/7z.exe a include/error_logs_%date:~4,2%_%date:~7,2%_%date:~10,4%.ZIP HOMEDRIVE/bin/log/*_%date:~4,2%_%date:~7,2%_%date:~10,4%.txt

febootimail -to you@yours.com -from backup@yours.com -smtp smtp.isp.com -msg backup logs -subj—ERROR—BACKUP -ATTACH INCLUDE/error_logs_%date:~4,2%_%date:~7,2%_%date:~10,4%.ZIP -auth auto -user email@isp.com -pass password -LOG d:\bin\log\email.log
GOTO END
del J:/backups/DAILY/log/*.*
:END
ECHO DONE >> D:\bin\log\results.txt
DEL D:\bin\log\results.txt
EXIT

With this I use febootimail, works really great and have great support for troubleshooting

PS I noticed that there could be a problem with the posting on this site with the % word %, so if you see the word INCLUDE and HOMEDRIVE, they have % on both side

Xander Mcguire

9. May 2007

This was exactly what I was looking for… I modified my script to use 7zG instead, though, for a graphical window instead of the command line, but I guess it’s just a matter of personal preference. Thanks so much for the info!!!

paul4ta

9. November 2007

Your Backup-System works. However, it is NOT practical. First: you get what you did not want: one file.
Second: If you do not need encryption on your external HD, you should NOT use 7-Zip.
Third: For compression you could set the file system compressed.

The task: backup 2 folders with many files, also many jpgs. 3 GB.

7-Zip: 7z u -pSecret D:\archive.7z @C:\7ZipBackup.txt versus
xcopy /D /E /V /Y C:\Folder1\*.* D:\Folder1\*.* (same for Folder2)

First a complete backup, then addition of 1 new file and “update backup”

Result in minutes:
—————7z —- xcopy
Complete Backup 29’ 5’
Update Backup 9’ 1s(!)
==============================

Conclusion: if you don’t need encryption you should prefer xcopy over 7zip for backup purposes. Most users don’t need encryption at home but need to secure their valuable data.
Encryption rises other problems and makes you dependent on another piece of software.
(Nevertheless 7zip is excellent and probably the coming compression standard.)

-Mino

Minotaurus007

16. November 2007

Adding the arugment -mx=7 will result in smaller compressed files at the cost of a lot more memory usage and a little bit slower speed. -mx=9 is also available, but this setting requires over 512M of ram for a single instance.

26. December 2007

Hi!

If you have enough space on your backup drive, you should make two batch files, one for backup1.7z and one for backup2.7z -> If you have an brownout, computer failure or whatever, at least one backup remains working and not all data is corrupt…

Chri

12. March 2008

eh why not use something like combain backup. it supports zip and sqx compression. it has gui and can be scheduled to run in different modes. eg. full, incremental, differential.

eep

6. April 2008

Hi guys, just wanted to say thanks for a very educational article, and moreso comments, awesome, you answered all the questions i was asking myself!!

19. August 2008

Timestamp it!
source: http://swamproot.blogspot.com/2007/04/howto-create-timestamp-in-bat-file.html

for /F “tokens=1-4 delims=/ “ %%i IN (‘date /t’) DO (
set DT_DAY=%%i
set DT_MM=%%j
set DT_DD=%%k
SET DT_YYYY=%%l)
7z a c:/”DT_YYYY-%DT_MM%-%DT_DD%-“backup.7z @c:/backup.txt

nicholas.alipaz

18. September 2008

WinRAR with the “m” option could move files to an arhcive (or in other words, delete the original files after a successful compression). Is there any command line option for this in 7zip, or does it have to be scripted?

24. October 2008

I’d love to be able to delete the original file/folder after a successful compression as well. Is there a way to do that?

3. November 2008

I’d like to use 7-zip as rar to backup my files but i cannot find an equivalent option for rar -ver5…
Any ideas?

unknown user

Commenting is closed for this article.

Textpattern Solutions

Textpattern Solutions

Textpattern Solutions is the first book published on Textpattern.

details at friends of ED or the official book website.

buy it at amazon.com