Archive folders or logs as zip file in Windows

Archive folders or logs as zip file in Windows

February 25, 2022

As an IT guy, we might need to set a custom backup or archiving of multiple folders or logs and keep it inside or out of the server to prevent it consumes disk space. On this way, we can archive the old data as compressed file, with a date time.

On Linux, we can use following:

sudo tar -cvpzf /destinationfolder-date '+%Y-%m-%d'.tar.gz -C source

In this example, I want to back up a single folder on www folder.

sudo tar -cvpzf /root/backups/www_backup/wordpress-date '+%Y-%m-%d'.tar.gz -C /var/www/html wordpress

On Windows, we can use robocopy combines it with 7zip which provides the shell command.
First of all, install 7zip, and create a batch script and put the following code:

: Copy the recursive folder to destination with date format.
robocopy C:\Install\test1 C:\Install\test2\%date:~-10,2%%date:~-7,2%%date:~-4,4%\ /MIR

: Change directory to where's the 7zip is installed.
cd "C:\Program Files\7-Zip\"

: Compresh the destination backup folder as zip.
7z.exe a C:\Install\test2\%date:~-10,2%%date:~-7,2%%date:~-4, C:\Install\test2\%date:~-10,2%%date:~-7,2%%date:~-4,4%\

: Delete the destination backup folder, so it will keep only the .zip file.
rmdir /s /q C:\Install\test2\%date:~-10,2%%date:~-7,2%%date:~-4,4%\

To make it run as scheduled, we can use crontab -e on Linux, and Windows scheduler on Windows.


Leave a Comments

Your email address will not be published. Required fields are marked *

Copyright ©2023 All rights reserved