-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_backup.sh
44 lines (37 loc) · 1.2 KB
/
run_backup.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
#!/bin/bash
# Set variables
IMAGE_NAME="backup-image"
CONTAINER_NAME="backup-container"
DOCKERFILE="Dockerfile.backup"
# Load environment variables from .env file
if [ -f .env ]; then
echo "Loading environment variables from .env file..."
export $(grep -v '^#' .env | xargs)
else
echo ".env file not found. Exiting..."
exit 1
fi
# Build the Docker image
echo "Building Docker image from $DOCKERFILE..."
docker build -t $IMAGE_NAME -f $DOCKERFILE .
# Check if the container already exists
if [ "$(docker ps -aq -f name=$CONTAINER_NAME)" ]; then
echo "Removing existing container $CONTAINER_NAME..."
docker rm -f $CONTAINER_NAME
fi
# Run the container with environment variables and volume mounts
echo "Running backup container..."
docker run --name $CONTAINER_NAME \
--env-file .env \
-v "${SOURCE_DIR:-/path/to/source}:/data" \
-v "${BACKUP_DIR:-/path/to/backup}:/backup" \
$IMAGE_NAME
# Check if the container exists and remove it
if [ "$(docker ps -aq -f name=$CONTAINER_NAME)" ]; then
echo "Removing the completed container..."
docker rm -f $CONTAINER_NAME
else
echo "Container not found or already removed."
fi
# Completion message
echo "Backup process completed and container cleaned up."