Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
305 views
in Technique[技术] by (71.8m points)

powershell - How to find most recent full and incremental files using script

We're using Veeam Agent for Windows for several machines which typically produces folders with contents like this:

2021-01-16  11:14 PM    69,181,009,920 DEV82021-01-16T230042.vbk
2021-01-17  11:02 PM     2,516,852,736 DEV82021-01-17T230037.vib
2021-01-18  11:03 PM     4,966,821,888 DEV82021-01-18T230106.vib
2021-01-19  11:02 PM     4,096,479,232 DEV82021-01-19T230103.vib
2021-01-20  11:02 PM     4,885,250,048 DEV82021-01-20T230103.vib
2021-01-21  11:02 PM     4,519,645,184 DEV82021-01-21T230037.vib
2021-01-22  11:02 PM     5,085,712,384 DEV82021-01-22T230037.vib
2021-01-23  11:17 PM    70,159,249,408 DEV82021-01-23T230038.vbk
2021-01-24  11:02 PM     3,897,864,192 DEV82021-01-24T230026.vib
2021-01-25  11:02 PM     6,467,932,160 DEV82021-01-25T230027.vib
2021-01-26  11:02 PM     5,735,276,544 DEV82021-01-26T230031.vib
2021-01-28  07:42 AM     6,019,416,064 DEV82021-01-28T074032.vib

What I need is to be able to capture the most recent .vbk file (full backup) and all of its related *.vib files (incremental backups). The script would run daily so if the most recent .vbk hasn't changed from previous run then skip uploading it and its related .vib files, uploading only new .vib files until a new .vbk is created. After 7 days a new .vbk would be created so we should be uploading this and any related .vib files.

Using example above the following files should be uploaded (we use gsutil):

2021-01-23  11:17 PM    70,159,249,408 DEV82021-01-23T230038.vbk
2021-01-24  11:02 PM     3,897,864,192 DEV82021-01-24T230026.vib
2021-01-25  11:02 PM     6,467,932,160 DEV82021-01-25T230027.vib
2021-01-26  11:02 PM     5,735,276,544 DEV82021-01-26T230031.vib
2021-01-28  07:42 AM     6,019,416,064 DEV82021-01-28T074032.vib

When the script runs again the next day, check if there's a new or changed .vbk or .vib file and upload them using gsutil. After 7 days, delete all old .vbk files and its related .vib files leaving only the newest .vbk file and its newest related .vib files.

How can I capture these filenames into some variable using preferably a windows batch script or alternatively a powershell script, then process each file from the variable and upload it using gsutil with something like:

gsutil cp -n -r "192.168.2.251Backups%COMPUTERNAME%" gs://%Bucket%/"%COMPUTERNAME%"

Right now I have something like this:

for /f "skip=1" %%i in ('dir "\192.168.2.251Backups\%COMPUTERNAME%" /b /o-d') do (set newestvbk=%%i)

but this only gives me the newest .vbk (DEV82021-01-23T230038.vbk). I also need its related .vib files:

DEV82021-01-24T230026.vib DEV82021-01-25T230027.vib DEV82021-01-26T230031.vib DEV82021-01-28T074032.vib

How do I capture the most recent vbk file AND all .vib files created afterwards?

question from:https://stackoverflow.com/questions/65942949/how-to-find-most-recent-full-and-incremental-files-using-script

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
for /f %%i in ('dir "\192.168.2.251Backups\%COMPUTERNAME%" /b /a-d /o-d') do (
 if /i "%%~xi"==".vib" echo backup %%i
 if /i "%%~xi"==".vbk" echo backup %%i&goto done
)
:done

Backing up each vib file until a vbk is found

[revision in response to comment - delete older .vib, .vbk]

set "keepme=Y"
for /f %%i in ('dir "\192.168.2.251Backups\%COMPUTERNAME%" /b /a-d /o-d') do (
 if /i "%%~xi"==".vib" if defined keepme (echo backup %%i) else (echo del %%i)
 if /i "%%~xi"==".vbk" if defined keepme (echo backup %%i&set "keepme=") else (echo del %%i)
)

Using if defined to interpret a batch switch named keepme. Before the loop, keepme is defined (the actual value is irrelevant). the .vib and .vbk files are processed as before, but after the first .vbk is backed up, keepme is set to nothing which makes it undefined, so further files of the selected extensions are deleted.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...