How do I make quick automatic backup with using PowerShell

Paweł Świderski
4 min readFeb 25, 2018
Keep your data safe on the external drive. Source of the picture.

Making copies of the important files is crucial for everyone. Especially everyone that creates artifacts like computer programs.

You could say I don’t need any additional backup, I use code version systems and cloud synchronization apps like Google Drive, Dropbox, but it can be not enough. Do not make your sensitive data in risk of lost. You should always keep your data in at least 3 different places that are separated by physical location, device connectivity etc.

In this article I would not write about security of the data. I assume that other mechanisms will provide you sufficient level of data security. The topic is data safety, in another words — the protection against the data lost.

Why should I backup my files?

There are lots of different bad situations that can happen. Fortunately all of them are rare. Anyway you should protect your data in the same way you protect yourself and your family by buying proper insurance policy. So protect it, just in case.

What bad can happen:

  • hardware breakdown
  • hardware lost e.g. theft
  • software bug/virus/failure
  • data removal by mistake.

Protect data

I practice to maintain 3 backups that are separated through each other. Here there are:

  • original files located on the disk of the workstation
  • cloud copy that is synchronized constantly or version control system
  • copy on external disk drive that is synchronized once a week or biweekly.

Cloud backup is reliable if it saves versions of files, then it protects against data removal by mistake. Another problem with cloud systems is that you can lose access to your account. That situation is very unlikely (especially with two-factor authentication) but it can happen.

This is why you should have additional backup on additional physical hardware disk.

Solution

Solution I propose is simple and easy in use. It is a PowerShell script that copies files, that have changed, from one location to another.

When I was choosing technology I was thinking about script languages that OS supports. Those ones are the most efficient when they’re dealing with files. I could choose from Bash, Batch or PowerShell. Of course there are more script languages but I wanted to make a decision from the ones that are the most popular. I’ve chosen PowerShell because this is a modern multi-platform script technology. PowerShell console is available out of the box in Windows since XP SP2 but it can be used also with Linux and OS X.

Script

The whole backupScript.ps1 looks as follows:

Class DirToBackup
{
[String]$path
DirToBackup([String]$path) {
$this.path = $path
}
}
$defaultListOfExcluded = "D:\backup\listOfExcluded.txt"
$pathFromPrefix = "D:\"
$pathToPrefix = "F:\Backup\"
Write-Output "Plug external disk drive. It should be visible as F drive"
pause
$dirsToBackup = @(
New-Object DirToBackup "backup"
New-Object DirToBackup "development"
New-Object DirToBackup "Dropbox"
New-Object DirToBackup "Google"
)
$dirsToBackup | ForEach-Object {
mkdir -Path $($pathToPrefix + $_.path) -Force
xcopy $($pathFromPrefix + $_.path) $($pathToPrefix + $_.path) /D /S /Y /H /EXCLUDE:$defaultListOfExcluded
}
pause

I decided to represent directory to backup by instance of class DirToBackup. In this version each object holds only path to the dir. In the future modifications you can specify more properties e.g. list of files that you do not want to backup from the directory.

Variable defaultListOfExcluded is a path to the file that contains list of files or extensions of files that you do not want to backup. Example listOfExcluded.txt list looks as follows:

\build\
\.idea\
\.DS_Store\
\captures\
.iml\
\target\
\node_modules\
\out\

Backslashes mean that the name of dir or file starts or ends. For example if you would like to exclude files with txt extension then you should add

.txt\

On the other hand this:

\fileName

will ignore all files and dirs that start with fileName.

pathFromPrefix is a prefix for paths given by instances of DirToBackup.

pathToPrefix is a location that files will be copied to.

dirsToBackup is a list of DirToBackup. The “backup”, “development”, “Dropbox” are names of folders located in “D:\” (pathFromPrefix).

The main part of the script is a loop that goes through all dirsToBackup elements and executes mkdir and xcopy functions.

mkdir creates directories in the target location. Flag -Force makes possible to not error the script when directory’s already exists.

xcopy do the job of copying from one location to another with ignoring excluded paths given from the defaultListOfExcluded file. There are few flags that are very important. Meaning of them is taken from Microsoft Support page:

  • /D — Copies only those files whose source time is newer than the destination time.
  • /S — Copies directories and subdirectories except empty ones.
  • /Y — Suppresses prompting to confirm you want to overwrite an
    existing destination file.
  • /H — Copies hidden and system files also.

Run the script

in Windows, by right clicking on the script file and choosing “Run with PowerShell”. Then you see an information that you should connect your computer with external drive that should be disk F. Click any key, script is making a backup. After the process you will see a report on how many files was copied.

Conclusions

I hope you will be using my script just like I am.

If you have any questions or comments. Please let me know. Constructive feedback is appreciated.

--

--