-
Notifications
You must be signed in to change notification settings - Fork 1
/
sync.sh
executable file
·46 lines (34 loc) · 887 Bytes
/
sync.sh
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
#!/bin/bash
set -Eeuo pipefail
if [ -z "$S3_BUCKET" ] || [ -z "$DESTINATION" ] || [ -z "$INTERVAL" ]; then
echo "Must set S3_BUCKET, INTERVAL, and DESTINATION env vars" 1>&2
exit 1
fi
# OWNER_UID defaults to 0
if [ -z "$OWNER_UID" ]; then
OWNER_UID=0
fi
# OWNER_GID default to OWNER_UID
if [ -z "$OWNER_GID" ]; then
OWNER_GID=$OWNER_UID
fi
function pull_data {
echo "Pulling initial Data from S3"
aws s3 sync s3://$S3_BUCKET $DESTINATION
# Optionally set file permissions
echo "Setting permissions to $OWNER_UID:$OWNER_GID"
chown -R $OWNER_UID:$OWNER_GID $DESTINATION
touch /initial_sync_completed
echo "Done"
}
pull_data
function push_data {
echo "Pushing Data to S3"
aws s3 sync $DESTINATION s3://$S3_BUCKET
}
trap push_data SIGHUP SIGINT SIGTERM
while true; do
s=`date +'%s'`
push_data
sleep $(( $INTERVAL - (`date +'%s'` - $s) ))
done