Showing posts with label Robocopy. Show all posts
Showing posts with label Robocopy. Show all posts

26 Jan 2018

Finddocx - a script to find and backup documents from the C:, where ever they may be hiding!

Are you working in IT support, have you ever had to backup user documents from the C: drive before replacing or re-imaging their computer? I've had to do this and I know what a nightmare it can be. Copying the Documents folder is not enough, there are always more user documents elsewhere, potentially anywhere on the computer! Sometimes the user doesn't even know where their treasured files are.

In this article we'll look at a solution, to use Robocopy to scan the C: drive for document files and back them up (for example; copy to a USB flash drive).


Robocopy
This is a free command line program included with Windows as standard. In my example here I'll be using Windows 7 but this will also work for Windows 10 as well.

If you'd like to learn more about the basic functionality of Robocopy please see my previous article on the subject: https://mgxp.blogspot.ch/2015/01/robocopy-backup-and-file.html


Finddocx 
Finddocx is just a name I've given my script. It consists of two files, a command (batch) file and a Robocopy job file.


Finddocx.cmd (command file)
The command (batch) file, double click to execute. This contains the source and destination locations. Here's what it looks like inside:

ROBOCOPY C:\ %username% /JOB:finddocx 
pause

The first line is as follows:
ROBOCOPY <source> <destination> /JOB:<jobfile>

In my example I will search the entire C: drive (C:\). The backup files will be stored in a folder named after my own Windows username, the %username% is a variable. The /JOB:finddocx will use the finddocx.rcj for parameters.

The pause command at the end is just to stop the command window from closing when the script ends.


Finddocx.rcj (job file)
The job file. It contains various parameters including which file types to backup. The following is inside the file:

:: Finddocx example job file
:: mgxp.blogspot.ch
:: January 2018

:: Use two colons :: to disable a command from running. 
:: This job file is a modified version of one generated by Robocopy.
:: See Robocopy /? for more details. 

:: 
:: Include These Files :
::
 /IF  :: Include Files matching these names
  *.doc?
::  *.xls?
::  *.ppt?
::  *.pdf
::  *.txt
::  *.png
::  *.jpg
  
::
:: Exclude These Directories :
::
 /XD  :: eXclude Directories matching these names
  AppData
::
:: Copy options :
::
 /S  :: copy Subdirectories, but not empty ones.
 /COPY:DAT :: what to COPY for files (default is /COPY:DAT).
 /PURGE  :: delete dest files/dirs that no longer exist in source.
::
:: Retry Options :
::
 /R:0  :: number of Retries on failed copies: default 1 million.
 /W:30  :: Wait time between retries: default is 30 seconds.
::
:: Logging Options :
::
 /LOG+:log.txt :: output status to LOG file (append to existing log).
 /TEE  :: output to console window, as well as the log file.

In the above job file, where you see two colons :: it means that the text on that line is ignored. In other words, :: means a comment.

/IF
You'll notice that there's an /IF command at line 12. Below it there is a list of file types. In my example above only *.doc? is enabled. However, if you wish, remove the :: next to the others to backup more file types. You could also extend this list and add even more file types.

/XD
Add folders here that you do not want it to search. I've put AppData here because it was just giving 'access denied' errors so I prefer to exclude it. You can add more folders here as needed.


Setup Finddocx
The following is an example only:

  1. Insert a USB flash drive
  2. Create a folder on it called "finddocx"
  3. Copy the source code for finddocx.cmd (above)
  4. Open Notepad and Paste
  5. Save the file to the USB drive into the finddocx folder as finddocx.cmd
  6. Copy the source code for finddocx.rcj (above)
  7. Open Notepad (a new window) and Paste
  8. Save the file to the USB drive into the finddocx folder as finddocx.rcj
  9. Edit the finddocx.cmd and rcj files as needed (you may wish to add more file types to search for, you may wish to change the destination folder, etc). 

Run Finddocx
Here's what happens if I double click my finddocx.cmd, a command window will open you'll see thousands of files zoom by:


Don't worry, don't touch, just let it work. Once it has finished press a key to close it.

Look in the folder on your flash drive:

A log.txt file has been created, open in Notepad to see what happened, what was backed up, if there were errors, etc.

You'll see a folder with your username, in my example "Michael". This is where the backup is. Double click Michael (or whatever your folder is called) and you'll see something like this:


There will be sub-folders. These represent the folders from your C: drive. If you explorer these folders you'll find the various doc files that were backed up. The folder structure where they were found on your C: is maintained and replicated here.


Notes
  • The second time you run Finddocx with the same user it will delete the existing files on your backup destination (USB drive) and replace them with new copies from the C: drive.
  • Using the %username% variable means you can take your USB flash drive from computer to computer backing up data. The backed up files will be stored in separate folders on your USB drive.
  • Instead of using %username% you could edit the finddocx.cmd and change it to backup to %computername%.
  • If you want to create your own Robocopy job file from scratch you can do. You must use the /SAVE:<job> parameter. Please see the Robocopy documentation or Robocopy /? for details.


Disclaimer
Use at your own risk! The script I've included here is just an example to get you started. I am not guaranteeing anything. I am not responsible if you mess something up! Take care, especially with user data.


Conclusion
This script is just a start, using it and the power of Robocopy you can easily backup user files. Of course it can't replace a proper backup solution but as explained in the introduction, it could be a simple way to make a copy of important user files prior to replacing a computer or re-imaging it. I hope you found this article useful.






6 Jun 2016

RoboBackup7z - an AutoIt script to backup files using Robocopy and 7za


Previously I wrote an AutoIt script called RoboBackup. That script used Robocopy to automate backing up files into three (or more) folders. It cycled through those folders synchronising files each time.

In this article I am going to explain a slightly more advanced script that also backs up files but does so by using both the Robocopy command and also the 7-Zip 7za.exe command line program. I've called this backup script 'RoboBackup7z'.

The idea behind RoboBackup7z is that the latest backup is a synchronised folder managed by Robocopy. But previous Robocopy backups are archived in 7z files for safe-keeping. This means the latest data is easy to recover and the previous backups are still available but are smaller compressed files you could copy elsewhere - to DVD or cloud storage perhaps.

First let's have a look at how this script works and after how it is put together. I hope you'll find this interesting.


Setup
The idea is that you create a folder on a removable drive or USB flash drive and run the script from there. In the following screen shot you can see my example:


In the above example I have an external USB hard disk drive that is drive G: on my computer. I am backing up files from my D: drive (source) and so I've created a folder called "D" (I could've called it anything but this makes sense to me).

Inside the "D" folder I have the following files:

  • 7za.exe
  • RoboBackup7z.au3
  • RoboBackup7z.exe
  • RoboBackup7z.ini


Edit RoboBackup7z.ini


Add a folder to backup. This can also be a drive letter, in our example we'll use D:. This backs up all folders and files found on drive D:.


Run
Double click RoboBackup7z.exe

The first time it runs the following will be created:

  • A folder called Backup where a copy of the folders and files will be made.
  • A log file named yyyymmdd_hhmmss_LOG.txt (the RoboCopy log showing what was backed up).

The second time it runs the following happens:

  • A folder called Archive is created
  • 7-Zip runs, it adds all the folders/files from the Backup folder to a 7z archive file in the Archive folder.
  • It names the 7z with the name of the last backup log file (it gets this from the ini).
  • It splits the 7z file into 300MB separate files.
  • The log file is moved to the Archive folder.
  • RoboCopy runs again, the Backup folder is updated with new files, a new log file appears in the script folder.

The third time it runs:

  • The same as the second time, a new 7z is created and stored in the Archive folder with the log file. RoboCopy runs again to update the Backup folder and create a new log file.


In the above example I have run the script more than twice. The Backup folder contains the files of the latest copy and the Archive folder contains the 7z files. The log is the latest log file.

Here's what it looks like inside the Archive folder:


In the above screen shot you can see the txt files are the logs. You can open these in Notepad. They are the original log files produced by Robocopy when it originally ran.

The other files are the 7z files. They are in 300 MB chunks. To view and extract files open the first one (001) with 7-Zip for Windows.

IMPORTANT: There is no control over how much disk space is used or how many backups can be made. You must manage the disk space yourself. Move files from the Archive folder to another location as needed.


Source Code
In the following screen shot you'll see the code as I see it when I writing it. On the left hand side you can see the line numbers. Below the image I'll explain line by line what the script does but first a few basics, here's how the colour coding works:

Green - comments, text to explain what the script is doing. Therefore lines 1 to 11 is just for information.
Black - variables, they also start with a dollar sign $
Blue - functions like IF.
Orange - operators like <>=
Red - text



--- Line 15
This defines the $ini variable that identifies the RoboBackup7z.ini file. It's a configuration file containing the drive/folder to backup and the name of the last log file.

--- Line 17 to 25
These are the variables and what their initial values are. This includes reading data from the ini file.

--- Line 27 to 39
First this checks if the 7za.exe exists. If it does not, it'll skip this part.
If 7za.exe does exist then it'll check for the destination folder (Backup), if this exists then it'll run the 7za program to archive the files from the destination (Backup) folder.

--- Line 32
This is the 7za line. It compresses the files from the Backup folder into one or more 7z compressed (zipped) files. The 7za.exe has a number of parameters:

-r = recurse, meaning to archive sub-folders/files

-v300m = this splits the 7z archive file into smaller files (volumes). In this case we're splitting to 300MB files. You can change this to any size that fits your needs. If you do not use this parameter then the archive will be one large 7z file. Such a file might be very big, depending on what files you have to backup, larger than 2GB, therefore it makes sense to split to smaller files to make them easier to copy. Many services do not support files larger than 2GB.

--- Line 36 and 37
These copy the log file to the Archive folder. This is so the log is stored along with the backup it was made from.

--- Line 42
Save the date (name of the log file) to the ini file. This so it can be used the next time the backup runs to identify the 'last' backup.

--- Line 45
Runs ROBOCOPY to synchronise files from the source to the backup (destination) folder.

--- Line 57
Open Notepad and display the log file. This will happen at the end and shows you what has happened during this backup. It's the usual output from Robocopy.

--- Line 59
Exit just closes the script. You don't really need this but usually put it just to show where it ends.


My RoboBackup7z Script Source Code for Copy/Paste
Here's the same script as shown above in the screen shot. But this time it's text so you can copy/paste it and use it. Feel free you change it :-)

#cs ----------------------------------------------------------------------------

RoboBackup using 7-Zip

 AutoIt Version: 3.3.10.2
 Author:         Michael Gerrard / http://mgxp.blogspot.com

 Script Function:
 Create a backup using Robocopy and archives using 7-zip

#ce ----------------------------------------------------------------------------


; Set variables
$ini   = @ScriptDir & "\RoboBackup7z.ini"

$FolderToBackup = IniRead($ini, "Settings", "FolderToBackup", @MyDocumentsDir)
$source     = '"' & $FolderToBackup & '"'
$dest   = @ScriptDir & "\Backup"
$archive  = @ScriptDir & "\Archive"
$today   = @YEAR & @MON & @MDAY & "_" & @HOUR & @MIN & @SEC
$log      = @ScriptDir & "\" & $today & "_LOG.txt"
$7za   = @ScriptDir & "\7za.exe"
$last   = IniRead($ini, "Settings", "LastBackup", "00000000_000000")
$7zBackup  = $archive & "\" & $last

; Check if a backup was done before, if so, archive it to a 7z file
If FileExists($7za) Then
 If FileExists($dest) Then
  DirCreate($archive) ;make sure the archive folder exists

  RunWait(@ComSpec & " /c " & $7za & " a -r -v300m -w -y " & $7zBackup & " " & _
  $dest & "\*.*", @ScriptDir, @SW_MINIMIZE) ; add to 7z archive file

  ; Move the log to the archive folder
  FileCopy(@ScriptDir & "\" & $last & "_LOG.txt", $archive)
  FileDelete(@ScriptDir & "\" & $last & "_LOG.txt")
 EndIf
EndIf

; Write today's date and time to the ini
IniWrite($ini, "Settings", "LastBackup", $today)

; Run RoboCopy
RunWait(@ComSpec & " /c ROBOCOPY " & $source & " " & $dest & _
" /MIR /R:2 /LOG:" & $log & " /TEE" & " /XF desktop.ini ", @ScriptDir, @SW_MINIMIZE)

; /MIR = mirror
; /R:2 = retry twice
; /LOG: = record to a log file
; /TEE = output to the screen too
; /XF desktop.ini = don't copy the desktop.ini file (avoids getting the My Documents folder name)

; Display the log file
If FileExists($log) Then
 Run("notepad " & $log)
EndIf
Exit


Other required files
For the script to work you'll need Robocopy, this command is included with Windows as standard. Also you'll need the 7za.exe file, you can download it free of charge from http://www.7-zip.org/
For more detailed information please see the following article:
https://mgxp.blogspot.ch/2016/04/7-zip-7za-command-line-zip-tool.html


Conclusion
It's amazing what you can do with free tools! AutoIt is of course the easiest way to make a script that's compact and simple. Just look at how few lines of code I used. Robocopy and 7za too, excellent. This script works very nicely and I've been using it for several months now. But it has some shortcomings. For instance, I have to manage the disk space myself. Also it only backs up from one location (drive or folder location and sub-folders). But this is fine, it means I have more work to do in the future!


Disclaimer
The script here is just an example. I do not provide it with any guarantee. I'm not advocating this script be used for anything specific, this article is just to demonstrate what can be done. Use at your own risk! 


Reference

AutoIt
https://www.autoitscript.com

Robocopy at Wikipedia
https://en.wikipedia.org/wiki/Robocopy

Various articles about Robocopy
https://mgxp.blogspot.ch/search/label/Robocopy
7za, the 7-zip command line program

If you are looking for some help with creating self-extracting archives, please see my previous articles specifically on this more advanced subject:
PART 1 - http://mgxp.blogspot.com/2010/07/create-self-extracting-archive-exe.html
PART 2 - http://mgxp.blogspot.com/2013/01/create-self-extracting-archive-exe-part.html

23 Feb 2016

When deleting files - Source Path Too Long

In Windows, when deleting folders/files have you seen this error message:

Source Path Too Long
The source file name(s) are larger than is supported by the file system. Try moving to a location which has a shorter path name, or try renaming to shorter name(s) before attempting this operation.


Windows 8.1 screen shot - it varies depending on your version of Windows.


If copying not deleting?
If you received a similar message when copying files, please see a previous article I wrote on how you may use RoboCopy to help in that scenario:
http://mgxp.blogspot.ch/2015/10/when-copying-files-source-path-too-long.html


Why?
It's due to a limitation of 256 characters of the folder and file name length combined.


Solution
To delete a folder that contains many sub-folders and files with very long file names, there are a few solutions out there including some third-part tools. You could also use the SUBST command to shorten the path but that may not be successful depending on the length of the files/folders you have. However, I've discovered that it's possible to use RoboCopy, the free command line tool that comes with Windows to do the job perfectly!

RoboCopy is for copying files or synchronising folders. I've written a few articles on this tool already - click here for more information. RoboCopy can understand and work with file paths longer than 256 characters. RoboCopy allows you to synchronise folder 'A' with folder 'B'. Imagine that folder 'A' is empty, there are no files in it - if we synchronise that folder with a folder that does have files (folder 'B') then that folder will also be empty. That's the solution. The following is an example of how this works in practice.


Example
I have a folder called 'Folder with very long names'. Inside this folder I have many nested sub-folders with long names. Inside those sub-folders I have many files with very long names. When I try to delete the folder (and sub-folders/files) I get the 'Source Path Too Long' error. 

At the same level of the folder I want to delete 'Folder with very long names', create a new folder called 'Empty':


The folder called 'Empty' will have nothing in it (it should be empty!). 

Open a command window (press Win+R and type CMD [Enter])

At the command prompt change to the same drive/folder where you have the folders 'Empty' and 'Folder with very long names'. 

At the command prompt type the following command:
ROBOCOPY Empty "Folder with very long names" /MIR  [Enter]

The contents of the folder 'Empty' will be mirrored (/MIR parameter) to the folder 'Folder with very long names'. Because the folder 'Empty' has nothing in it, the contents of the destination folder 'Folder with very long names' will end up with nothing in it! Perfect! Now you can remove (delete) the folder yourself without any trouble.


Conclusion
It's very annoying that Windows doesn't properly support folder and file names longer than 256 characters. It's as if they added the functionality and forgot to update all the tools. Even if you try the command line DEL command you can't delete a folder with very long folder/file names. Using RoboCopy is hassle free, it works. Again it has come to the rescue because it can also solve this issue for copying files


16 Oct 2015

When copying files - Source Path Too Long

In Windows, when copying files have you seen this error message:

Source Path Too Long
The source file name(s) are larger than is supported by the file system. Try moving to a location which has a shorter path name, or try renaming to shorter name(s) before attempting this operation.

Source Path Too Long
Windows 8.1 screen shot - it varies depending on your version of Windows.

It's due to a limitation of 256 characters of the folder and file name length combined. 

The simplest solution I've found to this issue is to use RoboCopy to copy the files. It ignores the 256 character limit and just copies everything, no questions asked!

From the command line (CMD):
ROBOCOPY [source] [destination] /s  

For more help please have a look at the following article about RoboCopy:


5 Aug 2015

Robocopy - backup and file synchronisation PART 2

In my first article about Robocopy (click here) I gave an example of how to use the Robocopy command and how to make a simple command file (batch file) to backup (synchronise) a folder.

In this article we will look at how we can automate the use of Robocopy further. This time we'll use AutoIt to control the parameters used and how our backup runs. What's the advantage of using AutoIt over a command file? You can do lots of clever things with AutoIt that would be painful to do with a mere command file.

AutoIt is a free scripting language for Windows. It is easy to learn and creates standalone EXE files. For more on AutoIt click here.


Example Scenario
I want to do the following:
  • Backup files to a USB external hard drive (storage that can be detached from the computer is best in case computer dies your backup won't die with it).
  • A simple solution where I can double click a file and it will backup, no prompts.
  • I don't want to overwrite my backup each time I run it. Imagine if I make a mistake, run the backup, my mistake would be backed up. I need a solution for this.
  • A log or report of what's been backed up.
  • It should be efficient and fast, only changed files backed up.
Of course your needs maybe different but I hope that at least this article will give you a few ideas as to how you can use Robocopy and AutoIt together to do some amazing things! 


My AutoIt and Robocopy Example Solution
I wrote a script call RoboBackup.au3. I wrote it using AutoIt in the custom SciTE editor available from AutoIt's website.

In the above example scenario I state that I'd like to be able to run the backup multiple times and not overwrite each time. To do this I decided to make my script manage three backups (by default). What this means is that I'll have three folders; backup1, backup2, backup3. Each time the backup script is run it will copy files to each folder consecutively (one by one). Because it's using Robocopy not all the files will be copied each time, only those changed since. However, it won't be since the last backup, but since three backups ago. That's a compromise on my part otherwise the script might've become more complex, I want to keep it as simple as possible. Anyway, the general concept is that you can have a maximum of three backups at any one time. This should cover most situations. 


In the above screen shot you'll see the code as I see it when I writing it. On the left hand side you can see the line numbers. I'll explain line by line what the script does but first a few basics, here's how the colour coding works:

Green - comments, text to explain what the script is doing. Therefore lines 1 to 11 is just for information. 
Black - variables, they also start with a dollar sign $
Blue - functions like IF.
Orange - operators like <>=
Red - text
Yellow - internal 'macros', these start with @ and are like system variables.

--- Line 15
So that the script 'knows' which backup folder (1, 2 or 3) to use there has to be an INI configuration file to hold these settings. Line 15 creates a variable $ini with the name of the file: RoboBackup.ini. The @ScriptDir means the current folder.

--- Line 16
$number is the number of backups folders you want to use. By default there are three. This can be changed though, if you put a different number in the ini file, that number will be read and used instead of 3. 

--- Line 17
$count is used to keep count of which folder was backed up last.

--- Line 19 
Folder to backup - this script will backup one folder but all sub-folders are included. You can specify the folder name in the ini file or it will use the My Documents folder.

--- Line 20
In case the folder to backup has spaces in it, double quotes are added. This is important because the $source variable will be used on the Robocopy command line where any spaces without double quotes " " would cause trouble.

--- Line 21 
The destination is Backup1, Backup2, etc. The number on the end comes from the ini, the $count variable. This means you'll maintain multiple backups.

--- Line 22
The log file is named as RoboBackupLOGx.txt where "x" is the number ($count) of the backup. If you are using the default settings there will only be three log files created. Each time the backup runs the old log file with the same name will be overwritten. I've done it this way so there are not lots of log files created. However, you might prefer to have more comprehensive logging than the simple three log file approach I've used. For example, you could change this line to create a log that has the date in it, meaning that a log file will be created every time the script is run and it will remain until you delete it (it will not be overwritten unless you run the script twice on the same day):

$log = @ScriptDir & "\RoboBackupLOG" & @YEAR & @MON & @MDAY & "_" & $count & ".txt"

The above would create a file name like this: RoboBackupLOG20150803_1.txt
I've put the date backwards, year-month-day, to ensure that all the logs will line up when you look at them in explorer. The last digit is the count, it'll tell you which backup folder to look in.

--- Lines 25 and 26
These lines increment the $count. When the $count is greater than the $number, it resets. This is for naming the backup folders 1, 2, 3 and then going back to 1 and starting again.

--- Lines 29 and 30
This is actually one line. At the end of line 29 you'll see an underscore "_" in AutoIt this means to start a new line. The only purpose here is for readability as it's a long command line so I wanted to split it to two lines. Just imagine it is one long line though. You can see that this is the Robocopy command itself! It's run using the AutoIt function RunWait(). It opens a command window and runs a command. In my script I've done this but minimised the command window. It's nice like that because you can check it if you want. However, if you prefer you can hide it altogether. 

--- Lines 32 to 36
Comments, an explanation of the command line parameters for Robocopy that I used. For line 36, regarding the RoboCopy /XF command, this is to stop RoboCopy coping the desktop.ini file. The Desktop.ini sometimes has a command inside it to show a different folder name. This is the case for My Documents and it can cause confusion. Not including the Desktop.ini means that the folders appear with their real names.

--- Line 39
Open Notepad and display the log file. This will happen at the end and shows you what has happened during this backup. It's the usual output from Robocopy. 

--- Line 40
Exit just closes the script. You don't really need this but usually put it just to show where it ends.



My RoboBackup Script Source Code for Copy/Paste
Here's the same script as shown above in the screen shot. But this time it's text so you can copy/paste it and use it. Feel free you change it :-)

#cs ----------------------------------------------------------------------------

RoboBackup

 AutoIt Version: 3.3.10.2
 Author:         Michael Gerrard

 Script Function:
 Create three backups of the same data using the RoboCopy mirror function

#ce ----------------------------------------------------------------------------


; Set variables
$ini   = @ScriptDir & "\RoboBackup.ini"
$number   = IniRead($ini, "Settings", "Number", "3")  ; number of backups
$count   = IniRead($ini, "Settings", "Count", "1")   ; count of backups (where we are now)

$FolderToBackup = IniRead($ini, "Settings", "FolderToBackup", @MyDocumentsDir)
$source   = '"' & $FolderToBackup & '"'
$dest   = '"' & @ScriptDir & '\backup' & $count & '"'
$log   = @ScriptDir & "\RoboBackupLOG" & $count & ".txt"

; Increment the count
If $count >= $number Then $count = 0
IniWrite($ini, "Settings", "Count", $count + 1)

; Run RoboCopy
RunWait(@ComSpec & " /c ROBOCOPY " & $source & " " & $dest & _
" /MIR /R:0 /LOG:" & $log & " /TEE" & " /XF desktop.ini ", @ScriptDir, @SW_MINIMIZE)

; /MIR = mirror
; /R:0 = retry zero times
; /LOG: = record to a log file
; /TEE = output to the screen too
; /XF desktop.ini = don't copy the desktop.ini file (avoids getting the My Documents folder name)

; Display the log file
Run("notepad " & $log)
Exit

Here's what to do:
  • Select the above text, press Ctrl-C to copy it. Switch to your text editor (SciTE) and press Ctrl-V to paste. 
  • Make sure you have AutoIt installed. 
  • Compile and run the script. 

RoboBackup.ini
The RoboBackup.ini is just a text file. You can create this yourself in Notepad or in any case you'll see it is created by the script the first time you run it. I'll look like this:

[Settings]
count=2
number=3
FolderToBackup=C:\YourData

The count is the variable the script reads and it will change every time you run the script. In my example above it says "2" so the latest backup is number 2. Next it'll be 3 and then 1, etc. This line is generated automatically by the script. 

The number line is optional. I have it set to 3, the default. But you can change this to 4 or 8, whatever. This is the number of backup copies you'd like.

The FolderToBackup line is also optional. By default it'll backup your My Documents folder. If you want to backup something else you can specify the folder here. All sub-folders below it will be included in the backup.


Where to put the files
To use the script put the compiled RoboBackup.exe and the RoboBackup.ini files in the destination drive. For example, if you have a USB flash memory drive or a USB hard disk drive, make a folder called backup on it, copy the files RoboCopy.exe (and RoboBackup.ini) into that folder. Run the script from that folder, it'll create the Backup1, Backup2, etc, folders below that point. This is a nice way of working because it means you can unplug your removable backup drive and put it somewhere safe. The next time you want to backup, plug it in and run RoboBackup.exe. 


IMPORTANT: Disk Space 
The main purpose of this script is to make a number of backup copies. This is so that you don't overwrite your backup each time, that you have a chance to recover files from a previous backup. However, to make this all work you do need to be aware of the disk space required for the backup. I've not included any disk space checks in the script (maybe in version 2!!! ;-)). Therefore look at what you want to backup and make sure your backup drive has enough space to accommodate multiple copies of that data. Also factor in that the data will grow in size as you use it. 


Conclusion
RoboCopy is a powerful command line tool for file/folder synchronisation on its own. Combined with AutoIt you can make a script that provides much more control. Here I've provided just one example and I hope it's given you ideas for how it can be adapted to solve your backup requirements. Often the simplest form of backup is best. Imagine with this, restoring is easy, just copy the files. Also for the backup itself it's easy with few requirements, just one standalone exe file (your script, RoboCopy is included as part of Windows by default) and enough disk space. This is a free solution too!


Disclaimer
The script here is just an example. I do not provide it with any guarantee. I'm not advocating this script be used for anything specific, this article is just to demonstrate what can be done. Use at your own risk! 


Reference

AutoIt 

My first RoboCopy article explaining the Robocopy command itself:


19 Jan 2015

Robocopy - backup and file synchronisation PART 1

For Windows there are many options for backing up or synchronising files between folders. One of them is a nice little program called FreeFileSync that has a simple user interface but recently I've been using Robocopy.

Robocopy comes as part of Windows (Vista, 7, 8 and other versions). There's no graphical user interface but that's fine because you can easily automate your backup or file sync. In this article I'll explain how to use Robocopy in a simple way to backup files from your 'Documents' folder to another drive, perhaps a USB flash drive.


Command Line
The Robocopy command is like this:
ROBOCOPY <source> <destination> [options]
You can find full details on Microsoft's Technet:
http://technet.microsoft.com/en-us/library/cc733145.aspx



Example 1 - a simple start
Let's say you want to backup the Documents folder from your C: drive to a USB flash drive - here's what to do. When you insert the USB flash drive it'll be given a drive letter. Below in our example it is drive K: but yours might be something else.

Windows 7 - click Start | CMD [Enter]
Windows 8.1 - right click | Command Prompt

At the command prompt type the following:
ROBOCOPY "c:\users\michael\documents" "K:\backup" /MIR [Enter]
Replace michael with your username.
Replace K: with the letter of the USB flash drive you have plugged in.

The /MIR mirrors the files from the documents folder to the destination (K:\backup) folder. By 'mirror' it means to copy the files. The next time you run the same command line it'll copy any changes from the source to the destination. If you delete a file in the source then the same file will be deleted in the destination folder. This is folder synchronisation!


Example 2 - automate the above
Typing the above command is a bit of a pain, isn't it!?! Every time you want to backup your documents you have to remember all of that? There must be an easier way. The answer is to put this command line into a file that's called a command file (or batch file). You can double click the file to execute it.

We will create the command file on the USB flash drive, when you wish to run it, even if the USB flash drive has a different drive letter it'll still run, to do this the command line will be slightly different - here's what to do:

Open Windows Explorer and click the USB flash drive.

Create a new text file (open Notepad).

In Notepad enter the following single line:
ROBOCOPY %homedrive%%homepath%\documents \backup /MIR /LOG:\RoboLog.txt /TEE /R:0 /XF desktop.ini

Save the file as Robo.cmd on the USB flash drive.

Double click Robo.cmd, it'll copy all the files from documents to a folder called Backup on your USB flash drive.


Example 2 - explanation
Here I've replaced C:\Users\michael\documents with %homedrive%%homepath%\documents - this means I can use my command file on any computer (tested on Windows 7 but should work on other versions). There are two standard Windows variables:
  1. homedrive
  2. homepath
To tell the computer these are not just part of the folder/path name, add the percent signs % at the beginning and end of each variable. Add \documents on the end to complete it.

/LOG creates a log file called RoboLog.txt. Once the backup has finished you can open the log file in Notepad and see exactly what has happened, which files were updated, which were deleted, etc.

The /TEE parameter means the log file is updated and the results are also shown on the screen, in the command window.

The /R:0 is to set the retries to zero. This means if there is a problem with reading a file it'll move past it to the next file immediately. If you want to use the retry function then set it to /R:1 for one try or /R:2 for two, etc. For most people /R:0 makes sense though, using the multiple retries is mostly useful for reading files from network drives when files maybe shared.

The /XF means to exclude a file. In my example above I set it to exclude the desktop.ini file. The reason is that with this file present in the Documents folder, your 'backup' folder on the USB flash drive will appear as "My Documents". You can also use /XF to exclude other files you may not need in the backup.


Example 2 - finishing touch
Its' a good idea to add a pause command at the end of your command file. This will stop the command window from disappearing after Robocopy has finished. It's good to give you some on-screen feedback that it's finished. Your command file would have two lines:
ROBOCOPY %homedrive%%homepath%\documents \backup /MIR /LOG:\RoboLog.txt /TEE /R:0 /XF desktop.ini
PAUSE


Conclusion
Robocopy is a fast command line program. You can create a simple command file and run it whenever you need to backup. You could also schedule the command file to run at a predetermined time.

Because Robocopy only copies files, it doesn't compress or encrypt them, it is simple to restore them. This might be a limitation in that you may be worried about data security. On the other hand there are many times when a simple file copy makes more sense, fewer worries about your backup files being corrupted when you come to restore them.

Robocopy is reliable and easy to use. It's a good replacement for the old COPY and XCOPY commands. The folder synchronisation is such a welcome feature, it speeds up backups. Robocopy is also part of Windows so you know it's there - this makes it an ideal tool for those working in IT who don't want to use a third-party tool. In my experience using Robocopy I've not had any problem, it's a great little program, I recommend it.

PART 2 - I've written a second article that includes an example of an AutoIt script that, using Robocopy, will create a multi-copy backup. It's the next step, to make your backups a little more sophisticated.
http://mgxp.blogspot.ch/2015/08/robocopy-backup-and-file.html


Reference
Robocopy at Microsoft Technet:
http://technet.microsoft.com/en-us/library/cc733145.aspx [last accessed 19/01/2015]

Robocopy at Wikipedia:
http://en.wikipedia.org/wiki/Robocopy [last accessed 19/01/2015]