Friday, January 4, 2013

Using command line to compress files using 7zip

In my day work I had to compress the database backup file to an external storage using 7zip. I did this by creating a batch file E.g. Compress.bat and inside it using the 7zip executable to compress the backup file

"C:\Program Files\7-Zip\7z.exe" a -t7Z -m0=LZMA2 -mx9 -mmt10 "E:\backup.7z" "C:\Database\productionbackup.bak"

This assumes that your 7z.exe is installed in C:\Program Files directory. The -t7Z means that you want the destination file to be in 7zip format. The -m0=LZMA2 means that the compression algorithm to be used is LZMA2. The mx9 switch specifies that you want Ultra compression ratio.

The mmt10 switch specifies that you want to use 10 processor core for the compression. PLEASE BE AWARE that you should specify -mmt5 if you want to use 5 out of the 10 cores available to avoid overloading the processor for the batch job.

The next parameter is the destination file name, after that would be the source file name.

With the batch file then you can schedule a scheduled task in Windows server for compression of the backup file automatically. Poor man's implementation but it's been so far so good for me.

Hope this helps.