Skip to content

Instantly share code, notes, and snippets.

@tyzhnenko
Created June 8, 2021 15:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tyzhnenko/d17b3cdc7ec6edf4164d788b552c1513 to your computer and use it in GitHub Desktop.
Save tyzhnenko/d17b3cdc7ec6edf4164d788b552c1513 to your computer and use it in GitHub Desktop.
#!/bin/bash
# The script can copy part of TimeMachine folders from one to another place.
# In the way like TM does. Next folder contains just difference.
# Unfortunatry the target folder will take more space. Becase of rsync doesn't support hard links for folders.
BYPASS=/System/Library/Extensions/TMSafetyNet.kext/Contents/Helpers/bypass
RSYNC=/usr/local/bin/rsync # Path to rsync. By default in MacOS it's version 2.
RSYNC2_ARGS="--extended-attributes"
RSYNC3_ARGS="--xattrs --acls --crtimes --one-file-system --numeric-ids"
RSYNC_ARGS="--archive --progress -P --human-readable --update --stats --verbose --inplace"
SRC="/Volumes/SrouceTimeMachine" # Source TimeMachine disk
DST="/Volumes/TargetTimeMachine" # Target TimeMachine disk
BDIR="Backups.backupdb"
DATES_MASKS="2021-*" # Folder mask on to copy
MACHINE_NAME=$(ls -d $SRC/$BDIR/*)
MACHINE_NAME=$(basename "$MACHINE_NAME")
# The script works only with one device time machine archive
SRC_DATES=""
for mask in $DATES_MASKS; do
for folder in $SRC/$BDIR/"$MACHINE_NAME"/$mask; do
echo "Add folder $folder to queue"
SRC_DATES="$SRC_DATES $(basename "$folder")"
done
done
if $RSYNC --version | grep -q 'rsync version 3'; then
echo "You have rsync 3"
RUN_RSYNC="$RSYNC $RSYNC3_ARGS $RSYNC_ARGS"
else
echo "You have rsync 2"
RUN_RSYNC="$RSYNC $RSYNC2_ARGS $RSYNC_ARGS"
fi
echo "Add bypass"
RUN_RSYNC="$BYPASS $RUN_RSYNC"
echo "Sync $BDIR"
echo "You can find progess in log file sync_log_$BDIR"
echo $RUN_RSYNC --exclude \""$BDIR/"$MACHINE_NAME"/*"\" "$SRC/$BDIR" "$DST" 2\>\&1 \> sync_log_$BDIR
$RUN_RSYNC --exclude "$BDIR/$MACHINE_NAME/*" "$SRC/$BDIR" "$DST" 2>&1 > sync_log_$BDIR
first=true
for curr in $SRC_DATES; do
if [ "$first" = true ]; then
echo "Sync $curr folder"
echo "You can find progess in log file sync_log_$curr"
echo $RUN_RSYNC "$SRC/$BDIR/$MACHINE_NAME/$curr" "$DST/$BDIR/$MACHINE_NAME/" 2\>\&1 \> sync_log_$curr
$RUN_RSYNC "$SRC/$BDIR/$MACHINE_NAME/$curr" "$DST/$BDIR/$MACHINE_NAME/" 2>&1 > sync_log_$curr
prev=$curr
first=false
continue
fi
echo "Sync $curr folder"
echo "You can find progess in log file sync_log_$curr"
echo $RUN_RSYNC --link-dest=../$prev/ "$SRC/$BDIR/$MACHINE_NAME/$curr/" "$DST/$BDIR/$MACHINE_NAME/$curr/" 2\>\&1 \> sync_log_$curr
$RUN_RSYNC --link-dest=../$prev/ "$SRC/$BDIR/$MACHINE_NAME/$curr/" "$DST/$BDIR/$MACHINE_NAME/$curr/" 2>&1 > sync_log_$curr
prev=$curr
done
@sokai
Copy link

sokai commented Mar 22, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment