Shortcuts
|
How to Backup using Batch Files under Windows 102018-10-01 (updated: 2020-10-06) by PhilipIt is sometimes useful, or even necessary to simply copy common directories and files to another/external drive, or a network attached storage (NAS). This article provides a comparatively simple solution using a batch file to backup folders. Even though it can copy only changed/newer files to save time, it is still not intended as a replacement for a full-featured backup software with versioning, incremental backups, etc. This article works well for backing up common files/folders under Windows 10 using robocopy. It is an updated, expanded and improved version of an older article we wrote for Windows XP using batch files and xcopy. The batch file below is designed to work with the newer Windows directory structure, and robocopy seems to be more robust than xcopy in Windows 10. Batch files have a comparatively easy syntax. You can simply copy the text below, paste into Notepad and modify it to your needs. Note that the batch file should have either .bat or .cmd extension, rather than .txt so that it would be executed when you double-click on it. Here is a working sample backup script, you should modify at least the destination drive to your needs. This batch file includes many commonly needed variables, such as dates and timestamps. Explanation for each section is included below the script. For a simpler variant of the script, you can skip down to the "TL;DR;" section. @echo off Explanation of the scriptIn the above file, all lines that begin with ":: " are comments. Below we will examine most of the sections from the above batch file, so that they are easier to understand and modify. You can simply omit sections that you do not need to use, for example the "DATE Variables" and the "Backup Outlook" sections may not be needed by everyone. Here are the file sections explained, from the beginning ot the batch file: set drive=G:\backup This defines the backup destination drive/folder. The "set drive=..." defines the destination hard drive, it can also be a network location, just copy the name from Windows Explorer, for example "drive=\\router-nas", or "drive=Z:\backups". Variables in general are defined using the "set variablename=..." command. Those same variables can leter be referenced by the same vairable name, surrounded by percentage symbols, as in "%variablename%", or %drive%, etc. set backupcmd=robocopy /e /xo /R:5 /XJD This sets the backup command, we are using the robocopy command that is included with morern Windows installations. There are some useful command-line switches that can be added to the robocopy command as follows: :: ROBOCOPY switches set excludefiles=/xf *.vsdelta *.tmp Thumbs.db These two variables define excluded files/folders that you may not want to copy. As an example, the above batch file excludes *.tmp temporary files, Thumbs.db files, and the Google Chrome temporary/user files to improve speed and reduce unnecessary clutter at the destination. :: -+-+-+- DATE Variables (optional) -+-+-+- This section is optional, it can be commented out if you do not plan to use dates in your backup folder names. This section defines a number of variables that you can use, if you want. For example, you may want to use the %datestamp% variable as a directory name to create different backups based on date. You can only use portions of the date, as in "%YY%-%MM%" to get a two-digit year and two digit month. In this batch file, only the %YYYY% (four digit year) is used in the destination folder name below. :: Using the Windows Environment Variable %UserName% to get the current user This section defines the %user% variable to the currently logged in Windows user, and the %folder% variable that we will use as a destination for our backups. Note the %folder% variable will contain both our Windows username and the current year from previously defined variables. :: -+-+-+- start backup below -+-+-+- This section simply uses our backup command defined above, and copies the currently logged in Windows user's Desktop, Documents, and Pictures folders to the destination drive/folder. Only modified files will be copied on subsequent runs of the batch file. :: -+-+-+- Backup Outlook files (optional) -+-+-+- This last section is completely optional, if you use Outlook it backs up your email files. It stops Outlook in order to copy the files. If you do not use MS Outlook, you can comment it out, or delete this section completely. echo ### Backup Complete! ### This simply pauses at the end of the batch file, so that you can review what has been copied. TL;DR;Too Long? Didn't Read? Want something simpler? Here is a very simple working batch file that also backs up the Desktop, Documents and Pictures of the current Windows user (without most variables, outlook files, or excluded filetypes/folders): @echo off The above simply backs up three sample folders, "C:\Users\{username}\Destkop", "Documents", and "Pictures" to a backup destination, in this example their corresponding folders under G:\backup\... You only have to modify the destination drive letter to your external hard drive to make this script work. The global Windows %UserName% is the only variable in this file, corresponding to the name of the currently logged in Windows user. Alternatively, you can also simply copy/paste the source directory paths from Windows Explorer. Notes:Directory paths need to be enclosed in parenthesis if the directory structure contains any spaces. Useful global Windows Environment Variables that can be included in batch files: For an older version of this article that works well with Windows XP using xcopy instead of robocopy, see this.
User Reviews/Comments:
rate:
avg:
|