Feb 25, 2011

How To Password Protect Zip/Tar Archive Via Script in Linux

So the reason I am writing this post is because I was searching for something very similar myself recently. I recently setup a password server at my office, and wanted an easy way to back it up in case I ever had to restore it. We use Microsoft DPM at my company, and it doesn’t really back up Linux, so I needed a way to copy the configs and such for my server over to a Windows box to be backed up to tape.

I decided I wanted to compress the install directory for my program into an archive, then copy over to a Windows file server. Since it contained passwords, even though they are stored using AES encryption, I wanted to password protect the archive for another layer of security. Normally for Linux I like to create tar.gz files, however no matter where I searched, I couldn’t find an article on how to password protect it. I found a lot of people encrypting their tarballs with GPG, but that wasn’t what I wanted.

7zipNext I thought about using zip, so I installed zip in my Linux server. Zip has an –e switch for password protecting, but it prompts you. I couldn’t figure out a good way to script that. Then I sat back and had an aha moment. Why not use 7zip?

I wrote a few months ago about how easy it is to use 7zip for a backup program in Windows. Well, 7zip is available for Linux too! To install it run the following:

sudo apt-get install p7zip-full

Once installed, I used the following script to create a 7z archive of my directory with password protection!


NOW=$(date +"%b-%d-%y")
7za a "/path/to/archive/DIRECTORYNAME-$NOW.7z" "/path/to/directory/to/backup" -pYOURPASSWORD

If you don’t speak shell, what I’ve done above is created a variable called NOW that puts the date in a MM-DD-YY format. I do that so I can time stamp the filename of my archive. I do that by adding the $NOW in the archive name. Make sure to change the above to fit your environment. Also, make a note that there is NO space between the –p switch and the password.

There you have it, it will now create a 7z archive that is password protected for somewhat secure transportation and/or offsite backup!

Do you do something different to password protect archives on your Linux server? Let us know in the comments!

Twitter Delicious Facebook Digg Stumbleupon Favorites More

Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | stopping spam