Hey, Scripting Guy! How Can I Back Up Specific Folders on My Windows Computer?

Hey, Scripting Guy! How Can I Back Up Specific Folders on My Windows Computer?

  • Comments 2
  • Likes

Hey, Scripting Guy! Question

Hey, Scripting Guy! I would like to be able to perform a backup of only certain folders on my Windows Vista laptop. I am concerned about running through the airport and having it getting bumped, dropped, or stolen. My job requires me to travel extensively, and I am just paranoid about a laptop crash and about losing my data. Can you help me?

- PC

SpacerHey, Scripting Guy! Answer

Hi PC,

Scripting Guy Ed agrees with you and shares your concerns. Wait while he goes and gets a cup of tea. He is out in Redmond, Washington, in the United States this week teaching a group of Microsoft engineers Windows PowerShell. In honor of his presence, it has been sunny with clear skies all week long.

One time a couple of years ago, while Ed was traveling to Quebec City, the airplane he was riding in hit what felt like a series of speed bumps in the sky, and his cup of hot coffee went onto his laptop. Because his laptop was not java enabled, it killed the laptop. (Interestingly enough, this is about the same time that Ed quit drinking coffee.) He lost two days of work on his Windows PowerShell book and became depressed for a week. After he arrived at the Microsoft office in Quebec City, he was able to get the laptop repaired (luckily, it was only the keyboard that was damaged, and the remainder of the laptop, including the data on the hard drive, emerged unscathed). When his laptop was repaired and running, he wrote the BackupFolderToServer.ps1 script you see just below. It has since served him well. We hope you will also find it useful, too.

BackupFolderToServer.ps1

param($source, $destination, $help)

function funHelp()
{
$helpText=@"
DESCRIPTION:
NAME: BackupFolderToServer.ps1 
Backes up files in a folder to a mapped drive. The destination
folder does not have to be present

PARAMETERS: 
-source      the source of the files and folders
-destination where the files are to be copied
-help        prints help file

SYNTAX:
BackupFolderToServer.ps1 -source c:\fso -destination h:\fso

Backs up all files and folders in c:\fso on local machine to
a mapped drive called h. The \fso folder does not need to 
exist on the h:\ drive.

BackupFolderToServer.ps1 

generates an error. the -source and -destination parameters
must be present

BackupFolderToServer.ps1 -help ?

Displays the help topic for the script

"@
$helpText
exit
}

if($help){ "Obtaining help ..." ; funhelp }
if(!$source -or !$destination) 
  { 
    $(throw "You must supply both source and destination.
 Try this BackupFolderToServer.ps1 -help -?") 
  }
Copy-Item -Path $source -destination $destination –recurse

In the BackupFolderToServer.ps1 script, we use the Copy-Item cmdlet to copy files in a particular folder to a mapped drive location on a server or some other device on which you wish to store your user data. This could be used to copy data to a portable storage device such as a flash memory card or a USB drive. It will also copy files to a user’s mapped home directory. The BackupFolderToServer.ps1 script begins with the param statement, which allows us to specify command-line arguments to the script. These arguments control how the script will run. This also saves the trouble of having to edit the script before using it. Such a script can be “driven” from a batch file that supplies a number of parameters. It can also be called from other Windows PowerShell scripts. This script defines three parameters: –source, –destination, and –help. Each of these parameters will be stored in the corresponding variable with the same name. This line of code is seen here:

param($source, $destination, $help)

We then create the funhelp function. The funhelp function is used to display a Help text message when the script is run with the –help parameter. The funhelp function begins with a declaration of the $helpText variable. This variable will be used to store a here-string. The here-string is used to allow us to type text in the manner in which it will be displayed on the screen. This saves time and reduces potential quoting errors. The Help text consists of three sections: the description of the script, the parameters the script will accept, and the syntax that is required. AFter the Help text is created, we display the contents of the $helpText variable on the screen and exit the script. The completed funhelp function is seen here:

function funHelp()
{
$helpText=@"
DESCRIPTION:
NAME: BackupFolderToServer.ps1 
Backes up files in a folder to a mapped drive. The destination
folder does not have to be present

PARAMETERS: 
-source      the source of the files and folders
-destination where the files are to be copied
-help        prints help file

SYNTAX:
BackupFolderToServer.ps1 -source c:\fso -destination h:\fso

Backs up all files and folders in c:\fso on local machine to
a mapped drive called h. The \fso folder does not need to 
exist on the h:\ drive.

BackupFolderToServer.ps1 

generates an error. the -source and -destination parameters
must be present

BackupFolderToServer.ps1 -help ?

Displays the help topic for the script

"@
$helpText
exit
}

We now need to check for the presence of the $help variable. If we find the $help variable, we display a progress message and call the funhelp function. The semicolon allows us to run two separate commands on the same line of text. This command is seen here:

if($help){ "Obtaining help ..." ; funhelp }

We need to look for the presence of the two mandatory parameters. These parameters are the –source and the –destination parameters. The source is a local path that must exist on the machine and for which you have rights to the folder. The destination does not have to have a folder that is the same name as the source. The destination can be a UNC path, a mapped network drive, a local drive, or even another folder on the same drive as the source. If these two parameters do not exist, we use the throw statement to display an error message and exit the script. This error is seen here:

Image of the error displayed by the throw statement

 

In the error that appears, we point the user to the Help text syntax. The code that does this is seen here:

if(!$source -or !$destination) 
  { 
    $(throw "You must supply both source and destination.
 Try this BackupFolderToServer.ps1 -help -?") 
  }

Now it is time to copy the files. The Copy-Item cmdlet accepts the –path parameter that is contained in the $source variable. We use the $destination variable to feed the –destination parameter, and we use the –recurse switch to cause nested folders to also be copied. The source and destination variables are created via the parameters of the same name from the command line when the script is run. This line of code is seen here:

Copy-Item -Path $source -destination $destination –recurse

Well, PC, Ed has finished his cup of tea (a nice aromatic mint tea). We hope you will be able to use the BackUpFolderToServer.ps1 script. Ed keeps a 4 GB SD chip in his laptop while he is traveling, and he uses BackupFolderToServer.ps1 to copy his working files to the SD chip about once each hour. Join us tomorrow as we continue to look at managing user data. We also hope you are getting in shape for the Summer Scripting games. Warmups begin next week. For all the details, follow us on Twitter. Until tomorrow, peace!

 

Ed Wilson and Craig Liebendorfer, Scripting Guys

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment