Publishing static content of a web site

Publish web site composed of static files and directories in directory ~/subdir/ on local machine to web site directory ~/dir/subdir on remote server

$ rsync -thrivpbl ~/mysite/ foouser@host.domain.com:~/dir/msite

or

$ rsync -zhavib ~/mysite/ foouser@host.domain.com:~/dir/mysite

  • -a equals -rlptgoD
    • -r recursive
    • -l preserve links
    • -p preserve permissions
    • -t preserve time stamp
    • If copying on the same computer:
      • -g preserve group
      • -o preserve owner
      • -D or --devices preserve device files
  • -h report human readable numbers
  • -i report itemized list of the changes to each file, including attribute changes.
  • -v verbose
  • -b backup
  • -z compress file data during transfer

Verbose output (-v)

Some common indicators:

  • >f+++++++++: recieving (>) a regular file (f) which is newly created (+).
  • <f.st......: sending (<) a regular file (f) whose size (s) and time stamp (t) are different.
  • >f.st......: recieving (>) a regular file (f) whose size (s) and time stamp (t) are different.
  • .d..t......: not updating (initial .) a directory (d) whose time stamp (t) is different.

References: http://stackoverflow.com/questions/4493525/rsync-what-means-the-f-on-rsync-logs

Safely copying large file

$ sudo rsync -vahzi --progress /Volumes/large_file.mp4 /Volumes/new_file.mp4

Safely copying a large directory

$ sudo rsync -vahzi --progress /Volumes/large_dir/ /Volumes/new_dir

Remarks:

  • --progress: Display progress. Use only if individual files are large and slow to transfer.

Safely merging two directories

To safely merge two directories using rsync without

  1. overwriting newer files in the destination or,
  2. deleting files unique to the destination,

use the following rsync command:

rsync -auhv --progress /path/to/source_folder/ /path/to/destination_folder/

where the options are

-a (or --archive): This is a combination of several options that ensure files are copied in "archive mode," preserving permissions, ownership, timestamps, symbolic links, and other attributes. It's equivalent to -rlptgoD. • -u (or --update): This crucial option ensures that rsync only copies files from the source to the destination if the source file is newer than the destination file, or if the destination file does not exist. This prevents overwriting newer files in the destination with older versions from the source. • -h (or --human-readable): Displays numbers in a human-readable format (e.g., file sizes in KB, MB, GB). • -v (or --verbose): Enables verbose output, showing which files are being transferred and providing progress updates. • --progress: Shows the progress of each file transfer.

Remarks:

  • Purpose: A typical reason why it might be attempted to merge the two directories is that they are two different versions of the same original data that diverged for some reason. For example a directory of files that was copied to a different location then separately evolved from the original version.

Trailing slashes: Ensure that both source and destination paths have a trailing slash (/). This tells rsync to copy the contents of the source directory into the destination, rather than copying the source directory itself as a subdirectory within the destination.

Dry run: Before executing the command, consider performing a "dry run" using the -n (or --dry-run) option to see what rsync would do without actually making any changes:

rsync -auhv --progress --dry-run /path/to/source_folder/ /path/to/destination_folder/

Backup: While rsync -u or rsync --dry-run is designed to prevent data loss by not overwriting newer files, it is always recommended to create a backup of your destination folder before performing a merge operation, especially when dealing with critical data.

No deletion: This command will not delete any files in the destination that are not present in the source. If you require synchronization that includes deleting files from the destination that are absent in the source, you would need to add the --delete option, but this changes the merge behavior to a mirroring operation. For safely merging, --delete should generally be avoided.

rsync -auhv --progress --delete --dry-run /path/to/source_folder/ /path/to/destination_folder/

Installing up-to-date rsync via Homebrew on macOS

The version of rsync coming with macOS operating system is an older version (2.* as of OS X 10.10.2). To install the more up-to-date version,

brew doctor
brew upgrade
brew tap homebrew/dupes
brew install rsync

Make sure the path Homebrew installs softwares to, /usr/local/bin appears before other paths in /private/etc/paths so it's searched first. Restart the terminal.

blog comments powered by Disqus