1

I am attempting to automate the backup of a folder on a Windows 2003 R2 server (currently a manual process).

There are three components to this task:

1) Generate a zip file of the following folder in the same directory, without using 3rd party software:

d:\source\folder_to_back_up

Which gives us:

d:\source\folder_to_back_up.zip

2) Move the newly-created zip file to:

d:\destination

3) Rename the zip file according to the following naming convention:

Backup_YYYYMMDD.zip

For example, using today's date, the name would be:

Backup_20150516.zip

I have, so far, found out how to automate step 2:

robocopy /mov d:\source d:\destination folder_to_back_up.zip

...but need some help automating the creation of the zip file and also the renaming step.

Any ideas would be much appreciated!

2 Answers 2

1

Since you started considering 7-zip in the comments of the other answer, here's how I did it:

set SOURCE=d:\source\folder_to_back_up
set DEST=d:\destination
rem # a: Add, -mx: compression level 0-9, -tzip use ZIP format, -ssc: case sensitive, -ssw: add files opened for writing, -r: recursive
pushd "%SOURCE%" && 7za a -mx9 -tzip -ssc -ssw -r "%DEST%\Backup_%date%.zip" & popd

Note that this is essentially a one-liner if you inline the source and dest variables. The pushd/popd pair is there so that the context doesn't change in case you're doing multiple backups in one batch. %date% is a built-in.

I don't think it's reasonable to want to create a special file format (zip) without the proper tools. It's like wanting to compile Java source code into jars without a JDK.

Dependencies

You can work around the problem of deleting the tool by having the above code in backup_stuff.bat and copying 7za.exe next to that file named backup_stuff.bat-7za.exe and then you just need to replace 7za with %~dp0\backup_stuff.bat-7za to reference exactly that file. This should deliver the message that there's an external dependency here...

Alternatively if you want to be really paranoid, just embed 7zip into the batch file :)

set SOURCE=d:\source\folder_to_back_up
set DEST=d:\destination

goto deps

:exec
pushd "%SOURCE%" && "%TEMP%\7za" a -mx9 -tzip -ssc -ssw -r "%DEST%\Backup_%date%.zip" & popd
goto end

:deps
@(
rem This is the Base64 enconded version of 7za.exe
rem To create this section execute the following commands:
rem certutil -encode 7za.exe 7za.exe.b64
rem echo @echo off>7za.exe.b64.bat && for /F "tokens=*" %L in (7za.exe.b64) do @echo echo %L>>7za.exe.b64.bat

echo -----BEGIN CERTIFICATE-----
echo TVqQAAMAAAAEAAAA//8AALgAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
echo AAAAAAAAAAAAAAAA+AAAAA4fug4AtAnNIbgBTM0hVGhpcyBwcm9ncmFtIGNhbm5v
echo dCBiZSBydW4gaW4gRE9TIG1vZGUuDQ0KJAAAAAAAAAB9EXJSOXAcATlwHAE5cBwB
echo QmwQATtwHAG6bBIBIHAcAQ9WFgFbcBwBt3hDAThwHAE5cB0BlnAcAbp4QQE+cBwB

rem ... lot of lines ommitted for sake of stackoverflow

echo AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
echo -----END CERTIFICATE-----
)>%TEMP%\7za.exe.b64
certutil -decode -f %TEMP%\7za.exe.b64 %TEMP%\7za.exe >NUL

goto exec

:end

My cmd crashed when I tried to do this with the original 7-zip v9.20 exe file. It looks like there's a limitation on block length (...), I found it to be around 370kB (roughly 5000 lines of b64). The original base64 of 7-zip was 800kB, but that's not a biggie, because it can be split up to multiple @(...)>>target.file blocks which are under the limit. But I suggest to run in through upx --brute 7za.exe which makes the exe file less than 300kB in size.

Sources

1
  • What's with the 7za? I changed it from 7za to 7z and then it worked fine.
    – Bodokh
    Aug 14, 2018 at 8:42
0

You can create a zip file from a batch file. This can be done using the Shell.Application software code in Microsoft Windows, and it can be done using a command line, but some additional code is needed. e.g., Ansgar Wiechers's answer to user2868186's question contains code (near the bottom) which looks solid. As an alternative, Rob Vanderwoude's page on VBS also shows a solution.

You might not like the idea of needing to have this 8-12 lines (estimated amount) of VBScript. Well, that is the best available method that I've found to automate things while relying on the least amount of code added to MS Windows. I don't think MS Windows (at least not the version you're asking for) provided a way to do that with less code.

However, if you're going to add that code on a regular basis, then you probably will want to download it, rather than type it more than once. And if you're downloading it, I would suggest that you might be just as well off downloading some other code, like 7-Zip or Info-Zip. 7-Zip may take some more time to learn how to use it from the command line, but I've had success with it. And if you're not going to do this more than once, then that's probably another reason that adding software once would probably be a good solution in the long term (minimal time investment, long term payoff). I understand that using such software is not precisely what you asked, but I am bringing that up anyway because I suggest really considering whether that ends up being the best solution.

Regarding your third step, place the following in a batch file:
For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c%%a%%b) echo %mydate% Note that the FOR command that is shown is intended to be run from a batch file. Attempting to run the exact same command outside of a batch file will not work as well. I tested in Win7 SP1. I don't have a 2003 R2 server handy to test that, but I'm guessing that this approach will work well for you.

I hereby credit to Jay's answer to Rory's question about time for the basis for this approach.

4
  • One reason I'd like to avoid using 7-Zip (or similar) is because I'd like this to be completely self contained. If another administrator, further down the line, uninstalled whatever 3rd-party application this relied on from the server - it would break this automated backup. I'd rather have numerous lines of VBScript than that risk. May 16, 2015 at 9:02
  • @Austin''Danger''Powers 7zip have portable version, so you can leave the 7z.exe somewhere in the folder of your choice. Other admin has to know exactly where the 7z executable located to delete it (which that information is inside the batch script of course, but once they know it is needed, I doubt they will delete it). They won't be able to do accidental un-installing from the list of Programs and Features.
    – Darius
    May 16, 2015 at 14:25
  • I'll give this a try with the portable version of 7-Zip. That sounds like a pretty robust solution. Thanks for the third section, regarding automating renaming with date+timestamp - I've modified it to do exactly what we need. Excited to see this coming together so fast! I just need to figure out how to automate using the portable version of 7-Zip now (which should be pretty easy). May 16, 2015 at 16:07
  • Austin: I'd like to point out that 7-Zip can make zip files, which can then be extracted, via scripting or other means, from right within the operating system. For example, you can just "open" the zip file by double clicking. Info-Zip is also a solution that can use jsut single executable/file; last I checked, the Win32 console version doesn't even come with a Windows installer, so the default distribution is a "portable" version.
    – TOOGAM
    May 20, 2015 at 10:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .