-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsync
executable file
·144 lines (130 loc) · 3.59 KB
/
csync
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/bash
# Script for syncing my project folders with the ones on the remote server we work on.
# Use with:
# clustersync push/pull to copy files
# clustersync push/pull sync to synchronize (be careful not to lose anything)
# clustersync push/pull check to check differences
# This is heavily inspired by Prof. Dr. Leandro Martinez's script (https://github.com/lmiq/tips/blob/master/GoogleDrive/gdrive)
# You can also create a .remoteignore file to exclude any file from syncing.
usage() {
echo "Usage: $0 [remote] [action] [sync/check] [options]"
echo
echo "Arguments:"
echo " remote The remote server to sync with: newton, jupiter, or pipeline."
echo " action The action to perform: push or pull."
echo " sync/check (optional) Synchronize the files or check for differences."
echo
echo "Options:"
echo " -n, --no-ignore Do not use .remoteignore files."
echo " -h, --help Show this help message."
exit 1
}
use_ignore=true
# Parse options and arguments
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help)
usage
;;
-n|--no-ignore)
use_ignore=false
shift
;;
newton|jupiter|pipeline)
remote="$1"
case "$remote" in
newton)
projects=$NEWTON_PROJECTS
;;
jupiter)
projects=$JUPITER_PROJECTS
;;
pipeline)
projects=$LOCAL_PROJECTS
;;
*)
echo "ERROR: Invalid remote '$1'. Must be 'newton', 'jupiter', or 'pipeline'."
usage
;;
esac
shift
;;
push|pull)
action="$1"
shift
;;
sync|check)
sync_check="$1"
shift
;;
*)
echo "ERROR: Unknown argument '$1'."
usage
;;
esac
done
# Ensure required arguments are provided
if [[ -z "$remote" || -z "$action" ]]; then
echo "ERROR: Missing required arguments."
usage
fi
# Set up directories
current_dir=$(pwd)'/'
if [[ "$current_dir" == "$LOCAL_PROJECTS"* ]]; then
len_local=${#LOCAL_PROJECTS}
len_current=${#current_dir}
remote_dir=${current_dir:$len_local:$len_current}
target="$remote:$projects$remote_dir"
current_local="$current_dir"
current_remote="$target"
else
echo "Current dir is NOT a $remote subfolder. Quitting."
exit 1
fi
# Function to find .remoteignore file up to the home directory
find_remoteignore() {
local dir="$current_dir"
while [[ "$dir" != "$HOME" && "$dir" != "/" ]]; do
if [[ -f "$dir/.remoteignore" ]]; then
echo "$dir/.remoteignore"
return
fi
dir=$(dirname "$dir")
done
return 1
}
# Check for .remoteignore file
ignore_flag=""
if [[ "$use_ignore" == true ]]; then
ignore_files=$(find_remoteignore)
if [[ $ignore_files ]]; then
echo ".remoteignore found at $ignore_files, ignoring the files listed"
ignore_flag="--exclude-from $ignore_files"
else
echo "No .remoteignore file found."
fi
else
echo "Ignoring .remoteignore file is deactivated."
fi
# Set up rsync source/destination
if [[ "$action" == "push" ]]; then
source="$current_local"
destination="$current_remote"
elif [[ "$action" == "pull" ]]; then
source="$current_remote"
destination="$current_local"
else
echo "ERROR: Second argument must be 'push' or 'pull'"
exit 1
fi
# Handle sync/check options
last_flag=""
if [[ "$sync_check" == "sync" ]]; then
last_flag="--delete"
elif [[ "$sync_check" == "check" ]]; then
last_flag="--dry-run"
fi
# Ensure target directory exists on the remote server
ssh "$remote" "mkdir -p $projects$remote_dir"
# Run rsync command
rsync -rtvuc $ignore_flag $last_flag "$source" "$destination"