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