backups
This commit is contained in:
@ -1,5 +1,7 @@
|
||||
#!/bin/bash
|
||||
|
||||
source _dockerhelper.sh
|
||||
|
||||
# Print error message and exit with code 1
|
||||
# Usage: die "error message"
|
||||
die() {
|
||||
@ -44,47 +46,11 @@ grey_end() {
|
||||
echo -e -n "\033[0m"
|
||||
}
|
||||
|
||||
# Test if Docker is installed and working on the local machine
|
||||
# Returns 0 if Docker is working, 1 if there are any issues
|
||||
test_docker() {
|
||||
echo "Testing Docker on local machine..."
|
||||
|
||||
# Test Docker installation
|
||||
if ! command -v docker >/dev/null 2>&1; then
|
||||
die "Docker is not installed on this machine"
|
||||
fi
|
||||
|
||||
# Test Docker daemon is running
|
||||
if ! docker info >/dev/null 2>&1; then
|
||||
die "Docker daemon is not running"
|
||||
fi
|
||||
|
||||
# Test Docker can run containers
|
||||
if ! docker run --rm hello-world >/dev/null 2>&1; then
|
||||
die "Docker cannot run containers"
|
||||
fi
|
||||
|
||||
echo "Docker is working correctly on local machine"
|
||||
return 0
|
||||
}
|
||||
|
||||
|
||||
start_container() {
|
||||
# Check if container exists and is stopped
|
||||
echo "Checking container status..."
|
||||
if docker ps -a --format '{{.Names}}' | grep -q "^${CONTAINER_NAME}$"; then
|
||||
if docker ps --format '{{.Names}}' | grep -q "^${CONTAINER_NAME}$"; then
|
||||
die "Container ${CONTAINER_NAME} is already running"
|
||||
else
|
||||
echo "Starting existing container ${CONTAINER_NAME}..."
|
||||
grey_start
|
||||
if ! docker start "${CONTAINER_NAME}"; then
|
||||
die "Failed to start container ${CONTAINER_NAME}"
|
||||
fi
|
||||
grey_end
|
||||
fi
|
||||
create_and_start_container() {
|
||||
if _is_container_exists $CONTAINER_NAME; then
|
||||
_is_container_running $CONTAINER_NAME && return 0
|
||||
_start_container $CONTAINER_NAME
|
||||
else
|
||||
echo "Creating and starting new container ${CONTAINER_NAME}..."
|
||||
grey_start
|
||||
docker run -d \
|
||||
--restart unless-stopped \
|
||||
@ -95,33 +61,11 @@ start_container() {
|
||||
grey_end
|
||||
fi
|
||||
|
||||
# check if the container is running
|
||||
if ! docker ps --format '{{.Names}}' | grep -q "^${CONTAINER_NAME}$"; then
|
||||
die "Container ${CONTAINER_NAME} is not running"
|
||||
if ! _is_container_running $CONTAINER_NAME; then
|
||||
die "Container ${CONTAINER_NAME} failed to start"
|
||||
fi
|
||||
|
||||
# print the container id
|
||||
ID=$(docker ps --format '{{.ID}}' --filter "name=^${CONTAINER_NAME}$")
|
||||
ID=$(_get_container_id $CONTAINER_NAME)
|
||||
echo "Container ${CONTAINER_NAME} is running with ID ${ID}"
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
stop_container() {
|
||||
# Check if container is running
|
||||
echo "Checking if container is running..."
|
||||
if docker ps --format '{{.Names}}' | grep -q "^${CONTAINER_NAME}$"; then
|
||||
echo "Stopping container ${CONTAINER_NAME}..."
|
||||
grey_start
|
||||
if ! docker stop "${CONTAINER_NAME}"; then
|
||||
die "Failed to stop container ${CONTAINER_NAME}"
|
||||
fi
|
||||
grey_end
|
||||
echo "Container ${CONTAINER_NAME} stopped successfully"
|
||||
return 0
|
||||
else
|
||||
echo "Container ${CONTAINER_NAME} is not running"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
|
81
templates/squashkiwi/_dockerhelper.sh
Normal file
81
templates/squashkiwi/_dockerhelper.sh
Normal file
@ -0,0 +1,81 @@
|
||||
# Routines for interacting with docker
|
||||
|
||||
|
||||
# Check if docker is installed
|
||||
_check_docker_installed() {
|
||||
if ! command -v docker &> /dev/null; then
|
||||
echo "Docker is not installed"
|
||||
return 1
|
||||
fi
|
||||
|
||||
# check if docker daemon is running
|
||||
if ! docker info &> /dev/null; then
|
||||
echo "Docker daemon is not running"
|
||||
return 1
|
||||
fi
|
||||
|
||||
# check if user has permission to run docker
|
||||
if ! docker run --rm hello-world &> /dev/null; then
|
||||
echo "User does not have permission to run docker"
|
||||
return 1
|
||||
fi
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
# Check if a container exists
|
||||
_is_container_exists() {
|
||||
if ! docker ps -a --format "{{.Names}}" | grep -q "^$1$"; then
|
||||
return 1
|
||||
fi
|
||||
return 0
|
||||
}
|
||||
|
||||
# Check if a container is running
|
||||
_is_container_running() {
|
||||
if ! docker ps --format "{{.Names}}" | grep -q "^$1$"; then
|
||||
return 1
|
||||
fi
|
||||
return 0
|
||||
}
|
||||
|
||||
# get contianer ID
|
||||
_get_container_id() {
|
||||
docker ps --format "{{.ID}}" --filter "name=$1"
|
||||
}
|
||||
|
||||
# get container status
|
||||
_get_container_status() {
|
||||
docker ps --format "{{.Status}}" --filter "name=$1"
|
||||
}
|
||||
|
||||
# start container that exists
|
||||
_start_container() {
|
||||
_is_container_exists $1 || return 1
|
||||
_is_container_running $1 && return 0
|
||||
docker start $1
|
||||
}
|
||||
|
||||
# stop container that exists
|
||||
_stop_container() {
|
||||
_is_container_running $1 || return 0;
|
||||
docker stop $1
|
||||
}
|
||||
|
||||
# remove container that exists
|
||||
_remove_container() {
|
||||
_stop_container $1
|
||||
_is_container_exists $1 || return 0;
|
||||
docker rm $1
|
||||
}
|
||||
|
||||
# get container logs
|
||||
_get_container_logs() {
|
||||
if ! _is_container_exists $1; then
|
||||
echo "Container $1 does not exist"
|
||||
return 1
|
||||
fi
|
||||
|
||||
docker logs $1
|
||||
}
|
||||
|
@ -1,24 +1,34 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Source common functions
|
||||
source "$(dirname "$0")/_dockerhelper.sh"
|
||||
source "$(dirname "$0")/_common.sh"
|
||||
|
||||
# Load environment variables
|
||||
load_env "$1" || die "Failed to load environment variables"
|
||||
|
||||
# set the backup file name to be the current date and time
|
||||
BACKUP_FILE="$BACKUP_FOLDER/backup-squashkiwi-$(date +%Y-%m-%d_%H-%M-%S).tar.gz"
|
||||
|
||||
if [ -f "$BACKUP_FILE" ]; then
|
||||
echo "Backup file $BACKUP_FILE already exists"
|
||||
exit 1
|
||||
# Get backup file path from second argument
|
||||
BACKUP_FILE="$2"
|
||||
if [ -z "$BACKUP_FILE" ]; then
|
||||
die "Backup file path not provided"
|
||||
fi
|
||||
|
||||
stop_container
|
||||
# Check if backup file already exists
|
||||
if [ -f "$BACKUP_FILE" ]; then
|
||||
die "Backup file $BACKUP_FILE already exists"
|
||||
fi
|
||||
|
||||
# create the backup file, using relative paths.
|
||||
tar zcvf "$BACKUP_FILE" -C "$DATA_FOLDER" . || die "Failed to create backup"
|
||||
# Stop container before backup
|
||||
_stop_container "$CONTAINER_NAME"
|
||||
|
||||
start_container
|
||||
# Create backup of data folder
|
||||
echo "Creating backup of $DATA_FOLDER..."
|
||||
if ! tar zcvf "$BACKUP_FILE" -C "$DATA_FOLDER" .; then
|
||||
_start_container "$CONTAINER_NAME"
|
||||
die "Failed to create backup"
|
||||
fi
|
||||
|
||||
echo "Backup created in $BACKUP_FILE"
|
||||
# Start container after backup
|
||||
_start_container "$CONTAINER_NAME"
|
||||
|
||||
echo "Backup created successfully: $BACKUP_FILE"
|
||||
|
@ -9,7 +9,6 @@ HOST_PORT=80
|
||||
# Deployment settings
|
||||
DEPLOY_FOLDER="${HOME}/sk/deploy"
|
||||
DATA_FOLDER="${HOME}/sk/data"
|
||||
BACKUP_FOLDER="${HOME}/sk/backups"
|
||||
CONTAINER_NAME="squashkiwi"
|
||||
|
||||
# Image settings
|
||||
|
@ -1,19 +1,18 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Source common functions
|
||||
source "$(dirname "$0")/_dockerhelper.sh"
|
||||
source "$(dirname "$0")/_common.sh"
|
||||
|
||||
# Load environment variables
|
||||
load_env "$1" || exit 1
|
||||
|
||||
# Test Docker
|
||||
if ! test_docker; then
|
||||
if ! _check_docker_installed; then
|
||||
echo "Docker test failed, aborting installation..."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Rest of your install script here
|
||||
|
||||
function create_folder() {
|
||||
local folder="$1"
|
||||
if ! mkdir -p "$folder"; then
|
||||
@ -34,3 +33,8 @@ if ! docker pull "$IMAGE_REGISTRY/$IMAGE_REPO:$IMAGE_TAG"; then
|
||||
fi
|
||||
echo "Successfully pulled the docker image from the registry"
|
||||
|
||||
# start the container
|
||||
_stop_container $CONTAINER_NAME
|
||||
create_and_start_container || die "Failed to start container ${CONTAINER_NAME}"
|
||||
|
||||
echo "Installation complete"
|
||||
|
@ -1,12 +1,13 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Source common functions
|
||||
source "$(dirname "$0")/_dockerhelper.sh"
|
||||
source "$(dirname "$0")/_common.sh"
|
||||
|
||||
# Load environment variables
|
||||
load_env "$1" || die "Failed to load environment variables"
|
||||
|
||||
start_container || die "Failed to start container ${CONTAINER_NAME}"
|
||||
create_and_start_container || die "Failed to start container ${CONTAINER_NAME}"
|
||||
|
||||
grey_start
|
||||
docker logs --tail 20 "${CONTAINER_NAME}"
|
||||
|
@ -1,24 +1,23 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Source common functions
|
||||
source "$(dirname "$0")/_dockerhelper.sh"
|
||||
source "$(dirname "$0")/_common.sh"
|
||||
|
||||
# Load environment variables
|
||||
load_env "$1" || exit 1
|
||||
|
||||
# check if the service is running
|
||||
if ! docker ps | grep -q "$CONTAINER_NAME"; then
|
||||
echo "Service is not running"
|
||||
if ! _is_container_running $CONTAINER_NAME; then
|
||||
echo "Service is not running - did not find container $CONTAINER_NAME."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "Service is running"
|
||||
|
||||
# curl -s -X GET http://localhost:8080/health | grep -q "OK"
|
||||
if ! curl -s -X GET http://localhost:${HOST_PORT}/health | grep -q "OK"; then
|
||||
echo "Service is not healthy"
|
||||
echo "Service is not healthy - did not get OK response from /health endpoint."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "Service is healthy"
|
||||
return 0
|
||||
exit 0
|
||||
|
@ -1,12 +1,13 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Source common functions
|
||||
source "$(dirname "$0")/_dockerhelper.sh"
|
||||
source "$(dirname "$0")/_common.sh"
|
||||
|
||||
# Load environment variables
|
||||
load_env "$1" || die "Failed to load environment variables"
|
||||
|
||||
stop_container || die "Failed to stop container ${CONTAINER_NAME}"
|
||||
_stop_container $CONTAINER_NAME || die "Failed to stop container ${CONTAINER_NAME}"
|
||||
|
||||
grey_start
|
||||
docker logs --tail 20 "${CONTAINER_NAME}"
|
||||
|
@ -1,6 +1,7 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Source common functions
|
||||
source "$(dirname "$0")/_dockerhelper.sh"
|
||||
source "$(dirname "$0")/_common.sh"
|
||||
|
||||
# Load environment variables
|
||||
@ -14,7 +15,7 @@ fi
|
||||
echo "Successfully pulled the docker image from the registry"
|
||||
|
||||
# stop the old container
|
||||
stop_container
|
||||
_stop_container ${CONTAINER_NAME}
|
||||
|
||||
# remove the old container
|
||||
grey_start
|
||||
@ -26,5 +27,6 @@ grey_end
|
||||
echo "Successfully removed the old container"
|
||||
|
||||
# start the new container
|
||||
start_container
|
||||
create_and_start_container || die "Failed to start container ${CONTAINER_NAME}"
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user