Example: Directory backup with BITS cmdlets


UPDATE: This posting has been updated with a zip file that contains two versions of the script. The file “bitsbackup.ps1” is intended for use with Windows 7 RC version of BITS cmdlets and the file “bitsbackup-ctp3.ps1” is intended for use with Windows 7 Beta or PowerShell 2.0 CTP 3 version of BITS cmdlets.


—–


 Here we have a sample PowerShell script for copying a directory to another UNC path. What’s so special about this, you say? This scripts uses BITS PowerShell cmdlets; which means that you can backup a folder to a network location while preserving the responsiveness of other network apps.


This example is based on the Win7 beta RC version of the BITS Powershell cmdlets.


How to Use: 



  1. Load this script by running: Import-Module <location of script>\bitsbackup.ps1
  2. Usage example: Backup-Directory .\myfolder \\backupserver\sharename -recurse

    • This will create a copy of myfolder under \\backupserver\sharename\myfolder

  3. For more help: Get-Help Backup-Directory

Alex Ng [MSFT]

bitsbackup-sample.zip

Comments (6)

  1. Mr. X says:

    I am a PowerShell newbie of the first degree but was excited because we are trying to seed 300GB of data for a DFS-R setup and I thought this script would do the job.

    However, I get an error message when running the import-module command in V2 CTP3 on Server 2003 R2.

    Any ideas?  I also can’t invoke start-bitstranfer – where does this module live?

    Here is the error message for loading bitsbackup.ps1.

    PS S:OPSPowerShell> Import-Module bitsbackup.ps1

    Import-Module : The specified module ‘bitsbackup.ps1’ was not loaded because no valid module file was found in any module directory.

    At line:1 char:14

    + Import-Module <<<<  bitsbackup.ps1

       + CategoryInfo          : ResourceUnavailable: (bitsbackup.ps1:String) [Import-Module], FileNotFoundException

       + FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand

  2. Alex Ng [MSFT] says:

    Hi Mr. X,

    1) "However, I get an error message when running the import-module command in V2 CTP3 on Server 2003 R2."

    By default, the Import-Module cmdlet will look in your $pshomemodules directory.

    To import this script, you will need to give the path of the script. For example, if your script is located in C:tmpbitsbackup.ps1 then, you will need to run "Import-Module C:tmpbitsbackup.ps1".

    I’ve also re-uploaded a version of the script that should work with V2 CTP3.

    2) "I also can’t invoke start-bitstranfer – where does this module live?"

    This cmdlet does not exist in CTP3. For the BITS cmdlets in CTP3, you can look at the following post:

    http://blogs.msdn.com/wmi/archive/2009/01/23/introducing-bits-powershell-cmdlets.aspx

    Hope this helps!

    – Alex

  3. Mr. X says:

    I found have to do a “Import-Module FileTransfer”, but even then, “Start-FileTransfer” referenced in bitsbackup.ps1 does not exist in PowerShell.  Is there something I am missing or does this command not exist in v2 CTP3?

  4. Mr. X says:

    Hmmm…looks like the attachment was revised after it had initially been posted and it certainly makes sense because this is now importing File-Transfer.

    Off to the lab!

  5. Idan says:

    Another thing to be careful when importing a module is the 32/64 bit use.

    If for instance you are using visual studio and recieving the error

    "The specified module blah blah was not loaded because no valid module file was found in any module directory", it is probably because your application runs in 32 bit (under visual studio), and the OS is 64 bit.

    You can change it by clearly stating in the properties of your visual studio project that the target platform should be x64.