Created
December 16, 2024 06:15
-
-
Save unphased/468f79a6356698f3d7093dd5627b14a8 to your computer and use it in GitHub Desktop.
replicate
This file contains 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 | |
LOGFILE="$HOME/replicate.log" | |
exec >> "$LOGFILE" | |
exec 2>&1 | |
echo "Replicate script started at $(date) for $1 -> $2" | |
# Take a pair of args for $1 = location to backup and $2 = destination | |
# TODO Do a check via ssh call if it is a remote path. | |
# # Check if the destination exists | |
# if [ ! -d "$2" ]; then | |
# echo "Destination does not exist" | |
# exit 1 | |
# fi | |
# | |
# # Check if the destination is writable | |
# if [ ! -w "$2" ]; then | |
# echo "Destination is not writable" | |
# exit 1 | |
# fi | |
# Check if the source exists | |
if [ ! -d "$1" ]; then | |
echo "Source does not exist" | |
exit 1 | |
fi | |
# Path to the lockfile. Name it after the source and dest for disambiguation (hey maybe you have two of these running | |
# to the same destination) | |
TMPDIR="/var/lock" | |
[ -d "$TMPDIR" ] || TMPDIR="/tmp" | |
LOCKFILE="$TMPDIR/replicate-${1//[\/:@]/-}__${2//[\/:@]/-}.lock" | |
# Use -n to quit if unable to acquire lock. Don't need to use the fancy fd method since the critical command is | |
# singular. | |
FLOCK=/usr/bin/flock | |
[ -x $FLOCK ] || FLOCK='/opt/homebrew/bin/flock' | |
$FLOCK -n "$LOCKFILE" rsync -avi --stats "$1" "$2" | |
echo "Replicate script finished at $(date) for $1 -> $2" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment