-
Notifications
You must be signed in to change notification settings - Fork 0
/
dev-sync.sh
executable file
·86 lines (71 loc) · 2.31 KB
/
dev-sync.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/bin/bash
# Function to show usage
usage() {
echo "Usage: $0 [--sync] [--delete] <source_directory> <destination_directory>"
echo " --sync Perform actual synchronization (default is dry run)"
echo " --delete Delete files in the destination that are not in the source"
exit 1
}
# Check if the correct number of arguments is provided
if [[ "$#" -lt 2 || "$#" -gt 4 ]]; then
usage
fi
# Check if rsync is installed (should be by default on macOS)
if ! command -v rsync &> /dev/null; then
echo "ERROR: rsync is not installed. Please install it and try again."
exit 1
fi
# Initialize variables
DRY_RUN=true
DELETE_IN_DEST=false
SOURCE_DIR=""
DEST_DIR=""
# Parse command line options
while [[ "$1" == --* ]]; do
case "$1" in
--sync) DRY_RUN=false ;;
--delete) DELETE_IN_DEST=true ;;
*) usage ;;
esac
shift
done
# Assign source and destination directories
SOURCE_DIR="$1"
DEST_DIR="$2"
# Ensure both source and destination directories are provided and exist
if [[ -z "$SOURCE_DIR" || -z "$DEST_DIR" ]]; then
echo "ERROR: Both source and destination directories must be provided."
usage
fi
if [[ ! -d "$SOURCE_DIR" ]]; then
echo "ERROR: Source directory '$SOURCE_DIR' does not exist."
exit 1
fi
if [[ ! -d "$DEST_DIR" ]]; then
echo "ERROR: Destination directory '$DEST_DIR' does not exist."
exit 1
fi
# Set rsync options
RSYNC_OPTIONS="-av --checksum --exclude=.DS_Store"
# Add the delete flag if specified and not in dry run mode
if [[ "$DELETE_IN_DEST" == true && "$DRY_RUN" == false ]]; then
RSYNC_OPTIONS="$RSYNC_OPTIONS --delete"
echo "Files not present in source will be deleted from destination."
fi
# Add the dry-run flag by default unless --sync is provided
if [[ "$DRY_RUN" == true ]]; then
RSYNC_OPTIONS="$RSYNC_OPTIONS --dry-run"
echo "Performing a dry run by default. No changes will be made."
else
echo "Sync mode enabled. Performing actual synchronization."
fi
# Perform the sync using rsync
echo "Synchronizing $SOURCE_DIR to $DEST_DIR..."
echo "Running command: rsync $RSYNC_OPTIONS \"$SOURCE_DIR/\" \"$DEST_DIR/\""
rsync $RSYNC_OPTIONS "$SOURCE_DIR/" "$DEST_DIR/"
# Display message on completion
if [[ "$DRY_RUN" == true ]]; then
echo "Dry run complete. No changes were made."
else
echo "Synchronization complete."
fi