-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup
executable file
·82 lines (70 loc) · 1.72 KB
/
backup
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
#!/bin/bash
usage() {
script="$(basename "$0")"
cat <<-DOC
Usage: $script [Options] {address}
rsync ip:dir ip:dir
rsync -avh file.zip erik@108.61.119.116:~/Docs
0) Both sender/receiver must have the same directory layout. No spaces allowed in either path.
1) Navigate to the directory you want to backup
a) $script -r rairden.dev # receive
b) $script -s -n rairden.dev # send and -n is dry-run
Options:
-s, --send send
-r, --receive receive
-n, --dry-run dry-run
-h, --help show this help message
DOC
}
printSummary() {
createdFiles=$(echo "$1" | pcregrep -o1 "Number of created files:\s(.*)")
transferSize=$(echo "$1" | pcregrep -o1 "Total transferred file size:\s(.*)")
echo " New files: $createdFiles"
echo "Transfer total: $transferSize"
}
if [ $# -lt 1 ]; then
echo "Try '$script --help' for more information."
exit 1
fi
send=false
receive=false
dryrun=false
dry=
copyDir=$(pwd)
parentDir=$(dirname "$copyDir")
while [ $# -gt 0 ]; do
case $1 in
-s | --send)
send=true
;;
-r | --receive)
receive=true
;;
-n | --dry-run)
dryrun=true
dry='-n'
;;
-h | --help)
usage
exit 1
;;
*)
host=$1
esac
shift
done
if $dryrun; then
echo -e " (DRY RUN)"
fi
if $send; then
output=$(rsync -avh --del $dry --stats "$copyDir" "$host:$parentDir")
echo -e " [CMD] rsync -avh $dry --stats $copyDir $host:$copyDir"
printSummary "$output"
elif $receive; then
output=$(rsync -avh $dry --stats "$host:$copyDir" "$parentDir")
echo -e " [CMD] rsync -avh $dry --stats $host:$copyDir $parentDir"
printSummary "$output"
else
echo "Must give -s or -r argument to send or receieve. Try -h for more info."
exit
fi