ThisCalculator

Everyday

Backup Computer Files from Command Prompt

Updated · Standard formula, shown below

Backup Computer Files from Command Prompt

Yes, you can back up computer files from the command prompt using built-in tools like robocopy (Windows) and rsync (macOS/Linux). These commands copy files and folders with precise control, making them ideal for scheduled or scripted backups. This article explains how to use them, what options matter, and how to avoid common mistakes.

Why Use the Command Prompt for Backups

Graphical backup tools are convenient, but the command prompt offers several advantages:

  • Automation – Commands can be placed in scripts and run on a schedule via Task Scheduler (Windows) or cron (macOS/Linux).
  • Precision – You can include or exclude specific file types, folders, or age ranges.
  • Speed – Tools like robocopy are multi-threaded and often faster than copying in File Explorer.
  • Logging – You can generate detailed logs of what was copied, skipped, or failed.

For most users, the built-in tools are sufficient and free. You don’t need third-party software for basic backups.

Windows: Using Robocopy

robocopy (Robust File Copy) is included with Windows Vista and later. It copies files, directories, and trees, and it can resume interrupted copies. The basic syntax is:

robocopy <source> <destination> [options]

Example – copy all files from C:\Users\YourName\Documents to an external drive E:\Backup\Documents:

robocopy C:\Users\YourName\Documents E:\Backup\Documents /E /R:3 /W:10
  • /E – copies all subdirectories, including empty ones.
  • /R:3 – retries 3 times on failure (default is 1 million, which can hang).
  • /W:10 – waits 10 seconds between retries.

Other useful options:

  • /MIR – mirrors the source to destination, deleting files in destination that no longer exist in source. Use with caution.
  • /XO – excludes older files (only copies newer ones).
  • /LOG:file.txt – writes a log file.
  • /MT:16 – uses 16 threads for faster copying.

To see all options, type robocopy /?

macOS and Linux: Using Rsync

rsync is preinstalled on macOS and most Linux distributions. It copies files locally or over a network (via SSH). Basic syntax:

rsync [options] <source> <destination>

Example – back up /home/user/Documents to an external drive mounted at /Volumes/Backup:

rsync -avh /home/user/Documents/ /Volumes/Backup/Documents/
  • -a – archive mode, preserves permissions, timestamps, and symlinks.
  • -v – verbose output.
  • -h – human-readable sizes.
  • Trailing slashes matter: /source/ copies the contents, /source copies the folder itself.

To delete files in destination that are not in source, add --delete. To exclude files, use --exclude='*.tmp'. For network backups, use rsync -avh /local/path user@remote:/remote/path.

Choosing the Right Tool and Options

For Windows, robocopy is the standard. For macOS/Linux, rsync is standard. But you may need to decide which options to use based on your backup strategy:

  • Full backup – copy everything each time. Simple but slow and uses more space.
  • Incremental backup – copy only files that changed since last backup. robocopy uses /XO (exclude older) or /MAXAGE:n to limit by age. rsync compares file size and modification time by default.
  • Mirror backup – make destination identical to source. Use /MIR (robocopy) or --delete (rsync). This is useful for syncing, but dangerous if you accidentally delete files in source.

Always test your command on a small folder first. Verify that the destination has enough space.

Automating Backups with Scripts and Schedulers

To run backups regularly, create a script file and schedule it.

Windows (Task Scheduler):

  1. Create a text file named backup.bat with your robocopy command.
  2. Open Task Scheduler, click “Create Basic Task�.
  3. Set the trigger (e.g., daily at 2 AM).
  4. Set the action to “Start a program� and browse to your .bat file.

macOS/Linux (cron):

  1. Create a script file backup.sh with your rsync command, and make it executable with chmod +x backup.sh.
  2. Edit your crontab with crontab -e.
  3. Add a line like 0 2 * * * /path/to/backup.sh to run daily at 2 AM.

Make sure the external drive is connected when the backup runs. Some users use a script to check if the drive is mounted before proceeding.

Common Mistakes and How to Avoid Them

  • Forgetting trailing slashes in rsync – /source vs /source/ changes what gets copied. Always test.
  • Using /MIR or –delete without a backup – If you accidentally delete files in source, the mirror will delete them in destination too. Consider using a versioned backup or a separate archive.
  • Not checking exit codes – Both tools return an exit code. 0 means success; non-zero means a problem. In scripts, you can check %ERRORLEVEL% (Windows) or $? (Unix).
  • Copying to the same drive – A backup on the same disk is useless if the disk fails. Use an external drive, network share, or cloud-synced folder.
  • Ignoring open files – Files in use may be skipped or copied incorrectly. Close applications or use Volume Shadow Copy (Windows) if needed.

Frequently Asked Questions

Can I back up files while they are in use?

On Windows, robocopy may skip files that are locked by another program. You can use the /B option to run in backup mode, which requires administrative privileges and uses the Volume Shadow Copy service. On macOS/Linux, rsync will copy a file even if it changes during the copy, but the result may be inconsistent. For a consistent backup of open files, consider using a snapshot tool or closing the applications.

What is the difference between robocopy and xcopy?

xcopy is an older command that copies files and directories but lacks many of robocopy‘s features, such as multi-threading, restartable copies, and robust error handling. For modern Windows, robocopy is recommended.

How do I back up to a network drive from the command prompt?

For Windows, map the network drive (e.g., net use Z: \\server\share) then use robocopy C:\source Z:\backup. For macOS/Linux, use rsync with SSH: rsync -avh /local/path user@server:/remote/path. You may need to authenticate each time; consider setting up SSH keys for automation.

Does robocopy or rsync compress files?

No, they copy files as-is. To compress, you would need to use a compression tool like zip or tar. However, rsync can compress data during transfer with the -z option, which speeds up network transfers but does not store compressed files.

How do I restore files from a backup made with robocopy or rsync?

Simply run the command in reverse: robocopy E:\Backup\Documents C:\Users\YourName\Documents /E or rsync -avh /Volumes/Backup/Documents/ /home/user/Documents/. This copies the files back. Be careful not to overwrite newer files unless you intend to.

Can I use these commands to back up to cloud storage?

Not directly. Cloud providers have their own sync clients. However, you can back up to a local folder that is synced to the cloud (e.g., Dropbox, OneDrive, Google Drive). Alternatively, use command-line tools like rclone (third-party) to copy to cloud storage, but that is not built-in.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.