-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdosync
executable file
·105 lines (92 loc) · 2.58 KB
/
dosync
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
#!/bin/bash
function print_usage() {
echo "Usage: dosync ACTION [REPO-NAME]"
echo
echo "Actions:"
echo " to: sync 'remote' folder to the server"
echo " from: sync from the server to the 'remote' folder"
echo
echo "Use REPO-NAME if more than one repo is specified in the '.sync.config' file"
}
function print_config_file_format() {
echo "format of .sync.conf file should be as follows:"
echo " [repo_name1]=[ssh_url]"
echo " [repo_name2]=[ssh_url]"
echo " ..."
}
function print_error() {
printf "\033[0;31m"
echo "[!!] ERROR: $1"
printf "\033[0m"
}
function reset_mode() {
# Reset the mode of the files on server after rsync. On Windows, the files mode will be changed,
# so need to reset the mode on server after syncing the files
IFS=':' read -ra host <<< "$1"
ssh ${host[0]} "cd ${host[1]} && git diff -p -R | grep -E \"^(diff|(old|new) mode)\" --color=never | git apply"
}
if [ -z $1 ]
then
print_error "missing ACTION"
print_usage
exit 1
fi
sync_mode="$1"
if [ "$sync_mode" != "to" ] && [ "$sync_mode" != "from" ]
then
print_error "Unknown ACTION: $sync_mode"
print_usage
exit 1
fi
num_repos=0
if [ -f ".sync.conf" ]
then
num_repos=$(wc -l .sync.conf | cut -d ' ' -f1)
else
print_error "Missing .sync.conf file"
print_config_file_format
exit 1
fi
if [ $num_repos -lt 1 ]
then
print_error "Invalid .sync.conf file"
print_config_file_format
exit 1
fi
selected_repo=$2
if [ $num_repos -gt 1 ]
then
if [ -z $selected_repo ]
then
echo "There is more than one repo in the config file, specify which repo to use"
print_usage
exit 1
fi
else
# set the repo name by default
selected_repo=$(cat .sync.conf | cut -d '=' -f1)
fi
ssh_url=$(cat .sync.conf | grep "$selected_repo"= | cut -d '=' -f2)
if [ -z $ssh_url ]
then
print_error "could not find url for $selected_repo make sure the config file is properly formatted"
print_config_file_format
exit 1
fi
# At this point we have evrything we need, just run the command!
if [ $sync_mode == "from" ]
then
echo "Sync from $ssh_url to remote dir..."
# -p to ensure that the remote directory only gets created if it already does not exist
mkdir -p remote
rsync --progress -a --exclude '.git' $ssh_url/ remote
else
echo "Sync content from remote dir to $ssh_url ..."
if [ ! -d remote ]
then
print_error "There is no remote folder to sync!"
exit 1
fi
cd remote
rsync --progress -a --no-perms --exclude '.git' --exclude '*.swp' . $ssh_url
fi