I am working on database backup functionality with next cloud. My script is working perfectly with hard coded Url paths. now I want to create environment variables for the username, password, paths which I am using in my code. I tried many ways but when I run my container in docker compose the env variables are not accessible and they are empty. This is my shell script:
#!/bin/bash
#set -o allexport
#source .env
#set +o allexport
echo "`date` Starting MongoDB backup...";
# Parse Command Line Arguments
while getopts u:d: flag
do
case "${flag}" in
u) uri=${OPTARG};;
d) dest=${OPTARG};;
esac
done
# Get the current date and time
CURRENT_DATE=`date +%y%m%d-%H%M%S`;
DESTPATH="$dest/$CURRENT_DATE";
echo "`date` Backup destination is $DESTPATH";
echo $uri
# Create the destination directory and backup the database
mkdir -p $DESTPATH;
mongodump --uri=$uri -o $DESTPATH --gzip
# Set your nextcloud username and password
username=${NEXT_CLOUD_USERNAME};
password=${NEXT_CLOUD_PASSWORD};
# Set your mongodb path running on your system
# mongodbPath="http://localhost:8080/";
mongodbPath=${NEXT_CLOUD_PATH};
# Nextcloud path
nextcloudPath="remote.php/dav/files/";
# Concat username with the nextcloud path
nextcloudPath=$nextcloudPath$username;
nextcloudPath=$nextcloudPath"/";
# Concat the mongodb path with the nextcloud path
uriPath=$uriPath$mongodbPath;
uriPath=$uriPath$nextcloudPath;
#mkDirPath=$uriPath${NEXT_CLOUD_FOLDER};
#echo $mkDirPath
#curl -X MKCOL -u $username:$password $uriPath
uriPath=$uriPath$CURRENT_DATE;
uriPath=$uriPath".zip"
echo $uriPath
#craete zip for the backup folder
zip -r $DESTPATH.zip $DESTPATH
# Upload backup zip file to nextcloud
curl -X PUT -u $username:$password $uriPath -T $DESTPATH.zip
echo "`date` MongoDB backup completed.";
exit 0
Docker file:
FROM mongo:latest
RUN apt-get clean
RUN apt-get update
RUN apt-get install -y cron
RUN apt-get install -y curl
RUN apt-get install -y zip
ENV MONGODB_PATH=mongodb://mongo:27017
ENV NEXT_CLOUD_PATH=http://nextcloud
ENV NEXT_CLOUD_FOLDER=backup
ENV NEXT_CLOUD_USERNAME=admin
ENV NEXT_CLOUD_PASSWORD=example
ADD /script/container_cronjob /etc/cron.d/container_cronjob
RUN chmod 0644 /etc/cron.d/container_cronjob
RUN touch /var/log/cron.log
WORKDIR /usr/src/app
COPY ./script/backup.sh /backup.sh
COPY . .
RUN chmod +x /backup.sh
CMD env
CMD cron && tail -f /var/log/cron.log
Docker-Compose file:
version: "3"
services:
mongo:
container_name: "mongo"
image: "mongo:latest"
nextcloud:
container_name: "nextcloud"
image: "nextcloud:latest"
ports:
- "8080:80"
volumes:
- ./data:/var/www/html/data
- ./config:/var/www/html/config
backup:
container_name: "backup"
image: "d432f54c96df"
environment:
- MONGODB_PATH=mongodb://mongo:27017
- NEXT_CLOUD_PATH=http://nextcloud
- NEXT_CLOUD_FOLDER=backup
- NEXT_CLOUD_USERNAME=admin
- NEXT_CLOUD_PASSWORD=example
First I tried making env variables in docker-compose file as you can see in docker-compose file and try to access in my shell script using ${} way but it didn't work then I tried making an .env file and put my variables in it and try to access in shell script it didn't work. .env file:
MONGODB_PATH=mongodb://mongo:27017
NEXT_CLOUD_PATH=http://nextcloud/
NEXT_CLOUD_FOLDER=backup
NEXT_CLOUD_USERNAME=admin
NEXT_CLOUD_PASSWORD=example
with .env file I put these line
set -o allexport
source .env
set +o allexport
in top of my shell script but it gave me error that .env is not available in my docker container but I copied both my script and .env into container.
I also tried making variables in docker file as you can see in my docker file. It still not working. my containers are working:containers
when my script run in backup docker container it shows path is not setting because username and password is not setscript error
in image you can see remote.php/dav/files//231013-111101.zip has no username of nextcloud but I set it in my env variables.
It would be really helpful if someone guide me where I am wrong or how I can access my env variables. Thanks