A few days ago, I mentioned I did simple backups using the SyncToy powertool, and mentioned I might try replacing it with Robocopy. The end result is a simple PowerShell script that drives Robocopy. It simply backs up a specific set of directories and my VPC data virtual hard drive (.vhd) into a new folder with the current date, and leaves a log of the entire process.

The script also makes it easy to specify for each folder a set of file extensions to ignore. For example, I don't want to include object files when backing up my source code trees.

Anyway, it's not fancy, but it does the trick. Here's the script:

function script:filename([string] $path) {
   return [System.IO.Path]::GetFileName($path)
}
function stars {
   return (new-object string ('*', 80))
}

$BASE = 'g:\backups'
$today = [datetime]::Today.ToString("yyyyMMdd");

$dest = "$BASE\$today"
$log = "$dest\log.txt"

if ( -not (test-path $dest) ) {
   mkdir $dest
}

#
# write header
#
write (stars) >> $log
write "Backup procedure starting at " `
      ([datetime]::Now.ToString()) >> $log
write (stars) "`r`n`r`n" >> $log

#
# stop CVS services
#
net stop cvsnt >> $log
net stop cvslock >> $log

#
# backup our set of folders
#
$folders = (
   ('e:\CVS', ''),
   ('e:\devdeo', ''),
   ('e:\home', '*.swp'),
   ('e:\projects', '*.swp *.obj *.exe *.dll *.pdb *.pch *.idb'),
   ('e:\opensource', '*.swp *.obj *.exe *.dll *.pdb *.pch *.idb'),
   ('e:\tools', '')
)

$folders | %{
   $name = filename($_[0])
   $excludes = $_[1].Split(' ')
   robocopy $_ "$dest\$name" *.* /E /ZB /NP /XF $excludes >> $log
}

#
# backup our VPC data file
#
robocopy 'E:\vpc\' $dest 'datos.vhd' /NP >> $log

#
# restart CVS services
#
net start cvsnt >> $log
net start cvslock >> $log

#
# write footer
#
write "`r`n`r`n" (stars) >> $log
write "Backup procedure finished at " `
      ([datetime]::Now.ToString()) >> $log
write (stars) >> $log

write "Backup done. Check log file for details."
write $log

Here's the source as well.

Technorati tags: ,


Tomas Restrepo

Software developer located in Colombia. Sr. PFE at Microsoft.