Skip to content

Commit

Permalink
add tzdata package and update echo statements
Browse files Browse the repository at this point in the history
  • Loading branch information
viperadnan-git committed Feb 6, 2024
1 parent 9aed61c commit b77d613
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 41 deletions.
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ RUN apt-get update && apt-get install -y \
curl \
gnupg \
lsb-release \
unzip
unzip \
tzdata
RUN curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc | \
gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg \
--dearmor
Expand Down
12 changes: 9 additions & 3 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
#!/bin/bash

export TZ=${TZ:-"UTC"}

echof() {
builtin echo -e "$(date +"%Y-%m-%dT%H:%M:%S.000%z")\t$*"
}

if [ "$USE_MONGOEXPORT" == "true" ]; then
echo "Using mongoexport"
echof "using mongoexport"
bash mongoexport-rclone.sh
else
echo "Using mongodump"
echof "using mongodump"
bash mongodump-rclone.sh
fi
fi
43 changes: 21 additions & 22 deletions mongodump-rclone.sh
Original file line number Diff line number Diff line change
@@ -1,64 +1,63 @@
#!/bin/bash

echof() {
builtin echo -e "$(date +"%Y-%m-%dT%H:%M:%S.000%z")\t$*"
}

RCLONE_CONFIG=$RCLONE_CONFIG
RCLONE_CONFIG_FILE="/config/rclone/rclone.conf"

# if RCLONE_CONFIG file does not exist and RCLONE_CONFIG environment variable is not set
if [ ! -f "$RCLONE_CONFIG_FILE" ] && [ -z "$RCLONE_CONFIG" ]; then
echo "Error: rclone.conf file does not exist and RCLONE_CONFIG environment variable is not set"
exit 1
echof "Error: rclone.conf file does not exist and RCLONE_CONFIG environment variable is not set"
exit 1
fi

RCLONE_REMOTE=${RCLONE_REMOTE:-"default"}

if [ ! -f "$RCLONE_CONFIG_FILE" ]; then
echo "rclone.conf file does not exist, creating it ($RCLONE_CONFIG_FILE)"
mkdir -p ~/.config/rclone
echo "[$RCLONE_REMOTE]" > $RCLONE_CONFIG_FILE
echo -e "$RCLONE_CONFIG" >> $RCLONE_CONFIG_FILE
echof "rclone.conf file does not exist, creating it ($RCLONE_CONFIG_FILE)"
mkdir -p ~/.config/rclone
echof "[$RCLONE_REMOTE]" >$RCLONE_CONFIG_FILE
echof -e "$RCLONE_CONFIG" >>$RCLONE_CONFIG_FILE
fi

RCLONE_ARGS=${RCLONE_ARGS:-""}

RCLONE_ARGS="$RCLONE_ARGS --config $RCLONE_CONFIG_FILE"
RCLONE_REMOTE_PATH=${RCLONE_REMOTE_PATH:-"/"}


MONGODUMP_ARGS=${MONGODUMP_ARGS:-""}

FILENAME=${FILENAME:-"mongodb"}

if [ -n "$MONGO_DB" ]; then
MONGODUMP_ARGS="$MONGODUMP_ARGS -d $MONGO_DB"
MONGODUMP_ARGS="$MONGODUMP_ARGS -d $MONGO_DB"
fi

if [ -n "$MONGO_COLLECTION" ]; then
MONGODUMP_ARGS="$MONGODUMP_ARGS -c $MONGO_COLLECTION"
MONGODUMP_ARGS="$MONGODUMP_ARGS -c $MONGO_COLLECTION"
fi


FILENAME_TIMESTAMP=${FILENAME_TIMESTAMP:-"true"}

if [ "$FILENAME_TIMESTAMP" = "true" ]; then
FILENAME="$FILENAME-$(date "+%H-%M-%S-%F")"
FILENAME="$FILENAME-$(date "+%H-%M-%S-%F")"
fi

OUTPUT_FILE="$FILENAME.archive"

# if --gzip is in MONGODUMP_ARGS, add .gz to the file name
if [[ $MONGODUMP_ARGS == *"--gzip"* ]]; then
OUTPUT_FILE="$OUTPUT_FILE.gz"
OUTPUT_FILE="$OUTPUT_FILE.gz"
fi


echo "Dumping database to $OUTPUT_FILE"
echo "mongodump options: $MONGODUMP_ARGS"
echo "rclone options: $RCLONE_ARGS"
echof "dumping database to $OUTPUT_FILE"
echof "mongodump options: $MONGODUMP_ARGS"
echof "rclone options: $RCLONE_ARGS"

if [ -n "$MONGO_URI" ]; then
echo "Using MONGO_URI environment variable"
MONGODUMP_ARGS="$MONGODUMP_ARGS --uri=$MONGO_URI"
echof "Using MONGO_URI environment variable for connection with MongoDB"
MONGODUMP_ARGS="$MONGODUMP_ARGS --uri=$MONGO_URI"
fi

mongodump $MONGODUMP_ARGS --archive | \
rclone rcat $RCLONE_ARGS $RCLONE_REMOTE:"$RCLONE_REMOTE_PATH"/"$OUTPUT_FILE"
mongodump $MONGODUMP_ARGS --archive |
rclone rcat $RCLONE_ARGS $RCLONE_REMOTE:"$RCLONE_REMOTE_PATH"/"$OUTPUT_FILE"
33 changes: 18 additions & 15 deletions mongoexport-rclone.sh
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
#!/bin/bash

echof() {
builtin echo -e "$(date +"%Y-%m-%dT%H:%M:%S.000%z")\t$*"
}

RCLONE_CONFIG=$RCLONE_CONFIG
RCLONE_CONFIG_FILE="/config/rclone/rclone.conf"

# if RCLONE_CONFIG file does not exist and RCLONE_CONFIG environment variable is not set
if [ ! -f "$RCLONE_CONFIG_FILE" ] && [ -z "$RCLONE_CONFIG" ]; then
echo "Error: rclone.conf file does not exist and RCLONE_CONFIG environment variable is not set"
echof "Error: rclone.conf file does not exist and RCLONE_CONFIG environment variable is not set"
exit 1
fi

RCLONE_REMOTE=${RCLONE_REMOTE:-"default"}

if [ ! -f "$RCLONE_CONFIG_FILE" ]; then
echo "rclone.conf file does not exist, creating it ($RCLONE_CONFIG_FILE)"
echof "rclone.conf file does not exist, creating it ($RCLONE_CONFIG_FILE)"
mkdir -p ~/.config/rclone
echo "[$RCLONE_REMOTE]" > $RCLONE_CONFIG_FILE
echo -e "$RCLONE_CONFIG" >> $RCLONE_CONFIG_FILE
echof "[$RCLONE_REMOTE]" >$RCLONE_CONFIG_FILE
echof -e "$RCLONE_CONFIG" >>$RCLONE_CONFIG_FILE
fi

RCLONE_ARGS=${RCLONE_ARGS:-""}
Expand All @@ -32,10 +35,10 @@ if [ -n "$MONGO_DB" ]; then
fi

if [ -n "$MONGO_COLLECTION" ]; then
MONGOEXPORT_ARGS="$MONGOEXPORT_ARGS -c $MONGO_COLLECTION"
MONGOEXPORT_ARGS="$MONGOEXPORT_ARGS -c $MONGO_COLLECTION"
else
echo "Error: MONGO_COLLECTION environment variable is not set"
exit 1
echof "Error: MONGO_COLLECTION environment variable is not set"
exit 1
fi

FILENAME_TIMESTAMP=${FILENAME_TIMESTAMP:-"true"}
Expand All @@ -52,18 +55,18 @@ elif [ "$MONGOEXPORT_TYPE" = "csv" ]; then
OUTPUT_FILE="$FILENAME.csv"
MONGOEXPORT_ARGS="$MONGOEXPORT_ARGS --type=csv"
else
echo "Error: MONGOEXPORT_TYPE environment variable is not set to json or csv"
echof "Error: MONGOEXPORT_TYPE environment variable is not set to json or csv"
exit 1
fi

echo "Exporting database to $OUTPUT_FILE"
echo "mongoexport options: $MONGOEXPORT_ARGS"
echo "rclone options: $RCLONE_ARGS"
echof "exporting database to $OUTPUT_FILE"
echof "mongoexport options: $MONGOEXPORT_ARGS"
echof "rclone options: $RCLONE_ARGS"

if [ -n "$MONGO_URI" ]; then
echo "Using MONGO_URI environment variable"
echof "Using MONGO_URI environment variable for connection with MongoDB"
MONGOEXPORT_ARGS="$MONGOEXPORT_ARGS --uri=$MONGO_URI"
fi

mongoexport $MONGOEXPORT_ARGS | \
rclone rcat $RCLONE_ARGS $RCLONE_REMOTE:"$RCLONE_REMOTE_PATH"/"$OUTPUT_FILE"
mongoexport $MONGOEXPORT_ARGS |
rclone rcat $RCLONE_ARGS $RCLONE_REMOTE:"$RCLONE_REMOTE_PATH"/"$OUTPUT_FILE"

0 comments on commit b77d613

Please sign in to comment.