-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentrypoint.sh
More file actions
executable file
·87 lines (69 loc) · 2.43 KB
/
entrypoint.sh
File metadata and controls
executable file
·87 lines (69 loc) · 2.43 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
#!/bin/bash
REMOTE_PATH=${REMOTE_PATH:-'~'}
#
# Check required environment variables
#
REQUIRED=( HOST USER SYNC_1_SRC SYNC_1_DEST )
for i in "${REQUIRED[@]}"
do
if [ -z "${!i}" ]; then
echo -e "Environment variable ${i} is required, exiting..."
exit 1
fi
done
#
# Connect to remote filesystem
#
echo -e "sshfs ${USER}@${HOST}:${REMOTE_PATH} /mnt/remote/"
if [ -n "${PASS}" ]; then
echo ${PASS} | sshfs ${USER}@${HOST}:${REMOTE_PATH} /mnt/remote/ -o password_stdin,StrictHostKeyChecking=no
else
if [ -z "${IDENTITY_FILE}" ]; then
echo -e "Environment variable IDENTITY_FILE is required for public key authentication, exiting..."
exit 1
fi
sshfs ${USER}@${HOST}:${REMOTE_PATH} /mnt/remote/ -o IdentityFile=${IDENTITY_FILE},StrictHostKeyChecking=no
fi
#
# Cycle through syncing SYNC_#_SRC/SYNC_#_DEST pairs
#
get_src() { local tmp; tmp="SYNC_${c}_SRC"; printf %s "${!tmp}"; }
get_dest() { local tmp; tmp="SYNC_${c}_DEST"; printf %s "${!tmp}"; }
c=1
while [ -n "$(get_src)" ]
do
if [ -z "$(get_dest)" ]; then
echo -e "Environment variable SYNC_${c}_DEST is required when SYNC_${c}_SRC is provided, exiting..."
exit 1
fi
echo "Syncing $(get_src) to $(get_dest)"
mkdir -p /dest/$(get_dest)
rsync -avP --delete \
/mnt/remote/$(get_src) \
/dest/$(get_dest)
# Setting ownership
if [ -n "${DEST_USER}" ]; then
# If both environment variables are set
if [ -n "${DEST_GROUP}" ]; then
echo -e "Setting user and group on all files and directories in $(get_dest) to ${DEST_USER}:${DEST_GROUP}"
chown -R ${DEST_USER}:${DEST_GROUP} /dest/$(get_dest)
fi
# If only the user is set
if [ -z "${DEST_GROUP}" ]; then
echo -e "Setting user on all files and directories in $(get_dest) to ${DEST_USER}"
chown -R ${DEST_USER} /dest/$(get_dest)
fi
fi
# Setting file permissions
if [ -n "${DEST_FILE_PERMS}" ]; then
echo -e "Setting permissions on all files in $(get_dest) to ${DEST_FILE_PERMS}"
find /dest/$(get_dest) -type f -exec chmod ${DEST_FILE_PERMS} {} \;
fi
# Setting directory permissions
if [ -n "${DEST_DIR_PERMS}" ]; then
echo -e "Setting permissions on all directories in $(get_dest) to ${DEST_DIR_PERMS}"
find /dest/$(get_dest) -type d -exec chmod ${DEST_DIR_PERMS} {} \;
fi
(( c++ ))
done
echo -e "Sync completed."