PowerShell script exchange
Post Reply
GianlucaCroci
Expert
Posts: 231
Liked: 22 times
Joined: Feb 26, 2019 12:08 pm
Full Name: Gianluca Croci
Contact:

File-To-Tape

Post by GianlucaCroci »

Good morning,

every day a server saves data on a SMB Share by creating a new folder.
In this SMB Share we've 3 or 4 folders, and the retention of these is managed by the server that saves the data.
With Veeam I do a "File-to-Backup" every day and I extend the Retention to two weeks.

On the first day of each month I've to put this data on a Tape, and I wouldn't want to take all 3 (or 4) folders. Because now they are small, but in the future we'll also reach 1 TB for each.

I therefore thought of using "Pre" and "Post" scripts the "File-to-Tape", where I look for the last folder created and rename it in a name (the same every month) which I'll then insert in the job.
By doing so I no longer have to do anything at the level of the "File-to-Tape" job, nor in the folders.

Finished the saving on tape, I've to rename the folder putting back the original name. This is because, as written above, retention is managed by the server, or rather by the script that generates these folders.


Something I found but it doesn't work.

PRE command to write the folder name to a file
$ latest = Get-ChildItem | sort CreationTime -desc | select -f 1 | ft name -hide | Out-File -FilePath "D: \ scripts \ latest.txt"
PRE command to rename the folder
$ latest = Get-ChildItem | sort CreationTime -desc | select -f 1 | ft name -hide | Rename-Item -NewName {$ _. BaseName -replace 'MONTHLY'}
POST commands to read the folder name from the file
$ latest = Get-Content -Raw "D: \ scripts \ latest.txt" | where {$ _ -ne ""}
POST command to rename the folder
similar to the PRE


Maybe there is no need to do all these movements because there is another system, but I've not found it.

Thank's in advance for the Help.
Kind regards
Dima P.
Product Manager
Posts: 14726
Liked: 1707 times
Joined: Feb 04, 2013 2:07 pm
Full Name: Dmitry Popov
Location: Prague
Contact:

Re: File-To-Tape

Post by Dima P. »

Hello Gianluca,

I've moved your post to the PowerShell sub forums. Possibly other folks can comment / advise on your idea with pre-post job scripts and provide feedback. Thank you!
GianlucaCroci
Expert
Posts: 231
Liked: 22 times
Joined: Feb 26, 2019 12:08 pm
Full Name: Gianluca Croci
Contact:

Re: File-To-Tape

Post by GianlucaCroci »

thank you.
I had put it here because maybe there was another method of doing this tape save. :-)
david.domask
Veeam Software
Posts: 2123
Liked: 513 times
Joined: Jun 28, 2016 12:12 pm
Contact:

Re: File-To-Tape

Post by david.domask »

Hey Gianluca,

I think I get your idea; the thought is:

1. You have a folder list on some share like:

/backups/2022-01-28
/backups/2022-02-28
/backups/2022-03-28

2. You have a File to Tape (FTT/F2T) job and you added /backups/MONTHLY as the source item

3. You want pre/post job scripts that will:
a. Pre-job script finds the Newest folder and rename it to MONTHLY (e.g., from 1., /backups/2022-03-28 should be renamed to /backups/MONTHLY)
b. The FTT job runs
c. Post-Job script finds the folder /backups/MONTHLY and renames it to /backups/2022-03-28

Do I understand your goal right?

If so, can I ask, what does not work? There is an error? Something does or does not happen?
David Domask | Product Management: Principal Analyst
GianlucaCroci
Expert
Posts: 231
Liked: 22 times
Joined: Feb 26, 2019 12:08 pm
Full Name: Gianluca Croci
Contact:

Re: File-To-Tape

Post by GianlucaCroci »

Hello,

I apologize for the late reply. I thought I already did, and instead I got lost.

yes, exactly what is written.

1. I've the share as you wrote
2. I've the File-To-Tape job where I'll save the MONTHLY folder
3. I run the File-To-Tape job
a. I run the PRE script which will have to identify the most recent folder and rename it as MONTHLY
b. I backup the MONTHLY's folder on Tape
c. I run the POST script which will rename the MONTHLY folder with the name it had before the PRE run

To do the POST operation, when I do the PRE I go to write the name in a file, which I'll then read in the POST.
The problem I've is writing in the file only the folder name without anything else.

Code: Select all

PS C:\Windows\system32> Set-Location -path "D:\Temp"
PS D:\Temp> $latest = Get-ChildItem  | sort CreationTime -desc | select -f 1 | ft name -hide
PS D:\Temp> write $latest

ORIGINAL


PS D:\Temp> $latest = Get-ChildItem  | sort CreationTime -desc | select -f 1 | ft name -hide | Out-File -FilePath "D:\scripts\latest.txt"
VIEW OF THE FILE "latest.txt"
-------------------------

ORIGINAL


-------------------------
PS D:\Temp> $latest = Get-ChildItem  | sort CreationTime -desc | select -f 1 | ft name -hide | Rename-Item -NewName {$_.BaseName -replace 'MONTHLY'}
Rename-Item : Cannot bind argument to parameter 'NewName' because it is an empty string.
At line:1 char:105
+ ... ft name -hide | Rename-Item -NewName {$_.BaseName -replace 'MONTHLY'}
+                                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (Microsoft.Power...FormatStartData:PSObject) [Rename-Item], ParameterBindin
   gValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.Rena
   meItemCommand
Thank's in advance for the Help.
Kind regards
david.domask
Veeam Software
Posts: 2123
Liked: 513 times
Joined: Jun 28, 2016 12:12 pm
Contact:

Re: File-To-Tape

Post by david.domask »

Thanks for the update GianLuca,

I'd try it like this. Make a single script that has two switch parameters like:

Code: Select all

param(
     [switch]$pre,
     [switch]$post
)
you can have some simple code like this to avoid having to add multiple scripts.

Code: Select all

if($pre){
stuff
}

#.....


if($post){
stuff
}
Pre section should do something like:

Code: Select all

cd /path/to/your/working/directory
$latest = Get-Item | Sort-Object -Property CreationTime -Descending | Select -First 1
$latest.BaseName | Out-File temp-rename-file
Rename-Item -Path $latest.FullName -NewName "MONTHLY"
Post sections should do something like:

Code: Select all

cd /path/to/your/working/directory
$OGName = Get-Content temp-rename-file
Rename-Item -Path MONTHLY -NewName $OGName
When you add the script as a pre-post job in Veeam, small secret, you can add arguments for the script, so just do something like:

Pre-Job script: C:\Scripts\renamescript.ps1 -pre
Post-Job script: C:\Scripts\renamescript.ps1 -post

Give it a shot and see what you end up with.
David Domask | Product Management: Principal Analyst
GianlucaCroci
Expert
Posts: 231
Liked: 22 times
Joined: Feb 26, 2019 12:08 pm
Full Name: Gianluca Croci
Contact:

Re: File-To-Tape

Post by GianlucaCroci »

Hello,

I just tested with PowerShell, without the Veeam job and pre / post.

I had to change the line by putting
Get-ChildItem | Sort-Object -Property LastWriteTime -Descending
because he always asked me for a path and above all he always took the oldest folder from me.

in the folder I also have a file that has the same date and time as the folder, because it's a LOG file, and I always got this file.
to the Get-ChildItem command I added "-Directory" and now it works.


I try the job with the File-to-Backup

thank's a lot for the moment
david.domask
Veeam Software
Posts: 2123
Liked: 513 times
Joined: Jun 28, 2016 12:12 pm
Contact:

Re: File-To-Tape

Post by david.domask »

Ah, thanks for corrections :) As you can see from timestamps, it was in the evening for me.

But yes, you get the idea. Try it and see if it works. Also for the params, you need to make it:

param(
[switch]$pre=$false,
[switch]$post=$false
)

That way by default it does nothing, and looks for the flag; the param will flip the bit to be active.
David Domask | Product Management: Principal Analyst
GianlucaCroci
Expert
Posts: 231
Liked: 22 times
Joined: Feb 26, 2019 12:08 pm
Full Name: Gianluca Croci
Contact:

Re: File-To-Tape

Post by GianlucaCroci »

the script work very well.

at the end of the POST, added the row that delete the temporary file
Remove-Item -Path temp-rename-file
Thank's a lot for the Help
david.domask
Veeam Software
Posts: 2123
Liked: 513 times
Joined: Jun 28, 2016 12:12 pm
Contact:

Re: File-To-Tape

Post by david.domask »

Really glad you got it solved and filled in the blanks! Happy to help :)

If you don't mind, share your script; it might help others ;) Not a requirement, just would be nice.
David Domask | Product Management: Principal Analyst
GianlucaCroci
Expert
Posts: 231
Liked: 22 times
Joined: Feb 26, 2019 12:08 pm
Full Name: Gianluca Croci
Contact:

Re: File-To-Tape

Post by GianlucaCroci » 1 person likes this post

the script is

Code: Select all

param(
[switch]$pre=$false,
[switch]$post=$false
)

if($pre){

Set-Location -path "\\server\share\...\cluster_001"
$latest = Get-ChildItem -Directory | Sort-Object -Property LastWriteTime -Descending | Select -First 1
$latest.BaseName | Out-File -FilePath "D:\scripts\temp-rename-file.txt"
Rename-Item -Path $latest.FullName -NewName "MONTHLY"

}

#.....

if($post){

Set-Location -path "\\server\share\...\cluster_001"
$OGName = Get-Content "D:\scripts\temp1-rename-file.txt"
Rename-Item -Path MONTHLY -NewName $OGName

Remove-Item -Path "D:\scripts\temp-rename-file.txt"

}

On the "File-to-tape" job, "Options", "Advanced" button, "Advanced" form, "Job Scripts" part

Code: Select all

Powershell.exe -executionpolicy remotesigned -File D:\scripts\rename_filetotape.ps1 -PRE
Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests