Mass copy for backup in windows

What I do love as a system administrator is backup things. Documents, picture, archieves, anything. And I always want to do it in a simple but powerful command prompt or console.

In microsoft there are two command available in the command prompt to do this.

First : xcopy
This is the syntax

xcopy /s /d *.doc source destination

source could be D: and destination could be E:
/s means create also the subdirectories but not empty one
/d means if there is a similar file in the destination, copy only if the source is newer

Second : robocopy
Microsoft said, this command will not be found in basic microsoft or home edition. Only in network windows or professional edition. I read the how to command, it is quite powerfull. It can copy over network through samba or something like \\192.168.xxx.xxx\destination. The syntax is :

robocopy source destination *.doc /s /xo /r:2

/s means create also the subdirectories but not empty one
/xo means if there is a similar file in the destination, copy only if the source is newer
/r:2 means the number or retry when the command try to access to denial directory or error directory

This is the full option for xcopy

Syntax

xcopySource [Destination] [/w] [/p] [/c] [/v] [/q] [/f] [/l] [/g] [/d[:mm-dd-yyyy]] [/u] [/i] [/s [/e]] [/t] [/k] [/r] [/h] [{/a|/m}] [/n] [/o] [/x] [/exclude:file1[+[file2]][+[file3]] [{/y|/-y}] [/z]

Parameters

Source : Required. Specifies the location and names of the files you want to copy. This parameter must include either a drive or a path.

Destination : Specifies the destination of the files you want to copy. This parameter can include a drive letter and colon, a directory name, a file name, or a combination of these.

/w : Displays the following message and waits for your response before starting to copy files:

Press any key to begin copying file(s)

/p : Prompts you to confirm whether you want to create each destination file.

/c : Ignores errors.

/v : Verifies each file as it is written to the destination file to make sure that the destination files are identical to the source files.

/q : Suppresses the display of xcopy messages.

/f : Displays source and destination file names while copying.

/l : Displays a list of files that are to be copied.

/g : Creates decrypted destination files.

/d[:mm-dd-yyyy] : Copies source files changed on or after the specified date only. If you do not include a mm-dd-yyyy value, xcopy copies all Source files that are newer than existing Destination files. This command-line option allows you to update files that have changed.

/u : Copies files from Source that exist on Destination only.

/i : If Source is a directory or contains wildcards and Destination does not exist, xcopy assumes destination specifies a directory name and creates a new directory. Then, xcopycopies all specified files into the new directory. By default, xcopy prompts you to specify whether Destination is a file or a directory.

/s : Copies directories and subdirectories, unless they are empty. If you omit /s, xcopy works within a single directory.

/e : Copies all subdirectories, even if they are empty. Use /e with the /s and /t command-line options.

/t : Copies the subdirectory structure (that is, the tree) only, not files. To copy empty directories, you must include the /e command-line option.

/k : Copies files and retains the read-only attribute on destination files if present on the source files. By default, xcopy removes the read-only attribute.

/r : Copies read-only files.

/h : Copies files with hidden and system file attributes. By default, xcopy does not copy hidden or system files.

/a : Copies only source files that have their archive file attributes set. /a does not modify the archive file attribute of the source file. For information about how to set the archive file attribute by using attrib, see Related Topics.

/m : Copies source files that have their archive file attributes set. Unlike /a, /m turns off archive file attributes in the files that are specified in the source. For information about how to set the archive file attribute by using attrib, see Related Topics.

/n : Creates copies by using the NTFS short file or directory names. /n is required when you copy files or directories from an NTFS volume to a FAT volume or when the FAT file system naming convention (that is, 8.3 characters) is required on the destination file system. The destination file system can be FAT or NTFS.

/o : Copies file ownership and discretionary access control list (DACL) information.

/x : Copies file audit settings and system access control list (SACL) information (implies /o).

/exclude:filename1[+[filename2]][+[filename3]] : Specifies a list of files containing strings.

/y : Suppresses prompting to confirm that you want to overwrite an existing destination file.

/-y : Prompts to confirm that you want to overwrite an existing destination file.

/z : Copies over a network in restartable mode.

/? : Displays help at the command prompt.

This is the full option for robocopy

ROBOCOPY <source> <destination> [file…] [options]
<source> Source Directory (local or network path)
<destination> Destination Directory (local or network path) and
[file…] Specifies the file or files to be copied. You can use wildcard characters (* or ?), if you want. If the File parameter is not specified, *.* is used as the default value.

 

**Skip to the end of this document for more information on Robocopy Options and Switches .

Examples of Microsoft’s Robocopy Syntax

#1 Simple copy

To copy contents of C:\UserFolder to C:\FolderBackup:

Robocopy C:\UserFolder C:\FolderBackup

This is the simplest usage for Robocopy

#2 Copy all content including empty directory

To copy all contents including empty directories of SourceFolder to DestinationFolder:

Robocopy C:\SourceDir C:\DestDir /E

#3 List only

List only files larger than 32 MBytes(33553332 bytes) in size.

Robocopy.exe c:\sourceFolder d:\targetfolder /min:33553332 /l

Note: /l – will list files matching the criteria. if /l is omitted, files matching the criteria will be copied to the taget location

#4 Move files over 14 days old

Move files over 14 days old (note the MOVE option will fail if any files are open and locked).

ROBOCOPY C:\SourceFoldern D:\DestinationFolder /move /minage:14

Similarly you could use the below switches

  • /maxage: <N> Specifies the maximum file age (to exclude files older than N days or date).
  • /minage: <N>  Specifies the minimum file age (exclude files newer than N days or date).
  • /maxlad: <N> Specifies the maximum last access date (excludes files unused since N).
  • /minlad: <N> Specifies the minimum last access date (excludes files used since N) If N is less than 1900, N specifies the number of days. Otherwise, N specifies a date in the format YYYYMMDD

#5 Mirror a directory with subfolders incl. empty directories

/MIR is an option to ROBOCOPY where you mirror a directory tree with all the subfolders including the empty directories and you purge files and folders on the destination server that no longer exists in source.

ROBOCOPY \\sourceserver\share \\destinationserver\share /MIR

Or

ROBOCOPY source-drive:\DIR destination-drive:\DIR /MIR

#6 Mirror directories

The following command will mirror the directories using Robocopy:

Robocopy \\SourceServer\Share \\DestinationServer\Share /MIR /FFT /Z /XA:H /W:5

Explanation of the switches used:

  • /MIR specifies that Robocopy should mirror the source directory and the destination directory. Note that this will delete files at the destination if they were deleted at the source.
  • /FFT uses fat file timing instead of NTFS. This means the granularity is a bit less precise. For across-network share operations this seems to be much more reliable – just don’t rely on the file timings to be completely precise to the second.
  • /Z ensures Robocopy can resume the transfer of a large file in mid-file instead of restarting.
  • /XA:H makes Robocopy ignore hidden files, usually these will be system files that we’re not interested in.
  • /W:5 reduces the wait time between failures to 5 seconds instead of the 30 second default.

#7 Copy all changes

Use Robocopy to copy all changes to files in a directory called c:\data to a directory that contains the date, like data_20091124.  Create a batch file as follows.

@echo off
set day=%date:~0,2%
set month=%date:~3,2%
set year=%date:~6,4%
Robocopy "c:\data" "c:\backup\data\%day%-%month%-%year%\" /MAXAGE:1

#8 Mirror directory excl. deletion

To mirror the directory “C:\directory” to “\\server2\directory” excluding \\server2\directory\dir2″ from being deleted (since it isn’t present in C:\directory) use the following command:

Robocopy "C:\Folder" "\\Machine2\Folder" /MIR /XD  \\server2\ directory\dir2"

Robocopy can be setup as a simply Scheduled Task that runs daily, hourly, weekly etc. Note that Robocopy also contains a switch that will make Robocopy monitor the source for changes and invoke synchronization each time a configurable number of changes has been made. This may work in your scenario, but be aware that Robocopy will not just copy the changes, it will scan the complete directory structure just like a normal mirroring procedure. If there are a lot of files & directories, this may hamper performance.

#9 Copy permissions only

You have copied the contents from source to destination but now you made changes to the Security permissions at source. You wanted to copy only the permission changes and not data.

ROBOCOPY <Source> <Target> /E /Copy:S /IS /IT

Copy option have the following flags to use:

  • D Data
  • A Attributes
  • T Time stamps
  • S NTFS access control list (ACL)
  • O Owner information
  • U Auditing information

The default value for CopyFlags is DAT (data, attributes, and time stamps).

  • /IS – Includes the same files.
  • /IT – Includes “tweaked” files.

 

Leave a Reply

Your email address will not be published. Required fields are marked *