8

I'm trying to create a backup script with 7-Zip. I have everything else done already, but these two issues are still blocking me.

Firstly, I want to add a timestamp with a create date like this: 3.11.2010 Backup.7z

So the dd.mm.yyyy format. I tried several versions I found on the Internnet, but none of these would work the way I wanted. What should I do?

Secondly, I only want to add the last modified folder to the archive (no matter how many folders exist in directory; I just need the latest). Basically, I have something like this:

App_v.1.0.0.4.exe_Url_2um2yok5q4vpoxnvnscpq3adfwff4wsmi
App_v.1.0.0.5.exe_Url_ft4mnvbu54hfrgdhxrahj4imlmermdsoe

So I want to add the newest (1.0.0.5) folder only. These folders change and always have different names so name based sorting would be a bad idea.

TMRW
  • 1,074
  • 4
  • 17
  • 28

5 Answers5

12
7z a -r "%DATE:~7,2%.%DATE:~4,2%.%DATE:~-4% Backup".7z

Will create the archive with DD.MM.YYYY Backup.7z Format.

Explanation: Echoing %DATE% prints the date in your regional date format setting.

D:\>echo %DATE%
Thu 11/04/2010

By using ~x,y specifier your are doing an in string/substring extraction of the string - where x is the starting character and y number of characters you wish to extract.

On your second point:

I only want to add last modified folder to archive(no matter how many folders exist in directory I just need the latest).

7z u -r "%DATE:~2,2%.%DATE:~5,2%.%DATE:~-4% Backup".7z 

should do it.

7zip date

3

You can use "windows management instrumentation". See this one

test1.bat:

set mydate=
for /f "skip=1" %%x in ('wmic os get localdatetime') do if not defined mydate set mydate=%%x
echo Current date is %mydate%

Result:

Current date is 20140202145547.557000+060

Regardless of your "regional settings" (date format) in "Control Panel" it always returns YYYYMMDD.

vlakov
  • 31
0

I have simpler solution :

7z a "%DATE%_%TIME:~0,2%.%TIME:~3,2%_Backup.7z" c:\test

Output:

17.03.2019_18.43_Backup.7z

0

7zip command line isn't going to have the logic to perform the tasks you need. If you're running Linux, you'll have to figure out how to do this in your shell script. If you're in windows you can experiment with DOS batch language (which MAY be powerful enough), powershell (which requires that you install something), or write a vbscript (.vbs) file which should be a pretty good solution.

If you decide to go the vbs route, I recommend you look into using the filesystemobject with vbscript. http://www.devguru.com/technologies/vbscript/14048.asp

NoCarrier
  • 3,517
0

What I've done to get files that changed that day was use Robocopy to copy the newly modified files to a temporary directory and then 7zip that.

EDIT:
Right from the go I want to say I'm not batch script ninja, but through reading websites I was able to kluge something together that seem to work good enough. I'm not a programmer, but I'm the "computer guy", so I just know more than most I work with.

Here is the script I use. The date format is different than you asked for, chose YYYY-MM-DD because then things would sort well in Windows Explorer. You should be able to change that to how you want things labeled.

The /maxage:1 part of the script is what limits to things what had change within a day. I run the backup script daily.

One problem I hadn't fixed in the script is if no files have changed, a 7z file of 32k in size is still created (an empty 7z archive). I had planned to add empty directory detection, or if nothing else do a hash and compare to an empty 7z archive hash and if the match then delete the newly created empty 7z archive.

In my full backup script, I have the script hash the archive so I can manually compare the hashes of the last two full backups to see if they are the same. (Having a script like this won't detect if files were deleted because incremental backups would show no files added.) If the current hash and previous hash are the same, I delete newest backup because nothing had changed since then. (I planned to automate is checking if I had time.)

I hope cleaning the script up (removing server names and so on) I didn't break it, and I never had much time to polish up the script so it is crude. At any rate, I hope it will be good enough to get started down the right direction to a script that will meet your requirements.

If you need any clarification on switches or why I chose the switches I did, feel free to ask.

rem Setup Variables...
set zcmd="c:\program files\7-zip\7z" a -t7z -r -mx=7 -ssw -mtc=on
set backupdir=C:\_backups\2burn
set tempdir=C:\_backups\temp
rem date format YYYY_MM_DD
set mydate=%date:~10,4%_%date:~4,2%_%date:~7,2%
set rc=c:\robocopy.exe

rem Make Directories...
md %backupdir%\%mydate%
if exist %tempdir% rd %tempdir% /s /q
if not exist %tempdir% md %tempdir%

rem Copy changed files...
%rc% \\server1\source\path %tempdir%\temp\path *.* /s /z /copy:dat /maxage:1 /r:5 /w:5     /log:%backupdir%\%mydate%\log.txt /np

rem Compress changed files...
if exist %tempdir%\temp\path %zcmd% %backupdir%\%mydate%\backup1.7z %tempdir%\temp\path\*.*

rem Remove temp directory...
rd %tempdir%\temp\path\ /q /s

rem copy compressed file and log to offsite server...
%rc% /copy:dat /e /z /r:5 /w:5 %backupdir%\%mydate%  "\\remote_server\share\backup_path\%mydate%"


rem email changed files log from offsite server...
rem C:\blat\blat262\full\blat -subject "BATCH: backup1 inc backup for %mydate%." -bodyf "\\remote_server\share\backup_path\%mydate%\log.txt" -server \\mailserver -f AccountToSendTo -tf c:\ListOfAdmins2Email.txt -u Username -pw Pa$$w0rd