Monday, July 6, 2009

7-Zip Command Line

7-Zip Command Line Backups from here...

The tool that I used for a long time for extracting and compressing archives is 7-Zip. It’s free, open-source, and has a powerful set of command line options that is perfect for performing backups. And today that’s exactly what we’re going to use it for.

We’re going to use a special version of 7-Zip that has no user interface whatsoever. It has to be used via the command line, and one of the benefits for using this version is that the entire application is self-contained in one single executable file. That makes it a great tool to carry around with you on a USB drive, and extremely easy to work with.

Here’s how to use the 7-Zip command line options to create backups at the click of a mouse:

  1. The first thing you’ll want to do is go download the command line version of 7-zip. After you extract the contents you’ll notice that there are five files available. All we need is the 7za.exe, so you can delete the rest if you wish.
  2. Open up Notepad, and now we’re going to create a batch file that will backup a folder location of our choosing. I’ve decided to backup everything on my desktop this time around, but it will work for any location as long as you know the path.

    7za.exe a cybernet.7z "C:\Users\Ryan\Desktop"

    Be sure to include quotation marks around any paths that have spaces in them. For this example I didn’t have to put quotation marks around my path, but it never hurts to include them.

    This will archive the documents in the 7-Zip format, which means you’ll need to have 7-Zip to extract it. It’s good because the compression ratio is very high, which means the backup file will be rather small compared to the other compression formats. If you were going to send this to someone they may have troubles opening it, but you can choose save the file as a ZIP by replacing “7z” with “zip” in that line of code.

  3. Now we need to save the batch file in the same place where the 7za.exe is located. In Notepad go to File -> Save As, and choose any filename. The only thing you need to make sure of is that at the end of the filename you include the .BAT extension:
  4. Now all you have left to do is double-click on the batch file that you just created to run it. The resulting backup file will be located in the same place as the 7za.exe and the batch file, unless you’ve specified an alternate location in Step 2.
  5. Extra Credit: You can always create a scheduled task that will run your batch file automatically at a time/date that you specify. That can be done by using the built-in Windows Scheduled Tasks wizard, which can be found in the Control Panel.

One other tip that I wanted to provide is how to automatically insert the current year, month, and day into the resulting filename. This would be useful for anyone looking to schedule backups, without wanting to overwrite the previous backup. In Step 2 from our example above you would want to replace:

cybernet.7z

with:

%DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%_cybernet.7z

which for today would give a result of:

2008-01-03_cybernet.7z

Now every backup will remain in tact, assuming that you create no more than one backup per day.

For those of you who want to dive a little deeper in to the 7-Zip command line options, here are the different things that the “a” can be replaced with in Step 2:

  • a: Add to an existing archive, or create a new one
  • d: Delete files from an archive
  • e: Extract files from an archive
  • t: Test the integrity of an archive
  • u: Update the files in an archive

Compress with another type from here...
C:\Users\A>7za a -t7z files.7z *.txt
C:\Users\A>7za a -tzip files.zip *.txt

Reference

No comments:

Post a Comment