-
Notifications
You must be signed in to change notification settings - Fork 3
/
sync_loc_rem_dir
executable file
·96 lines (86 loc) · 2.53 KB
/
sync_loc_rem_dir
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
#! /bin/bash
#
# A bash script that syncs a local directory's contents to a remote target.
# Colors for later:
red='\x1b[0;31m'
grn='\x1b[0;32m'
ncl='\x1b[0m'
#############################################################################
# A couple of handler functions
#############################################################################
function usage {
echo -e "\nUSAGE:\n\tsync_loc_rem_dir <loc_dir> <rem_dir> <user> <host> <quiet>\n";
echo -e "NOTE: Do NOT add a trailing '/' to paths.";
exit;
}
function clean_rsync_verbose {
# Simplify the rsync varbose output.
# NOTE: if on linux, change \x1b in color defs red,grn,ncl to \e
cat $1 | while read line; do
if [[ $line == delet* ]]; then
echo -e "${red}$line${ncl}"
elif [[ $line == building* ]] || [[ $line == "" ]]; then
:
elif [[ $line == "sent "* ]] || [[ $line == "total size is"* ]]; then
echo -e "\n"$line
elif [[ $line == "cannot "* ]]; then
echo -e "${red}$line${ncl}"
else
echo -e "${grn}creating or updating" $line${ncl}
fi
done
}
#############################################################################
# The script itself...
#############################################################################
# First check if help is required:
if [ $1 == "--help" ] || [ $# -ne 5 ]; then
usage;
fi
# Next, do a dry run of the rsync:
mkdir $HOME/sync_temp;
rsync \
-nrvc \
--delete \
--exclude "*.err" \
--exclude "*.out" \
--exclude ".*" \
--exclude ".syncr" \
--exclude "*.pyc" \
--exclude "outputs" \
$1 \
$3@$4:$2 > \
$HOME/sync_temp/message.txt;
# Print the changes to be made for review and approval:
echo -e "The following changes will be made:\n";
clean_rsync_verbose $HOME/sync_temp/message.txt;
echo -e "OK? [y/n]";
read ok;
# if OK, then commence with sync, else abort.
if [ $ok == "y" ]; then
rsync \
-rvc \
--delete \
--exclude "*.err" \
--exclude "*.out" \
--exclude ".*" \
--exclude ".syncrconf*" \
--exclude "*.pyc" \
--exclude "outputs" \
--progress \
$1 \
$3@$4:$2 > \
$HOME/sync_temp/message.txt;
else
echo -e "${red}Sync aborted.${ncl}";
rm -r $HOME/sync_temp;
exit;
fi
# Wrap-up
rc=$?
if [[ $rc == 0 ]]; then
$5 && echo -e "${grn}\nDirectory\n\t$1\nsync'd with remote.${ncl}";
else
echo -e "${red}\nSync of directory\n\t$1\nwith remote failed.${ncl}";
fi
rm -r $HOME/sync_temp;