-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker-entrypoint.sh
More file actions
executable file
·191 lines (158 loc) · 5.55 KB
/
Copy pathdocker-entrypoint.sh
File metadata and controls
executable file
·191 lines (158 loc) · 5.55 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/bin/bash
# Controlled by the following ENV: -
#
# REPLICATE_DIRECTION (AtoB or BtoA)
# REPLICATE_DELETE (yes)
# REPLICATE_QUIETLY (yes)
#
# VOLUME_A_IS_S3 (just needs to exist)
#
# AWS_ACCESS_KEY_ID
# AWS_SECRET_ACCESS_KEY
# AWS_ENDPOINTS
# AWS_DEFAULT_REGION
# S3_BUCKET_NAME
# S3_REQUEST_STYLE
#
# USE_RCLONE Set to 'yes' to rclone to the (S3) destination
# USE_RCLONE_NO_CHECK_CERTIFICATE Set to 'yes' to avoid certificate checks
# USE_DOW_FOR_RCLONE Set to 'yes' to use day of the week as a destination sub-directory
# RCLONE_EXTRA_OPTIONS Extra options appended to the rclone command
# Is the replica direction set?
: "${REPLICATE_DIRECTION?is not set}"
# And a valid value?
case "$REPLICATE_DIRECTION" in
AtoB) SRC="/volume-a"
DST="/volume-b"
;;
BtoA) SRC="/volume-b"
DST="/volume-a"
;;
esac
if [ -z "$SRC" ]; then
echo "ERROR: Invalid REPLICATE_DIRECTION \"$REPLICATE_DIRECTION\""
exit
fi
echo "--] Ciao! ($(date))"
echo "--] REPLICATE_DIRECTION is ${REPLICATE_DIRECTION}"
echo "--] SRC is ${SRC}"
echo "--] DST is ${DST}"
echo "--] REPLICATE_DELETE is ${REPLICATE_DELETE:-yes}"
echo "--] REPLICATE_QUIETLY is ${REPLICATE_QUIETLY:-yes}"
# Is S3 the origin (AtoB) or destination (BtoA)?
# Here we've copied much of the logic from our 'bandr' repository.
#
# If using S3 for volume-a the user sets the variable 'VOLUME_A_IS_S3'
# and this code mounts the S3 bucket at /volume-a.
#
# (see https://github.com/s3fs-fuse/s3fs-fuse)
if [ -v VOLUME_A_IS_S3 ]; then
echo "--] Volume A is S3"
# Certain credentials are essential...
: "${AWS_ACCESS_KEY_ID?Need to set AWS_ACCESS_KEY_ID}"
: "${AWS_SECRET_ACCESS_KEY?Need to set AWS_SECRET_ACCESS_KEY}"
: "${S3_BUCKET_NAME?Need to set S3_BUCKET_NAME}"
echo "--] AWS_ACCESS_KEY_ID is (supplied)"
echo "--] AWS_SECRET_ACCESS_KEY is (supplied)"
echo "--] AWS_ENDPOINTS is ${AWS_ENDPOINTS}"
echo "--] S3_BUCKET_NAME is ${S3_BUCKET_NAME}"
echo "--] S3_REQUEST_STYLE is ${S3_REQUEST_STYLE}"
# We'll use s3fs to mount the bucket so it can be used
# as a conventional file-system.
#
# For this process to work the container MUST run in privileged mode.
# e.g. - if launching with the docker command
# you must use add the `--privileged` option.
# Put S3 credentials in a custom passwd file...
echo "${AWS_ACCESS_KEY_ID}:${AWS_SECRET_ACCESS_KEY}" > /tmp/.passwd-s3fs
chmod 600 /tmp/.passwd-s3fs
# Any extra S3-Fuse args required?
# i.e. is S3_URL or S3_REQUEST_STYLE defined?
S3FS_EXTRA_OPTIONS=""
if [ -n "$AWS_ENDPOINTS" ]; then
S3FS_EXTRA_OPTIONS+="-o url=${AWS_ENDPOINTS}"
fi
if [ -n "$S3_REQUEST_STYLE" ]; then
S3FS_EXTRA_OPTIONS+=" -o ${S3_REQUEST_STYLE}"
fi
# We create volume-a, but does volume-b exist?
if [ ! -d "/volume-b" ]; then
echo "Directory /volume-b DOES NOT exist."
exit
fi
# Create the S3 mount point and then invoke s3fs
mkdir -p /volume-a
S3FS_CMD_OPTIONS="/volume-a -o passwd_file=/tmp/.passwd-s3fs ${S3FS_EXTRA_OPTIONS}"
echo "--] s3fs S3_BUCKET_NAME=${S3_BUCKET_NAME}"
echo "--] s3fs S3FS_CMD_OPTIONS=${S3FS_CMD_OPTIONS}"
s3fs -d -o sigv2 ${S3_BUCKET_NAME} ${S3FS_CMD_OPTIONS}
echo "--] s3fs started ($?)"
elif [ -v USE_RCLONE ]; then
# Ensure the source volume exists...
if [ ! -d "$SRC" ]; then
echo "SRC directory ($SRC) DOES NOT exist."
exit
fi
else
# Ensure the source and destination volumes exist...
if [ ! -d "$SRC" ]; then
echo "SRC directory ($SRC) DOES NOT exist."
exit
elif [ ! -d "$DST" ]; then
echo "DST directory ($DST) DOES NOT exist."
exit
fi
fi
if [ "$USE_RCLONE" == "yes" ]; then
if [ "$REPLICATE_DIRECTION" == "AtoB" ]; then
echo REPLICATE_DIRECTION cannot be AtoB
exit 1
fi
# Certain credentials are essential...
: "${AWS_ACCESS_KEY_ID?Need to set AWS_ACCESS_KEY_ID}"
: "${AWS_SECRET_ACCESS_KEY?Need to set AWS_SECRET_ACCESS_KEY}"
: "${AWS_DEFAULT_REGION?Need to set AWS_DEFAULT_REGION}"
: "${S3_BUCKET_NAME?Need to set S3_BUCKET_NAME}"
RCLONE_SUB_DIR=""
USE_DOW_FOR_RCLONE=${USE_DOW_FOR_RCLONE:-no}
if [ "$USE_DOW_FOR_RCLONE" == "yes" ]; then
DOW=$(date +"%u-%A")
RCLONE_SUB_DIR="/${DOW}"
fi
DELETE=${REPLICATE_DELETE:-yes}
if [ "$DELETE" == "yes" ]; then
RCLONE_CMD="sync"
else
RCLONE_CMD="copy"
fi
RCLONE_OPTIONS=""
if [ "$USE_RCLONE_NO_CHECK_CERTIFICATE" == "yes" ]; then
RCLONE_OPTIONS="--no-check-certificate"
fi
RCLONE_EXTRA_OPTIONS=${RCLONE_EXTRA_OPTIONS:-}
echo "--] AWS_ACCESS_KEY_ID is (supplied)"
echo "--] AWS_SECRET_ACCESS_KEY is (supplied)"
echo "--] AWS_DEFAULT_REGION is ${AWS_DEFAULT_REGION}"
echo "--] RCLONE_S3_ENDPOINT is ${RCLONE_S3_ENDPOINT}"
echo "--] RCLONE_S3_PROVIDER is ${RCLONE_S3_PROVIDER}"
echo "--] S3_BUCKET_NAME is ${S3_BUCKET_NAME}"
echo "--] USE_DOW_FOR_RCLONE is ${USE_DOW_FOR_RCLONE:-no}"
echo "--] RCLONE_OPTIONS=${RCLONE_OPTIONS}"
echo "--] RCLONE_EXTRA_OPTIONS=${RCLONE_EXTRA_OPTIONS}"
echo "--] Replicating with rclone $RCLONE_CMD to remote:/${S3_BUCKET_NAME}${RCLONE_SUB_DIR}..."
rclone $RCLONE_CMD $SRC remote:/${S3_BUCKET_NAME}${RCLONE_SUB_DIR} ${RCLONE_OPTIONS} ${RCLONE_EXTRA_OPTIONS}
echo "--] Done"
else
DELETE=${REPLICATE_DELETE:-yes}
if [ "$DELETE" == "yes" ]; then
RSYNC_XTRA_OPTIONS="--delete"
fi
QUIET=${REPLICATE_QUIETLY:-yes}
if [ "$QUIET" == "yes" ]; then
RSYNC_QUIET="--quiet"
fi
echo "--] Replicating with rsync (RSYNC_XTRA_OPTIONS=$RSYNC_XTRA_OPTIONS)..."
rsync -a --exclude-from='./rsync-exclude.txt' $RSYNC_XTRA_OPTIONS $RSYNC_QUIET $SRC/ $DST
echo "--] Done"
fi
echo "--] Arrivederci! ($(date))"