#!/usr/bin/env bash # by x1phosura # Synchronizes the destination to the source, preserving metadata (owner, # group, permissions, timestamps, etc...) and symbolic links. It also skips # replacing files based on a calculated checksum, which can save a lot of time # with backups! ssh is used for encryption, and the script displays the sync # progress with what SHOULD be human-readable numbers. # # Does NOT need to be run with sudo (and shouldn't). echo "Enter the username for the remote machine: " read ruser echo "Enter the hostname (if DNS) or IPv4 address for the remote machine: " read rhost # '-a' preserves attributes like permissions, owner/group, and more, '-v' is # verbose, '-h' is "human readable", and '-c' compares files to-be synced/ # transfered, if already existing, by checksum (rather than by filesize or # modified time). '-e' specifies the remote shell to use, which here is ssh # '--stats' and '--progress' simply show a lot of info about the file transfers # '-z' compresses data during the transfer options="-avhcz -e ssh --stats --progress" echo "Delete files at destination not present in source directories? (y/n):" read del_remote if [ "$del_remote" = "y" ]; then # TEST THIS OPTION OUT BEFORE USE!! # '--delete' deletes files at the destination that are NOT present from the # source. USE CAREFULLY!! '--force' modifies '--delete' to handle something # do to with non-empty directories being deleted or overridden, so I'm # guessing I want it echo "Extraneous files found in destination will be deleted." options="$options --delete --force" elif [ "$del_remote" = "n" ]; then echo "Extraneous files found in destination will be kept." else echo "Error: expected 'y' or 'n' character as input. Aborting for safety..." exit 1 fi # Note: as they currently stand, DO NOT add trailing slashes to these # filenames!! Why? Here is the best explanation I've seen as to how trailing # slashes work in rsync: # Without a slash on the source directory means copy both the source # directory, and the contents (recursively if specified) to the destination # directory while adding a trailing slash means only copy the contents of # the source directory, recursively if specified, to the destination. # TODO: auto-generate paths (or read from textfile) instead of hardcoding filelist="$HOME/73h4x \ $HOME/Documents \ $HOME/Downloads \ $HOME/OSes \ $HOME/Library \ $HOME/Subgenius \ $HOME/temp" # Command structure: # rsync $options src/dir1 src/dir2... "$ruser"@"$rhost":dest/dir/ # copy from here TO a remote destination rsync $options $filelist "$ruser"@"$rhost":~/ # copy from a remote destination TO here (TODO)