-
-
Save pdf/9302412 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| set -e | |
| # Usage: | |
| # rsync_parallel.sh [--parallel=N] [rsync args...] | |
| # | |
| # Options: | |
| # --parallel=N Use N parallel processes for transfer. Defaults to 10. | |
| # | |
| # Notes: | |
| # * Requires GNU Parallel | |
| # * Use with ssh-keys. Lots of password prompts will get very annoying. | |
| # * Does an itemize-changes first, then chunks the resulting file list and launches N parallel | |
| # rsyncs to transfer a chunk each. | |
| # * be a little careful with the options you pass through to rsync. Normal ones will work, you | |
| # might want to test weird options upfront. | |
| # | |
| if [[ "$1" == --parallel=* ]]; then | |
| PARALLEL="${1##*=}" | |
| shift | |
| else | |
| PARALLEL=10 | |
| fi | |
| echo "Using up to $PARALLEL processes for transfer..." | |
| TMPDIR=$(mktemp -d) | |
| trap "rm -rf $TMPDIR" EXIT | |
| echo "Figuring out file list..." | |
| # sorted by size (descending) | |
| rsync $@ --out-format="%l %n" --no-v --dry-run | sort -n -r > $TMPDIR/files.all | |
| # check for nothing-to-do | |
| TOTAL_FILES=$(cat $TMPDIR/files.all | wc -l) | |
| if [ "$TOTAL_FILES" -eq "0" ]; then | |
| echo "Nothing to transfer :)" | |
| exit 0 | |
| fi | |
| echo "Calculating chunks..." | |
| # declare chunk-size array | |
| for ((I = 0 ; I < PARALLEL ; I++ )); do | |
| CHUNKS["$I"]=0 | |
| done | |
| # add each file to the emptiest chunk, so they're as balanced by size as possible | |
| # | |
| # Nah, fuck that - it's horribly slow with large file lists. If we're transferring | |
| # many small files, we may spend more time comparing their size than we would | |
| # transferring them. | |
| # | |
| I=0 | |
| while read FSIZE FPATH; do | |
| [ "$I" -eq "$PARALLEL" ] && I=0 | |
| echo $FPATH >> $TMPDIR/chunk.$I | |
| I=$((I+1)) | |
| done < $TMPDIR/files.all | |
| find "$TMPDIR" -type f -name "chunk.*" -printf "\n*** %p ***\n" -exec cat {} \; | |
| echo "Starting transfers..." | |
| find "$TMPDIR" -type f -name "chunk.*" | parallel -j $PARALLEL -t --verbose --progress rsync --files-from={} $@ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment