VMware Cloud Community
YannickTresch
Contributor
Contributor

Backup VMs with Copy-DatastoreItem

Hi

I'm trying to write a script who backups the VMs of our helpdesk. They have some testmachines, who need only a basic backup.

The Script:

function backupvm($name, $destination) {
    $test = Test-Path $destination
   
    if($test -eq $false) {
        mkdir $destination
    }
   
    Copy-DatastoreItem MyDS:\$name\* $destination
}

# Set variables
$server = "vCenterServer"
$folder = "Testclients CLC"
$ds = "datastore"
$backupdestination = "M:\TresY\testbackup"

# Connect to $Server
connect-viserver $server

# Generates a PSDrive
$datastore = Get-Datastore $ds
New-PSDrive -Name MyDS -PSProvider ViMdatastore -Root '\' -location $datastore

# Suspends all started VMs
Write-Host "VMs are now backupped:"
Get-VM -location $folder | Foreach-Object {
    if($_.PowerState -eq "PoweredOn") {
        Suspend-VM -VM $_.Name -Confirm:$false
       
        backupvm $_.Name $backupdestination"\"$_
       
        Start-VM -VM $_.Name -Confirm:$false
        Write-Host $_.FullName
    } Else {
        backupvm $_.Name $backupdestination"\"$_
        Write-Host $_.Name
    }
}


When I execute it, followed error arrives:

Copy-DatastoreItem : 12.04.2011 08:42:24    VimDatastore        Download of fil
e 'https://vCenterServer:443/folder%2ft008002%2fvmware.log?dcPath=
Biel&dsName=DateStore' failed. Error message: The remote serv
er returned an error: (401) Unauthorized.
At C:\Temp\powershell\backups-testesxi21.ps1:19 char:23
+     Copy-DatastoreItem <<<<  -Item MyDS:\$name\* -Destination $destination
    + CategoryInfo          : NotSpecified: (:) [Copy-DatastoreItem], VimExcep
   tion
    + FullyQualifiedErrorId : Client20_StorageServiceImpl_DownloadFile_Downloa
   dFailed,VMware.VimAutomation.ViCore.Cmdlets.Commands.CopyDatastoreItem

From MyDS to MyDS i can copy the files without problmes. But to my local path on M:\TresY\testbackup i receive the error.

Any ideas?

Thanks!
Yannick

0 Kudos
13 Replies
LucD
Leadership
Leadership

Seems to work for me.

Do you have write access to that M:\TresY\testbackup folder ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
YannickTresch
Contributor
Contributor

Yes, the mkdir in M:\TresY\testbackup works fine. I have only problems to download the files from the datastore. Normally that should work also, I'm admin on the datastore where I want to download the files.

0 Kudos
LucD
Leadership
Leadership

Can you try with the destination directory locally on the PC where you execute the script, i.e. C:\backupFOlder ?

Perhaps the cmdlet has a problem with UNCs.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
YannickTresch
Contributor
Contributor

Exactly as before.

0 Kudos
LucD
Leadership
Leadership

You're running the latest PowerCLI version I assume ?

Get-PowerCLIVersion

And which vCenter and ESX(i) versions are you using ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
YannickTresch
Contributor
Contributor

PowerCLIVersion

----------------
   VMware vSphere PowerCLI 4.1 U1 build 332441
---------------
Snapin Versions
---------------
   VMWare vSphere PowerCLI 4.1 U1 build 332441

vCenter 4.1.0, 258902

ESXi 4.1.0, 348481

0 Kudos
LucD
Leadership
Leadership

Those are the same versions I tested against.

Does it already fail on the first file ? Or is it with the bigger .vmdk files ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
YannickTresch
Contributor
Contributor

It fails already at the first file.

0 Kudos
LucD
Leadership
Leadership

A wild shot, can you add the following line before the Copy-DatastoreItem cmdlet ?

New-Item -Path $destination -ItemType "file" -Name test.txt

Just curious if you can write anything in that folder.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

YannickTresch
Contributor
Contributor

We've tried now to connect with another user (full access) on the vCenter. With an vcenter wide admin we can download the files to M:\TresY\testbackup.

We don't want to give the helpdesk and to the backup service account full access to our vCenter.

0 Kudos
LucD
Leadership
Leadership

That means you have to find out the right combination of privileges.

Once you have those you can create a new role and assign it to your helpdesk.

The vpxd logs normally contain indications of missing rights for an action.

Perhaps it is easier to start from there.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

YannickTresch
Contributor
Contributor

Ok, thank you! I will check that tomorrow.

0 Kudos
YannickTresch
Contributor
Contributor

Ok, the user must have the persmission Low level file operations on the whole datacenter - only the datastore isn't enough.

Thanks for you help!

0 Kudos