Your Ad Here
 

Reply to comment

devans's picture

Using 7-Zip in a BAT script as part of your Backup Strategy

Your rating: None Average: 3 (2 votes)

When performing file compression most people instinctively refer to WinZip. Beginning with Microsoft Windows ME (who can forget that quality release!), the Windows Operating System included their own tool for file compression.

While these along with many others provide a means to an end they are all limited in one particular way or another. Enter 7-Zip!

While I myself have only been using 7-Zip for the past 2 to 3 years it has none the less been around since the latter part of 2001.

Don't worry I'm not going to go off on a tangent and recount the entire history of the product for you here, you can check out the 7-Zip website at http://www.7-Zip.org or their Wikipedia page at http://en.wikipedia.org/wiki/7-Zip for that juicy information.

What I am going to impart in this page is a method for using 7-Zip in conjunction with the native Windows Scheduler to perform routine compressed and if you so choose, encrypted backups of your valued data. I know there are other tools to do this, but for today we are talking about 7-Zip!

First off I should note that 7-Zip affords you a variety of supported archive formats should you wish to access the data, using Windows along with a different non-Windows platform. Supported archive formats include .zip (7-Zip using .7z as the default), .gzip, .tar, .bzip2, along with the ability to open .dmg and even .rar archives to name a handful. There are several more supported formats so if you have a particular need head over to http://www.7-zip.org and check them out.

To perform all of the steps I am about to detail you will need at the very least the Command Line version of the 7-Zip application. For many, you may prefer to install the GUI application for accessing and extracting archives; both of which can be downloaded from here: http://www.7-zip.org/download.html.

Following the simple point and click install of 7-Zip we are almost ready to begin but first we need to complete one simple step to ensure maximum availability to the OS. Either add the 7z.exe or 7za.exe application paths to your main PATH directive or copy and paste the same executable inside of the c:\Windows\System32 directory. Performing either of these steps ensures access to the 7-Zip application no matter where you start or what user is logged into the system.

So now we have a working install of 7-Zip we are ready to perform our backup(s). In the examples that now follow I am assuming that we have created .BAT files that are then executed either manually or via the Windows Scheduler. To do this create a text file and save it with a .BAT file extension in place of the default .TXT.

In this first example I am going to select a couple of basic criteria that anyone can use to backup their data.


7z.exe a c:\Backup.zip c:\www\webroot\*

Here we are telling 7-Zip to add (that is what the 'a' indicates) to the archive 'c:\Backup.zip' everything contained within the 'c:\www\webroot' directory. Don't worry if this is the first run of the script because if the archive doesn't already exist, 7-Zip will create it for you.

Pretty simply stuff!

Now we can take this example a step further and specify that we only want to include files of a specific type, for example 'html' files. This can be done as follows:


7z.exe a c:\SDS-Backup.zip c:\www\webroot\sds\*.html

Again, very simple to achieve! To use a different file type simply swap the '.html' for something else.

Okay, so now you have the basic understanding as to how an archive can be created using 7-Zip.

In backing up data we routinely require 2 or more types of backup to cover all eventualities, the most common being a daily backup and a weekly backup. To achieve the execution of these jobs we utilize the built in Windows scheduler and specify each of the .BAT files to run daily or weekly respectively.

In the creation of your weekly backups or your dailies you may want to name them in a unique format that enables the retention of multiple copies or weeks worth of archive files. This can be achieved very easily by adding something similar to the following.


7z.exe a c:\%DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%_Backup.zip c:\www\webroot\*

What this provides us with is an archive that includes the date on which the script was executed in the format of YYYY-MM-DD.

Typically I use this method in establishing my weekly backups with the daily backup overwriting the standard 'c:\Backup.zip' archive.

Assuming you are the only person accessing your system (no other security is necessary) and you are storing your archives on a shared storage array, service or other you most likely have a need to protect your backups. This can be accomplished with 7-Zip through the use of encryption. As a simple example building upon the previous examples this can be achieved as follows:


7z.exe a c:\%DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%_Backup.zip c:\www\webroot\* -pPASSWORD

In the above example we have now added '-pPASSWORD' to the end of our previous example where 'PASSWORD' is the password you have assigned to this archive.

While I have not covered the extraction of archives I assume you are all familiar with the process either in using WinZip, Windows File Compression or 7-Zip itself.

Hopefully this page has provided you with a high-level overview of some of the capabilities of the 7-Zip application and how to automate some of the more frequent activities associated with regular backups.

More more information of the various switches and syntax that can be used with 7-Zip I have included the 7-zip.chm file in the downloads section at http://www.poscribes.com/downloads and also attached to this post.

Enjoy!

In support of this post regarding the creation Backup Archives next time around we explore automating data retention and the use of Forfiles as a way of purging old archive, log and other types of data. Stay tuned!

AttachmentSizeHitsLast download
7-zip.chm86.06 KB742 days 28 min ago

Reply

The content of this field is kept private and will not be shown publicly.
 
  • Allowed HTML tags: <a><em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
Testing for human visitors.