Copy selected backup file to another drive

mcooper13
mcooper13 used Ask the Experts™
on
My backup application outputs a file called servername_YYYY_MM_DD_HH_MM_SS with each backup job run.

I wish to automate copying this file to another drive for offsite storage purposes. Since there will be multiple backup files of this server by date/time what script can I use to select the last, or most recent backup file to copy?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Jason RybergTechnical Consultant IV

Commented:
Here is a PowerShell method to select the latest written file in a directory:

$Path = \\server\backup_directory
Get-ChildItem $Path | sort LastWriteTime | Select -Last 1

Open in new window

Author

Commented:
What would be the best way to schedule PowerShell script to run daily? Task scheduler?
Jason RybergTechnical Consultant IV

Commented:
Yes, task scheduler is a good choice.
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Just to be pedantic, that script doesnt do what you expect, it is using the last write time (not sure if thats "created date-time" or "modified date-time") from the file metadata, not selecting by filename.

Author

Commented:
To clarify, the drive that stores the daily backup files for server1 will also have backup files for server2, 3, etc. so good point. The file name does need to be part of the selection criteria.
Technical Consultant IV
Commented:
The easiest way to parse through a single directory with all your server backups would be to input a file that contains the list of servers.  Not knowing if you conform to a server naming standard, it would be hard to recommend a looping method to group the server backups by server name.

So, assuming you have a list you can input into PowerShell (CSV, txt, etc):

$servers = Import-CSV C:\pathToservers.csv #With "servers" as the header

$Path = \\server\backup_directory
#$BackupDir = Get-ChildItem $Path

$mostrecentBUs = @()

$servers | % {

  $mostrecentBUs += Get-ChildItem "$Path\$($_.servers)_*" | sort LastWriteTime | Select -Last 1

}

$mostrecentBUs | % { Copy-Item $_.FullName \\destination\address  }

Open in new window


Hopefully this helps.
Doh! and i didnt even get a few points for pointing out that glitch!!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial