Jump to content

Script needed to copy a single file to USB drive but only keep the last 3 versions


Recommended Posts

Posted
#Requires -Version 3.0$Source = "C:\iSCSIVirtualDisks\man-vd.vhdx"$Destination = "E:"$TargetDir = "{0}{1}" -f $Destination, (Get-Date).ToString("yyyy-MM-dd HH.mm.ss")Get-ChildItem -Path $Destination -Directory | Where-Object { $_.Name -match "^\d{4}-\d{2}-\d{2} \d{2}\.\d{2}\.\d{2}$"} | Sort-Object LastWriteTime -Descending | Select-Object -Skip 2 | Remove-Item -Force -RecurseIf (-not(Test-Path "$TargetDir")) { $Null = New-Item "$TargetDir" -Type Directory; Copy-Item -Path "$Source" -Destination "$TargetDir" -ErrorAction Stop -Verbose }

 

Hello, I have been using the above code at each of my schools and it has been working great. I have just taken over a new school where the backup data is much larger so I can only fit 1 backup on the USB drive each night and not the 3 that the code currently does.

 

What do I need to change in the code so it only copies 1 backup file to the USB and then deletes this the next night before copying the next backup.

 

BTW if I had to guess its something to do with the -Skip 2 section.

 

Thanks for your time, again

Posted
Hello, I have been using the above code at each of my schools and it has been working great. I have just taken over a new school where the backup data is much larger so I can only fit 1 backup on the USB drive each night and not the 3 that the code currently does.

 

What do I need to change in the code so it only copies 1 backup file to the USB and then deletes this the next night before copying the next backup.

 

BTW if I had to guess its something to do with the -Skip 2 section.

 

Thanks for your time, again

 

At a very quick glance you need to change the Select-Object -Skip 2 - If you are only wanting the latest I don't think you even need to do the skip. But if you play around trying that on 1 or removing that section you should get sorted. :)

  • Thanks 1
Posted

[color=#333333][i]#Requires -Version 3.0$Source = "C:\iSCSIVirtualDisks\man-vd.vhdx"$Destination = "E:"$TargetDir = "{0}{1}" -f $Destination, (Get-Date).ToString("yyyy-MM-dd HH.mm.ss")Get-ChildItem -Path $Destination -Directory | Where-Object { $_.Name -match "^\d{4}-\d{2}-\d{2} \d{2}\.\d{2}\.\d{2}$"} | Sort-Object LastWriteTime -Descending | Remove-Item -Force -RecurseIf (-not(Test-Path "$TargetDir")) { $Null = New-Item "$TargetDir" -Type Directory; Copy-Item -Path "$Source" -Destination "$TargetDir" -ErrorAction Stop -Verbose }[/i][/color]

 

Can try this.

 

Obviously in a test environment first.

 

:)

  • Thanks 1
Posted
Can try this.

I have fixed the formatting... ;)

 

#Requires -Version 3.0
$Source = "C:\iSCSIVirtualDisks\man-vd.vhdx"
$Destination = "E:"
$TargetDir = "{0}{1}" -f $Destination, (Get-Date).ToString("yyyy-MM-dd HH.mm.ss")

Get-ChildItem -Path $Destination -Directory | Where-Object { $_.Name -match "^\d{4}-\d{2}-\d{2} \d{2}\.\d{2}\.\d{2}$"} | Sort-Object LastWriteTime -Descending | Remove-Item -Force -Recurse

If (-not(Test-Path "$TargetDir")) { 
 $Null = New-Item "$TargetDir" -Type Directory; 
 Copy-Item -Path "$Source" -Destination "$TargetDir" -ErrorAction Stop -Verbose 
}

  • Thanks 2
Posted
I have fixed the formatting... ;)

 

#Requires -Version 3.0
$Source = "C:\iSCSIVirtualDisks\man-vd.vhdx"
$Destination = "E:"
$TargetDir = "{0}{1}" -f $Destination, (Get-Date).ToString("yyyy-MM-dd HH.mm.ss")

Get-ChildItem -Path $Destination -Directory | Where-Object { $_.Name -match "^\d{4}-\d{2}-\d{2} \d{2}\.\d{2}\.\d{2}$"} | Sort-Object LastWriteTime -Descending | Remove-Item -Force -Recurse

If (-not(Test-Path "$TargetDir")) { 
 $Null = New-Item "$TargetDir" -Type Directory; 
 Copy-Item -Path "$Source" -Destination "$TargetDir" -ErrorAction Stop -Verbose 
}

 

Thank you, sorry about bad formatting, I replied on my phone.

  • Thanks 1
  • 2 weeks later...
Posted

Hi all :0)

 

Currently the source is set to $Source = "C:\iSCSIVirtualDisks\man-vd.vhdx"

 

If I wanted to backup the iSCSIVirtualDisks folder rather than the single file in the folder, am I OK simply deleting the \man-vd.vhdx section?

 

Basically I have a few other files in that folder now that I need copying to the USB drive.

Posted
Hi all :0)

 

Currently the source is set to $Source = "C:\iSCSIVirtualDisks\man-vd.vhdx"

 

If I wanted to backup the iSCSIVirtualDisks folder rather than the single file in the folder, am I OK simply deleting the \man-vd.vhdx section?

 

Basically I have a few other files in that folder now that I need copying to the USB drive.

 

With a quick glance at it yes should be fine.

Posted

Just ran the script and it does not work with just C:\iSCSIVirtualDisks

 

The script deletes the older folders, creates the dated folder with the iSCSIVirtualDisks folder inside but the files are not copied.

Posted (edited)
I am at work so having to be sly haha

I can wait until later if needed :0)

 

tried with and without the \

 

#Requires -Version 3.0

$Source = "C:\iSCSIVirtualDisks"

$Destination = "E:"

 

$TargetDir = "{0}{1}" -f $Destination, ((Get-ItemProperty $Source).LastWriteTime).ToString("yyyy-MM-dd HH.mm.ss")

 

Get-ChildItem -Path $Destination -Directory | Where-Object { $_.Name -match "^\d{4}-\d{2}-\d{2} \d{2}\.\d{2}\.\d{2}$"} | Sort-Object LastWriteTime -Descending | Select-Object -Skip 2 | Remove-Item -Force -RecurseIf (-not(Test-Path "$TargetDir")) { $Null = New-Item "$TargetDir" -Type Directory; Copy-Item -Path "$Source" -Destination "$TargetDir" -ErrorAction Stop -Verbose }

Edited by TwistedHelixis
Posted

So you want to copy the C:\iSCSIVirtualDisks folder?

and only copy so many copies of it in the destination folder?

 

I've forgotten what the end goal was haha.

Posted
So you want to copy the C:\iSCSIVirtualDisks folder?

and only copy so many copies of it in the destination folder?

 

Yes, I can only fit a few copies of this folder on a USB drive. I will also be using more than one USB drive which will get switched each week.

Posted

USB drives for back ups? I'd consider something a little less volatile myself like haha.

I finish work at 4, I will look at this tonight for you if no one replies earlier with the solution for you.

 

:)

 

- - - Updated - - -

 

Hold on, my original script is now not working, perhaps something else is going on. Hold on and I will get back

I feel like what I said earlier as a modification should have worked at a quick glance at it so yes maybe something else is wrong :)

Posted
USB drives for back ups?

 

90% of all our files are cloud based, with cloud 2 cloud Spanning backup. For the few bits onsite that need backing up (DC etc) I have an onsite iSCSI server. I then wanted a way to have a few backups either offsite or in the safe which is where the USB drives come in.

Posted
Glad you got the original working. As I say, if no one manages to help you before I get home, I will look at the script tonight when I am not at work and make the adjustments you need.
  • Thanks 1
Posted (edited)

Hi, I have been trying and failing to write my own script. I have setup a test folder on my laptop and have used the script below as a start but I still end up copying just the iSCSIVirtualDisks folder and not both the files in the folder. I also found a completely different script online but that did not even run.

 

For anyone that has jumped to this last thread - I now need to copy a folder or the contents of a folder (2 large files) from the C drive to an external USB drive. The folder to be copied contain 2 very large files and the external USB will only be able to store the last 2 copies, so I need a way to to backup the files in to a folder with the current date / time and also need a way to delete older backups from the USB. The script below works perfectly when copying just 1 file to the USB but I now have more then one file in the iSCSIVirtualDisks folder to get copied :0)

 

Sorry, I know I am a pain in the bum but would be very grateful for some help.

 

#Requires -Version 3.0

$Source = "C:\iSCSIVirtualDisks\man-vd.vhdx"

$Destination = "E:"

 

$TargetDir = "{0}{1}" -f $Destination, ((Get-ItemProperty $Source).LastWriteTime).ToString("yyyy-MM-dd HH.mm.ss")

 

Get-ChildItem -Path $Destination -Directory | Where-Object { $_.Name -match "^\d{4}-\d{2}-\d{2} \d{2}\.\d{2}\.\d{2}$"} | Sort-Object LastWriteTime -Descending | Select-Object -Skip 2 | Remove-Item -Force -RecurseIf (-not(Test-Path "$TargetDir")) { $Null = New-Item "$TargetDir" -Type Directory; Copy-Item -Path "$Source" -Destination "$TargetDir" -ErrorAction Stop -Verbose }

Edited by TwistedHelixis
Posted (edited)

OK so the script below does copy the folder with both files but it deletes the older files based on the amount of days the files have been in the destination. What I need is to always keep the last couple of backups no mater the date, similar to the script above that works with one file.

 

Write-Host("Copying logs to place")

if (Test-Path("C:\iscsi")){

Get-ChildItem -Path "D:\folder" | Where-Object {$_.psiscontainer -and $_.lastwritetime -le (get-date).adddays(-7)} | ForEach {remove-item $_.fullname -Force -Recurse -ErrorAction SilentlyContinue } # Clears out files older than 7 days

 

Get-Childitem -Path "C:\iscsi" | Copy-item -Destination "D:\folder" -Force

}

else{Write-Host("*** Error: logs not copied")}

 

 

EDIT - It also does not create a new folder or filename for each backup, I think it is just overwriting the old backup which is no good.

Edited by TwistedHelixis

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...