-
Notifications
You must be signed in to change notification settings - Fork 13
/
run.sh
executable file
·64 lines (50 loc) · 1.52 KB
/
run.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/bin/bash
# Set sane bash defaults
set -o errexit
set -o pipefail
OPTION="$1"
ACCESS_KEY=${ACCESS_KEY:?"ACCESS_KEY required"}
SECRET_KEY=${SECRET_KEY:?"SECRET_KEY required"}
S3PATH=${S3PATH:?"S3_PATH required"}
CRON_SCHEDULE=${CRON_SCHEDULE:-0 * * * *}
S3CMDPARAMS=${S3CMDPARAMS}
LOCKFILE="/tmp/s3cmd.lock"
LOG="/var/log/cron.log"
echo "access_key=$ACCESS_KEY" >> /root/.s3cfg
echo "secret_key=$SECRET_KEY" >> /root/.s3cfg
trap "rm -f $LOCKFILE" EXIT
if [ ! -e $LOG ]; then
touch $LOG
fi
if [[ $OPTION = "start" ]]; then
CRONFILE="/etc/cron.d/s3backup"
CRONENV=""
echo "Found the following files and directores mounted under /data:"
echo
ls -F /data
echo
echo "Adding CRON schedule: $CRON_SCHEDULE"
CRONENV="$CRONENV ACCESS_KEY=$ACCESS_KEY"
CRONENV="$CRONENV SECRET_KEY=$SECRET_KEY"
CRONENV="$CRONENV S3PATH=$S3PATH"
CRONENV="$CRONENV S3CMDPARAMS=\"$S3CMDPARAMS\""
echo "$CRON_SCHEDULE root $CRONENV bash /run.sh backup" >> $CRONFILE
echo "Starting CRON scheduler: $(date)"
cron
exec tail -f $LOG 2> /dev/null
elif [[ $OPTION = "backup" ]]; then
echo "Starting sync: $(date)" | tee $LOG
if [ -f $LOCKFILE ]; then
echo "$LOCKFILE detected, exiting! Already running?" | tee -a $LOG
exit 1
else
touch $LOCKFILE
fi
echo "Executing s3cmd sync $S3CMDPARAMS /data/ $S3PATH..." | tee -a $LOG
/usr/local/bin/s3cmd sync $S3CMDPARAMS /data/ $S3PATH 2>&1 | tee -a $LOG
rm -f $LOCKFILE
echo "Finished sync: $(date)" | tee -a $LOG
else
echo "Unsupported option: $OPTION" | tee -a $LOG
exit 1
fi