Backing up data on a Windows machine using TAR and GZIP.

© copyright 01.Sep.2004 by Paul Bradley filed under TAR


[Ad] need a break from coding?

Hoseasons Villas

Introduction

Tar and Gzip are command line utilities usually associated with Unix systems, so why would you want to backup the data on your Windows machine with these tools.

Well there are several reasons why you might want to choose a command line utility as part of your backup strategy, the first being that you don't need to install a Windows program onto a new machine to restore your files. You can simply insert your CD-ROM containing your latest backup (and a copy of the command line utilities) and restore the files you need in a matter of seconds.

Beside being able to quickly restore files onto a new Windows machine, the files created by Tar and Gzip are cross platform compatible, so you can also use the Unix versions of these tools to restore the files onto a Unix machine, for example your web server.

The resulting .TGZ files are also compatible with Winzip, which would give you a visual front-end to your backups if you require it.

My strategy is to create a full backup of my data files at the beginning of each month, and burn the backup file onto CD-ROM, along with a copy of the tar.exe and gzip.exe utilities. Then throughout the month I use a batch file to create an incremental backup, only backing up files which have changed since the full backup date. The incremental backups are then kept on an encrypted secure pen drive.

Creating a Full Backup

You can create a simple batch file which calls both the tar and gzip programs in turn to create a compressed backup file, which contains all the data files in your specified directory.

Example1.bat

tar -cv --file=backup.tar d:/data*
gzip -9 < backup.tar > backup.tar.tgz
del backup.tar

Line 1 of the Example1.bat shown above, demonstrates creating a tar file called backup.tar which will contain all the files and sub directories under d:/data you will need to substitute d:/data with your own drive and directory name. The second line shows how to compress the backup.tar using gzip. The -9 command instructs gzip to use the maximum compression and output the compressed file to a new file named backup.tar.tgz. The last line of the batch file deletes the backup.tar file so that you are only left with the compressed version of the backup backup.tar.tgz.

Backing up More Than One Directory

You can set-up the batch file to backup multiple directory / folder tree structures. Example2.bat shown below demonstrates how this is achieved. You create the initial backup.tar file for your first directory using the -cv command. Then for every other directory structure, you use the -rv command to append the new directory / folder to the backup.tar file.

Example2.bat

tar -cv --file=backup.tar d:/data*

tar -rv --file=backup.tar d:/websites*

gzip -9 < backup.tar > backup.tar.tgz
del backup.tar

Creating an Incremental Backup

Creating an incremental backup is very easy, you just need to specify a date to the --newer command. Tar will then only include files which have changed since that date. The Example3.bat batch file shown below demonstrates creating an incremental backup for all files in the d:/data directory which have been modified since the 1st of December 2006.

Example3.bat

tar -cv --newer=2006-12-01 --file=incremental.tar d:/data*
gzip -9 < incremental.tar > incremental.tar.tgz
del incremental.tar

Additional References

 


If you have found this article helpful or useful please consider linking to it, emailing it to friends, or share it with others using social sites like del.icio.us, Stumble Upon or Twitter.

Paul Bradley

About the Author
Paul Bradley is a VB.NET software developer living and working in Cumbria. He provides PHP & MySQL bespoke development services via his software development company, Carlisle Software Limited.
He has over 20 years programming experience.

Other Popular Articles

Categories & Topics

Home · Apache · JavaScript · Perl · PDF · PHP · MySQL · MSSQL · TAR · Ubuntu Linux · Video · Visual Basic

Browse the complete article history, and if you like what you see; consider subscribing to the rss feed.