-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathremsync
executable file
·115 lines (103 loc) · 2.77 KB
/
remsync
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
#!/usr/bin/env bash
# helper script for syncing files to my server using rsync
# this syncs a copy of the directory, if you delete files
# locally, it deletes it up on the remote
#
# remsync [FILE...] [DIR...]
#
# If files/directories are passed as arguments, they're copied to
# $FROM_DIR and are then pushed to the server
#
# to delete remote files for REMSYNC_PUBLIC, set REMSYNC_DELETE=1
#
# files are served by nginx as static files, with:
#
# private remsync
#
# rewrite ^/f$ /f/ permanent;
#
# location /f/ {
# alias /home/sean/f/; # location
#
# # dont serve directories
# try_files $uri =404;
# autoindex off;
#
# # make sure files are downloaded instead of viewed
# expires -1;
# default_type application/octet-stream;
# }
#
# public remsync
#
# rewrite ^/p$ /p/ permanent;
#
# location /p/ {
# alias /home/sean/p/; # location
# try_files $uri $uri/ =404;
# autoindex on;
# autoindex_exact_size off;
# autoindex_localtime on;
# add_header "Access-Control-Allow-Origin" *;
# expires 1h;
# add_header Cache-Control "public";
# }
split() {
echo "#########################"
}
########## CONFIGURATION
# server information
readonly SYNC_USER="sean"
readonly SSH_TO='vultr' # setup in ~/.ssh/config
declare TO_DIR="/home/${SYNC_USER}/f/"
[[ -n "$REMSYNC_PUBLIC" ]] && TO_DIR="/home/${SYNC_USER}/p/"
readonly BASE_URL="https://purarue.xyz"
readonly TO_DIR
# local information
declare FROM_DIR="${XDG_DOCUMENTS_DIR}/remsync/"
[[ -n "$REMSYNC_PUBLIC" ]] && FROM_DIR="${HOME}/Files/remsync_public/"
readonly FROM_DIR
mkdir -p "${FROM_DIR}"
echo "$FROM_DIR"
########## RUN
# make local dir if it doesn't exist
[[ ! -d "$FROM_DIR" ]] && mkdir -p "$FROM_DIR"
# if files were passed, copy files to local sync dir
if (($# > 0)); then
for path in "$@"; do
if [[ -r "$path" ]]; then
cp -R "$path" "$FROM_DIR"
else
printf "No such file: %s\n" "$path" 1>&2
exit 1
fi
done
split
fi
# sync directory
rsync_args=(--chmod='D0755,F644' --archive --verbose --human-readable --partial --progress)
[[ -z "$REMSYNC_PUBLIC" || -n "$REMSYNC_DELETE" ]] && rsync_args+=("--delete-before")
rsync_args+=(-e 'ssh' "$FROM_DIR" "${SSH_TO}:${TO_DIR}")
echo 'Running: rsync' "${rsync_args[@]}" >&2
rsync "${rsync_args[@]}" || exit $?
split
# print urls of synced files
if (($# > 0)); then
split
# get the private/public part of the URL (f/F)
dir_part="$(rev <<<"$TO_DIR" | cut -d'/' -f 2)"
# for each item
for path in "$@"; do
basename_path="$(basename "$path")"
# if directory
if [[ ! -d "$path" ]]; then
# if file, just use filename to get url
FILEURL="$(printf "%s/%s/%s\n" "$BASE_URL" "$dir_part" "$basename_path")"
echo "$FILEURL"
[[ "$#" == 1 && -f "$1" ]] && {
printf "%s" "$FILEURL" | clipcopy
notify "Copied ${FILEURL} to your clipboard"
}
fi
done
fi