From ae36fb2338d670ce6c01ba5b7f12c0b67f3adf0a Mon Sep 17 00:00:00 2001 From: sahil-sagwekar2652 Date: Tue, 14 Nov 2023 13:50:12 +0530 Subject: [PATCH 1/4] add backup and restore --- db/Dockerfile | 15 +++++++++++++++ db/backup.sh | 12 ++++++++++++ db/bro.cnf | 3 +++ db/restore.sh | 9 +++++++++ docker-compose.yml-cron-backup | 20 ++++++++++++++++++++ 5 files changed, 59 insertions(+) create mode 100644 db/Dockerfile create mode 100755 db/backup.sh create mode 100644 db/bro.cnf create mode 100644 db/restore.sh create mode 100644 docker-compose.yml-cron-backup diff --git a/db/Dockerfile b/db/Dockerfile new file mode 100644 index 0000000..4e88d4c --- /dev/null +++ b/db/Dockerfile @@ -0,0 +1,15 @@ +FROM mariadb:10.5.12 + +ENV MYSQL_ROOT_PASSWORD=secret +ENV MYSQL_USER=username +ENV MYSQL_PASSWORD=password +ENV MYSQL_DATABASE=wordpress + +COPY backup.sh /usr/local/bin/backup.sh +COPY restore.sh /usr/local/bin/restore.sh + +RUN chmod +x /usr/local/bin/backup.sh +RUN chmod +x /usr/local/bin/restore.sh +RUN /usr/local/bin/restore.sh + +RUN echo "0 2 * * * /usr/local/bin/backup.sh" | crontab - diff --git a/db/backup.sh b/db/backup.sh new file mode 100755 index 0000000..fc46b5c --- /dev/null +++ b/db/backup.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +# Define variables +DB_USER="username" +DB_NAME="wordpress" + +# Set the current date for the backup file +DATE=$(date +"%Y%m%d_%H%M%S") +BACKUP_FILE="/etc/mysql/conf.d/backups/${DB_NAME}_backup_$DATE.sql" + +# Run mysqldump command +mysqldump --defaults-extra-file=/etc/mysql/conf.d/custom-my.cnf "$DB_NAME" > "$BACKUP_FILE" diff --git a/db/bro.cnf b/db/bro.cnf new file mode 100644 index 0000000..1f1738b --- /dev/null +++ b/db/bro.cnf @@ -0,0 +1,3 @@ +[mysqldump] +user=django +password=password diff --git a/db/restore.sh b/db/restore.sh new file mode 100644 index 0000000..92aecab --- /dev/null +++ b/db/restore.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +directory="/etc/mysql/conf.d/backups" + +if [ -z "$(ls -A "$directory")" ]; then + echo "No Backups found!" +else + mysql --defaults-extra-file=/etc/mysql/conf.d/custom-my.cnf wordpress < $(ls -t1 /etc/mysql/conf.d/backups | head -n 1) +fi diff --git a/docker-compose.yml-cron-backup b/docker-compose.yml-cron-backup new file mode 100644 index 0000000..6b123bf --- /dev/null +++ b/docker-compose.yml-cron-backup @@ -0,0 +1,20 @@ +version: "2" +services: + data: + build: data + + db: + build: db + volumes_from: + - data + volumes: + - ~/dockerfiles/etc/mysql/conf.d:/etc/mysql/conf.d + + wp: + image: wordpress:5.8.1-php8.0-apache + ports: + - "80:80" + links: + - db:mysql + volumes: + - ~/dockerfiles/wp-content:/var/www/html/wp-content From 26af71a2e66f9e5afb316ce7386f10d1bdce9d67 Mon Sep 17 00:00:00 2001 From: sahil-sagwekar2652 Date: Tue, 14 Nov 2023 17:52:17 +0530 Subject: [PATCH 2/4] working backup and restore --- db/Dockerfile | 5 ++++- db/backup.sh | 3 +-- db/bro.cnf | 8 ++++++-- db/restore.sh | 6 +++--- 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/db/Dockerfile b/db/Dockerfile index 4e88d4c..25d1d83 100644 --- a/db/Dockerfile +++ b/db/Dockerfile @@ -7,9 +7,12 @@ ENV MYSQL_DATABASE=wordpress COPY backup.sh /usr/local/bin/backup.sh COPY restore.sh /usr/local/bin/restore.sh +COPY bro.cnf /home + +# Install cron +RUN apt-get update && apt-get install -y cron RUN chmod +x /usr/local/bin/backup.sh RUN chmod +x /usr/local/bin/restore.sh -RUN /usr/local/bin/restore.sh RUN echo "0 2 * * * /usr/local/bin/backup.sh" | crontab - diff --git a/db/backup.sh b/db/backup.sh index fc46b5c..9dbdb98 100755 --- a/db/backup.sh +++ b/db/backup.sh @@ -1,7 +1,6 @@ #!/bin/bash # Define variables -DB_USER="username" DB_NAME="wordpress" # Set the current date for the backup file @@ -9,4 +8,4 @@ DATE=$(date +"%Y%m%d_%H%M%S") BACKUP_FILE="/etc/mysql/conf.d/backups/${DB_NAME}_backup_$DATE.sql" # Run mysqldump command -mysqldump --defaults-extra-file=/etc/mysql/conf.d/custom-my.cnf "$DB_NAME" > "$BACKUP_FILE" +mysqldump --defaults-extra-file=/home/bro.cnf "$DB_NAME" > "$BACKUP_FILE" diff --git a/db/bro.cnf b/db/bro.cnf index 1f1738b..2a2d1fe 100644 --- a/db/bro.cnf +++ b/db/bro.cnf @@ -1,3 +1,7 @@ -[mysqldump] -user=django +[mysqldump] # NEEDED FOR DUMP +user=username +password=password + +[mysql] # NEEDED FOR RESTORE +user=username password=password diff --git a/db/restore.sh b/db/restore.sh index 92aecab..4048c60 100644 --- a/db/restore.sh +++ b/db/restore.sh @@ -1,9 +1,9 @@ #!/bin/bash -directory="/etc/mysql/conf.d/backups" +backup=$(ls -t1 /etc/mysql/conf.d/backups | head -n 1) -if [ -z "$(ls -A "$directory")" ]; then +if [ -z ${backup} ]; then echo "No Backups found!" else - mysql --defaults-extra-file=/etc/mysql/conf.d/custom-my.cnf wordpress < $(ls -t1 /etc/mysql/conf.d/backups | head -n 1) + mysql --defaults-extra-file=/home/bro.cnf wordpress < /etc/mysql/conf.d/backups/${backup} fi From b9bd8e134926e3c0cc8721665a3fcda5de00b269 Mon Sep 17 00:00:00 2001 From: sahil-sagwekar2652 Date: Wed, 29 Nov 2023 17:44:50 +0530 Subject: [PATCH 3/4] add physical backup --- db/Dockerfile | 7 ++++--- db/backup.sh | 10 ++++++---- db/bro.cnf | 4 ++++ db/dump.sh | 11 +++++++++++ docker-compose.yml | 21 +++++++++++++++++++++ 5 files changed, 46 insertions(+), 7 deletions(-) create mode 100755 db/dump.sh diff --git a/db/Dockerfile b/db/Dockerfile index 25d1d83..3d34690 100644 --- a/db/Dockerfile +++ b/db/Dockerfile @@ -5,14 +5,15 @@ ENV MYSQL_USER=username ENV MYSQL_PASSWORD=password ENV MYSQL_DATABASE=wordpress -COPY backup.sh /usr/local/bin/backup.sh +COPY dump.sh /usr/local/bin/dump.sh COPY restore.sh /usr/local/bin/restore.sh COPY bro.cnf /home # Install cron RUN apt-get update && apt-get install -y cron -RUN chmod +x /usr/local/bin/backup.sh +RUN chmod +x /usr/local/bin/dump.sh RUN chmod +x /usr/local/bin/restore.sh -RUN echo "0 2 * * * /usr/local/bin/backup.sh" | crontab - +RUN echo "0 2 * * * /usr/local/bin/dump.sh" | crontab - +RUN echo "30 3 * * 6 /usr/local/bin/backup.sh" | crontab - diff --git a/db/backup.sh b/db/backup.sh index 9dbdb98..261cac0 100755 --- a/db/backup.sh +++ b/db/backup.sh @@ -3,9 +3,11 @@ # Define variables DB_NAME="wordpress" -# Set the current date for the backup file +# Set the current date for the dump file DATE=$(date +"%Y%m%d_%H%M%S") -BACKUP_FILE="/etc/mysql/conf.d/backups/${DB_NAME}_backup_$DATE.sql" +DUMP_FILE="/etc/mysql/conf.d/backup/${DB_NAME}_backup_$DATE.sql" -# Run mysqldump command -mysqldump --defaults-extra-file=/home/bro.cnf "$DB_NAME" > "$BACKUP_FILE" +# Run mysqlbackup command +mariabackup --defaults-extra-file=/home/bro.cnf \ + --backup \ + --target-dir=/etc/mysql/conf.d/backup/ diff --git a/db/bro.cnf b/db/bro.cnf index 2a2d1fe..b408fe2 100644 --- a/db/bro.cnf +++ b/db/bro.cnf @@ -5,3 +5,7 @@ password=password [mysql] # NEEDED FOR RESTORE user=username password=password + +[mariabackup] +user=root +password=secret diff --git a/db/dump.sh b/db/dump.sh new file mode 100755 index 0000000..2613f80 --- /dev/null +++ b/db/dump.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +# Define variables +DB_NAME="wordpress" + +# Set the current date for the dump file +DATE=$(date +"%Y%m%d_%H%M%S") +DUMP_FILE="/etc/mysql/conf.d/dumps/${DB_NAME}_dump_$DATE.sql" + +# Run mysqldump command +mysqldump --defaults-extra-file=/home/bro.cnf "$DB_NAME" > "$DUMP_FILE" diff --git a/docker-compose.yml b/docker-compose.yml index 7ddc208..7fdaed5 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,3 +3,24 @@ # and compose your stack by running # docker compose up -d ################################################################################ + +version: "2" +services: + data: + build: data + + db: + build: db + volumes_from: + - data + volumes: + - ~/dockerfiles/etc/mysql/conf.d:/etc/mysql/conf.d + + wp: + image: wordpress:5.8.1-php8.0-apache + ports: + - "80:80" + links: + - db:mysql + volumes: + - ~/dockerfiles/wp-content:/var/www/html/wp-content From dedd33a406b47342e7bbd707087297e19bd779cd Mon Sep 17 00:00:00 2001 From: sahil-sagwekar2652 Date: Wed, 29 Nov 2023 17:57:54 +0530 Subject: [PATCH 4/4] cleanup backup.sh --- db/backup.sh | 7 ------- 1 file changed, 7 deletions(-) diff --git a/db/backup.sh b/db/backup.sh index 261cac0..51c7f84 100755 --- a/db/backup.sh +++ b/db/backup.sh @@ -1,12 +1,5 @@ #!/bin/bash -# Define variables -DB_NAME="wordpress" - -# Set the current date for the dump file -DATE=$(date +"%Y%m%d_%H%M%S") -DUMP_FILE="/etc/mysql/conf.d/backup/${DB_NAME}_backup_$DATE.sql" - # Run mysqlbackup command mariabackup --defaults-extra-file=/home/bro.cnf \ --backup \