Skip to content

Commit

Permalink
fixed bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Cosmicoppai committed May 17, 2024
1 parent b64b86d commit 429a781
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 65 deletions.
3 changes: 2 additions & 1 deletion xbackup/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ RUN apt-get update && \
COPY entrypoint.sh /entrypoint.sh
COPY backup.sh /usr/local/bin/backup.sh
COPY prepare.sh /usr/local/bin/prepare.sh
COPY apply.sh /usr/local/bin/apply.sh

RUN mkdir -p /xbackup/ /etc/cron.d /var/log && \
chmod +x /usr/local/bin/backup.sh /usr/local/bin/prepare.sh /entrypoint.sh
chmod +x /usr/local/bin/backup.sh /usr/local/bin/prepare.sh /usr/local/bin/apply.sh /entrypoint.sh

ENTRYPOINT ["/entrypoint.sh"]
18 changes: 3 additions & 15 deletions xbackup/apply.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,8 @@
# It will copy/move the prepared backup in data dir of mysql
##❣️ Don't forget to run ./prepare.sh prior running this file

sudo systemctl stop mysql && sudo mv /var/lib/mysql/ /tmp/
. /etc/environments.sh

sudo mkdir /var/lib/mysql && sudo xtrabackup --copy-back --target-dir=/xbackup/recovery/final
mv "$DB_DATA_DIR" /xbackup/tmp

sudo chown -R mysql:mysql /var/lib/mysql
sudo find /var/lib/mysql -type d -exec chmod 750 {} \;

if sudo systemctl is-active --quiet mysql; then
echo "MySQL is active. Removing old data directory..."
sudo rm -rf /tmp/mysql/
sudo rm -rf /xbackup/recovery
echo "Old data directory removed successfully."
else
echo "Failed to start MySQL. Old data directory not removed."
fi

echo "><"
mkdir -p "$DB_DATA_DIR" && xtrabackup --copy-back --target-dir=/xbackup/recovery/final --datadir="$DB_DATA_DIR"
9 changes: 3 additions & 6 deletions xbackup/backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -212,16 +212,13 @@ push_to_s3() {
local compress_backup_file_name="${curr_hr}${COMPRESS_EXT}" # curr_hr.tar.zst || curr_hr_inc.tar.zst
local final_dest="${TARGET_DIR}/${compress_backup_file_name}" # /xbackup/curr_hr.tar.zst || /xbackup/curr_hr_inc.tar.zst

echo "Compressing backup..."
tar -cf - -C "${_final_dir}" . | pzstd -o "${final_dest}" -p"${CPUS}" || { echo "Compression failed"; exit 1; }
echo "Compressing and uploading backup..."
tar -cf - -C "${_final_dir}" . | pzstd - -p"${CPUS}" | s3cmd --config=/root/.s3cfg put - s3://"${S3_BUCKET}${final_dest}"

echo "Uploading backup to S3 of $curr_hr hr ... with name $compress_backup_file_name"
s3cmd --config=/root/.s3cfg put "${final_dest}" s3://"${S3_BUCKET}${final_dest}" --encrypt
if [ $? -eq 0 ]; then
echo "Upload complete."
echo "Removing compressed and prepared backup"
echo "Removing prepared backup"
rm -rf "$_final_dir" # remove duplicate backup
rm "$final_dest" # remove compressed backup
echo "$compress_backup_file_name" > "${TARGET_DIR}/${LATEST_BACKUP_TXT}" # write it to a file, later we'll need it to fetch latest backup
else
echo "Upload failed."
Expand Down
34 changes: 1 addition & 33 deletions xbackup/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,37 +28,5 @@ fi

run_cmd sudo systemctl is-active --quiet docker && echo "Docker is running" || echo "Docker is not running :("


if [ -f .env ]; then
echo "Loading environment variables from .env file"
set -o allexport
source .env
set +o allexport
else
echo ".env file not found!"
exit 1
fi

required_vars=(DB_HOST DB_DATA_DIR DB_NAME DB_USER DB_PASS CRON_TIME S3_BUCKET S3_REGION S3_ENDPOINT S3_ACCESS_KEY S3_SECRET_KEY S3_ENCRYPTION_KEY S3_UPLOAD_CHUNK_SIZE GPG_PATH ROLLBACK_WINDOW_HR XBACKUP_VOLUME)

for var in "${required_vars[@]}"; do
if [ -z "${!var}" ]; then
echo $var
echo "Error: Environment variable $var is not set!"
exit 1
fi
done

get_abs_path() {
echo "$(cd "$(dirname "$1")"; pwd)/$(basename "$1")"
}

if [[ "${DB_DATA_DIR}" != /* ]]; then
export DB_DATA_DIR=$(get_abs_path "${DB_DATA_DIR}")
fi

if [[ "${XBACKUP_VOLUME}" != /* ]]; then
export XBACKUP_VOLUME=$(get_abs_path "${XBACKUP_VOLUME}")
fi

chmod +x ./load_env.sh && . ./load_env.sh
run_cmd sudo docker compose up --build -d
32 changes: 32 additions & 0 deletions xbackup/load_env.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env bash

if [ -f .env ]; then
echo "Loading environment variables from .env file"
set -o allexport
source .env
set +o allexport
else
echo ".env file not found!"
exit 1
fi

required_vars=(DB_HOST DB_DATA_DIR DB_NAME DB_USER DB_PASS CRON_TIME S3_BUCKET S3_REGION S3_ENDPOINT S3_ACCESS_KEY S3_SECRET_KEY S3_ENCRYPTION_KEY S3_UPLOAD_CHUNK_SIZE GPG_PATH ROLLBACK_WINDOW_HR XBACKUP_VOLUME)

for var in "${required_vars[@]}"; do
if [ -z "${!var}" ]; then
echo "Error: Environment variable $var is not set!"
exit 1
fi
done

get_abs_path() {
echo "$(cd "$(dirname "$1")"; pwd)/$(basename "$1")"
}

if [[ "${DB_DATA_DIR}" != /* ]]; then
export DB_DATA_DIR=$(get_abs_path "${DB_DATA_DIR}")
fi

if [[ "${XBACKUP_VOLUME}" != /* ]]; then
export XBACKUP_VOLUME=$(get_abs_path "${XBACKUP_VOLUME}")
fi
9 changes: 4 additions & 5 deletions xbackup/prepare.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,23 @@

. /etc/environments.sh

S3_TARGET_DIR="xbackup"
DEST_DIR="/xbackup"
S3_TARGET_DIR="/xbackup"
TARGET_DIR="/xbackup/recovery"
COMPRESS_EXT=".tar.zst"
LATEST_BACKUP_TXT="latest_backup.txt"

extract_backup() {
local filename="$1"

cd "$DEST_DIR" || { echo "Failed to change directory to $DEST_DIR"; exit 1; }
cd "$S3_TARGET_DIR" || { echo "Failed to change directory to $S3_TARGET_DIR"; exit 1; }

if [ -z "$filename" ]; then # if filename arg is not there, read from the latest_backup.txt
if [ ! -f "$LATEST_BACKUP_TXT" ]; then
echo "Error: File $LATEST_BACKUP_TXT does not exist."
exit 1
fi

last_successful_backup=$(cat "${DEST_DIR}/${LATEST_BACKUP_TXT}")
last_successful_backup=$(cat "${LATEST_BACKUP_TXT}")
filename=$(basename "$last_successful_backup" "${COMPRESS_EXT}")
if [[ "$filename" =~ ^([0-9]{2}) ]]; then
filename="${BASH_REMATCH[1]}"
Expand All @@ -34,7 +33,7 @@ extract_backup() {

mkdir -p "$TARGET_DIR" || { echo "Failed to create target directory"; exit 1; }

s3cmd get "s3://$S3_BUCKET/$S3_TARGET_DIR/$filename" "$TARGET_DIR/" || { echo "Failed to download from S3"; exit 1; }
s3cmd get "s3://${S3_BUCKET}${S3_TARGET_DIR}/$filename" "$TARGET_DIR/" || { echo "Failed to download from S3"; exit 1; }

local downloaded_file="$TARGET_DIR/$filename"
if [ ! -f "$downloaded_file" ]; then
Expand Down
27 changes: 22 additions & 5 deletions xbackup/xbackup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,34 @@ perform_backup() {
}

prepare_backup() {
sudo docker exec xbackup /usr/local/bin/prepare.sh
sudo docker exec xbackup-percona-xtrabackup-1 /usr/local/bin/prepare.sh
}

apply_backup() {
sudo chmod _x ./apply.sh
sudo ./apply.sh
chmod +x ./load_env.sh && . ./load_env.sh
sudo systemctl stop mysql && sudo docker exec xbackup-percona-xtrabackup-1 /usr/local/bin/apply.sh && \
chown -R mysql:mysql "$DB_DATA_DIR" && \
find "$DB_DATA_DIR" -type d -exec chmod 750 {} \; && \
sudo systemctl start mysql

for i in {1..10}; do
if sudo systemctl is-active --quiet mysql; then
echo "MySQL is active. Removing old data directory..."
sudo docker exec xbackup-percona-xtrabackup-1 rm -rf /xbackup/tmp/ # old mysql data
sudo docker exec xbackup-percona-xtrabackup-1 rm -rf /xbackup/recovery # data downloaded from s3
echo "Old data directory removed successfully."
break
else
echo "MySQL not active, sleeping for $i seconds"
sleep "$i"
fi
done

echo "><"
}

show_logs() {
sudo docker compose logs
sudo docker logs -f xbackup-percona-xtrabackup-1
}

stop() {
Expand Down Expand Up @@ -50,4 +68,3 @@ case "$1" in
exit 1
;;
esac

0 comments on commit 429a781

Please sign in to comment.