Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Backup and Restore commands #623

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions commands/backup.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#!/usr/bin/env bash
[[ ! ${WARDEN_DIR} ]] && >&2 echo -e "\033[31mThis script is not intended to be run directly!\033[0m" && exit 1

WARDEN_ENV_PATH="$(locateEnvPath)" || exit $?
loadEnvConfig "${WARDEN_ENV_PATH}" || exit $?
assertDockerRunning

if (( ${#WARDEN_PARAMS[@]} == 0 )) || [[ "${WARDEN_PARAMS[0]}" == "help" ]]; then
warden backup --help || exit $? && exit $?
fi

### load connection information for the mysql service
DB_VOLUME="${WARDEN_ENV_NAME}_dbdata"
REDIS_VOLUME="${WARDEN_ENV_NAME}_redis"
ES_VOLUME="${WARDEN_ENV_NAME}_esdata"
CONTAINER_NAME="${WARDEN_ENV_NAME}_backup"
ENV_PHP_LOC="$(pwd)/app/etc/env.php"
AUTH_LOC="$(pwd)/auth.json"

"${WARDEN_DIR}/bin/warden" env down

if [ ! -d ".warden/" ]; then
mkdir .warden/
fi

if [ ! -d ".warden/backups" ]; then
mkdir .warden/backups/
fi

ID=$(date +%s)
BACKUP_LOC="$(pwd)/.warden/backups/$ID/"
mkdir $BACKUP_LOC

echo ""
echo ""
echo "------------------ STARTING BACKUP IN: $BACKUP_LOC (no output nor progress) ---------------------"
echo ""

case "${WARDEN_PARAMS[0]}" in
db)

docker run \
--rm --name $CONTAINER_NAME \
--mount source=$DB_VOLUME,target=/data -v \
$BACKUP_LOC:/backup ubuntu bash \

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't this can be done with an existing image instead? (or something smaller such as alpine).

-c "tar -czvf /backup/db.tar.gz /data"


;;
redis)

docker run \
--rm --name $CONTAINER_NAME \
--mount source=$REDIS_VOLUME,target=/data -v \
$BACKUP_LOC:/backup ubuntu bash \

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same suggestion as made earlier.

-c "tar -czvf /backup/redis.tar.gz /data"

;;
elasticserach)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo. This should be "elasticsearch"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should look to add support for OpenSearch here as well, since Warden offers the choice between ES and OS. I'm not certain if the data location is the same (can't see why it would be different) but just something to look at adding.


docker run \
--rm --name $CONTAINER_NAME \
--mount source=$ES_VOLUME,target=/data -v \
$BACKUP_LOC:/backup ubuntu bash \

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same suggestion as made earlier.

-c "tar -czvf /backup/es.tar.gz /data"

;;
all)

docker run \
--rm --name $CONTAINER_NAME \
--mount source=$DB_VOLUME,target=/data -v \
$BACKUP_LOC:/backup ubuntu bash \

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same suggestion as made earlier.

-c "tar -czvf /backup/db.tar.gz /data"

docker run \
--rm --name $CONTAINER_NAME \
--mount source=$REDIS_VOLUME,target=/data -v \
$BACKUP_LOC:/backup ubuntu bash \

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same suggestion as made earlier.

-c "tar -czvf /backup/redis.tar.gz /data"

docker run \
--rm --name $CONTAINER_NAME \
--mount source=$ES_VOLUME,target=/data -v \
$BACKUP_LOC:/backup ubuntu bash \

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same suggestion as made earlier.

-c "tar -czvf /backup/es.tar.gz /data"

if [ -f "$ENV_PHP_LOC" ]; then
cp $ENV_PHP_LOC $BACKUP_LOC
fi

if [ -f "$AUTH_LOC" ]; then
cp "$AUTH_LOC" $BACKUP_LOC
fi

;;
*)
fatal "The command \"${WARDEN_PARAMS[0]}\" does not exist. Please use --help for usage."
;;
esac

tar -czvf "$(pwd)"/.warden/backups/latest.tar.gz $BACKUP_LOC

echo ""
echo ""
echo "------------------ FNISHED BACKUP WITH ID: $ID ---------------------"
echo ""
15 changes: 15 additions & 0 deletions commands/backup.help
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env bash
[[ ! ${WARDEN_DIR} ]] && >&2 echo -e "\033[31mThis script is not intended to be run directly!\033[0m" && exit 1

WARDEN_USAGE=$(cat <<EOF
\033[33mUsage:\033[0m
backup (db, redis, elasticsearch, all)
\033[33mOptions:\033[0m
-h, --help Display this help menu
\033[33mCommands:\033[0m
db backup db volume
redis backup redis volume
elasticsearch backup elasticsearch volume
all backup db, redis and elasticsearch to one file including env.php and auth.json
EOF
)
136 changes: 136 additions & 0 deletions commands/restore.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
#!/usr/bin/env bash
[[ ! ${WARDEN_DIR} ]] && >&2 echo -e "\033[31mThis script is not intended to be run directly!\033[0m" && exit 1


CURRENT_DIR=$(pwd)
WARDEN_ENV_PATH="$(locateEnvPath)" || exit $?
loadEnvConfig "${WARDEN_ENV_PATH}" || exit $?
assertDockerRunning

if [[ ${WARDEN_DB:-1} -eq 0 ]]; then
fatal "Database environment is not used (WARDEN_DB=0)."
fi

### load information
DOCKER_COMPOSER_V=$( docker-compose -v | grep -Eo '[0-9]\.([0-9][0-9]|[0-9])\.[0-9]+')

DB_VOLUME_NAME="dbdata"
REDIS_VOLUME_NAME="redis"
ES_VOLUME_NAME="esdata"
DB_VOLUME="${WARDEN_ENV_NAME}_${DB_VOLUME_NAME}"
REDIS_VOLUME="${WARDEN_ENV_NAME}_${REDIS_VOLUME_NAME}"
ES_VOLUME="${WARDEN_ENV_NAME}_${ES_VOLUME_NAME}"
CONTAINER_NAME="${WARDEN_ENV_NAME}_restore"
LATEST_TIMESTAMP=$(ls "$(pwd)/.warden/backups/" | sort -n | tail -1)
ENV_PHP_LOC="$(pwd)/app/etc/env.php"
AUTH_LOC="$(pwd)/auth.json"
SKIP_DB=0
SKIP_REDIS=0
SKIP_ELASTICSEARCH=0


if [ ! -d ".warden/backups/$LATEST_TIMESTAMP" ]; then
fatal "No backups available in the directory .warden/backups/"
fi

if [ ! -f ".warden/backups/$LATEST_TIMESTAMP/db.tar.gz" ]; then
SKIP_DB=1
fi

if [ ! -f ".warden/backups/$LATEST_TIMESTAMP/redis.tar.gz" ]; then
SKIP_REDIS=1
fi

if [ ! -f ".warden/backups/$LATEST_TIMESTAMP/es.tar.gz" ]; then
SKIP_ELASTICSEARCH=1
fi


echo ""
echo ""
echo "------------------ STARTING INITIALIZATION ---------------------"
echo ""

RUNNING_CONTAINERS=$(warden env ps --services --filter "status=running" | grep 'php-fpm' | sed 's/ *$//g')
if [[ ! -z "$RUNNING_CONTAINERS" ]]; then
"${WARDEN_DIR}/bin/warden" env down
fi

echo ""
echo "------ CREATING CONTAINER (if necessary) ---------------------"
echo ""

if [[ $SKIP_DB -eq 0 ]]; then
if [ ! -z "$(docker volume ls | grep -w $DB_VOLUME)" ]; then
docker volume rm $DB_VOLUME && true # dont fail
fi
docker volume create $DB_VOLUME \
--label com.docker.compose.project=$WARDEN_ENV_NAME \
--label com.docker.compose.version=$DOCKER_COMPOSER_V \
--label com.docker.compose.volume=$DB_VOLUME_NAME
fi

if [[ $SKIP_REDIS -eq 0 ]]; then
if [ ! -z "$(docker volume ls | grep -w $REDIS_VOLUME)" ]; then
docker volume rm $REDIS_VOLUME && true # dont fail
fi
docker volume create $REDIS_VOLUME \
--label com.docker.compose.project=$WARDEN_ENV_NAME \
--label com.docker.compose.version=$DOCKER_COMPOSER_V \
--label com.docker.compose.volume=$REDIS_VOLUME_NAME
fi

if [[ $SKIP_ELASTICSEARCH -eq 0 ]]; then
if [ ! -z "$(docker volume ls | grep -w $ES_VOLUME)" ]; then
docker volume rm $ES_VOLUME && true # dont fail
fi
docker volume create $ES_VOLUME \
--label com.docker.compose.project=$WARDEN_ENV_NAME \
--label com.docker.compose.version=$DOCKER_COMPOSER_V \
--label com.docker.compose.volume=$ES_VOLUME_NAME
fi



echo ""
echo ""
echo "------------------ RESTORING BACKUP FROM: .warden/backups/$LATEST_TIMESTAMP/ (no output nor progress) ---------------------"
echo ""

if [[ $SKIP_DB -eq 0 ]]; then
docker run \
--rm --name $CONTAINER_NAME \
--mount source=$DB_VOLUME,target=/data \
-v $(pwd)/.warden/backups/$LATEST_TIMESTAMP/:/backup ubuntu bash \

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same suggestion as made earlier.

-c "cd /data && tar -xvf /backup/db.tar.gz --strip 1"
fi

if [[ $SKIP_REDIS -eq 0 ]]; then
docker run \
--rm --name $CONTAINER_NAME \
--mount source=$REDIS_VOLUME,target=/data \
-v $(pwd)/.warden/backups/$LATEST_TIMESTAMP/:/backup ubuntu bash \

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same suggestion as made earlier.

-c "cd /data && tar -xvf /backup/redis.tar.gz --strip 1"
fi

if [[ $SKIP_ELASTICSEARCH -eq 0 ]]; then
docker run \
--rm --name $CONTAINER_NAME \
--mount source=$ES_VOLUME,target=/data \
-v $(pwd)/.warden/backups/$LATEST_TIMESTAMP/:/backup ubuntu bash \

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same suggestion as made earlier.

-c "cd /data && tar -xvf /backup/es.tar.gz --strip 1"
fi


if [ -f ".warden/backups/$LATEST_TIMESTAMP/env.php" ]; then
cp .warden/backups/$LATEST_TIMESTAMP/env.php $ENV_PHP_LOC
fi

if [ -f ".warden/backups/$LATEST_TIMESTAMP/auth.json" ]; then
cp .warden/backups/$LATEST_TIMESTAMP/auth.json $AUTH_LOC
fi

echo ""
echo ""
echo "------------------ FNISHED BACKUP FROM: .warden/backups/$LATEST_TIMESTAMP/ ---------------------"
echo ""
10 changes: 10 additions & 0 deletions commands/restore.help
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env bash
[[ ! ${WARDEN_DIR} ]] && >&2 echo -e "\033[31mThis script is not intended to be run directly!\033[0m" && exit 1

WARDEN_USAGE=$(cat <<EOF
\033[33mUsage:\033[0m
restore restore redis, elasticsearch and database
\033[33mOptions:\033[0m
-h, --help Display this help menu
EOF
)