mirror of
https://github.com/jkaninda/mysql-bkup.git
synced 2025-12-07 22:19:42 +01:00
Integret s3 backup
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -2,3 +2,4 @@
|
|||||||
backup
|
backup
|
||||||
data
|
data
|
||||||
compose.yaml
|
compose.yaml
|
||||||
|
.env
|
||||||
51
README.md
51
README.md
@@ -11,6 +11,18 @@ MySQL Backup docker container image
|
|||||||
## Storage:
|
## Storage:
|
||||||
- local
|
- local
|
||||||
- s3
|
- s3
|
||||||
|
- Object storage
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
| Options | Shorts | Usage |
|
||||||
|
|---------------|--------|------------------------------------|
|
||||||
|
| --operation | -o | Set operation (default: backup) |
|
||||||
|
| --destination | -d | Set destination (default: local) |
|
||||||
|
| --source | -s | Set source (default: local) |
|
||||||
|
| --file | -f | Set file name for restoration |
|
||||||
|
| --timeout | -t | Set timeout (default: 120s) |
|
||||||
|
| --help | -h | Print this help message and exit |
|
||||||
|
| --version | -V | Print version information and exit |
|
||||||
|
|
||||||
## Backup database :
|
## Backup database :
|
||||||
```yaml
|
```yaml
|
||||||
@@ -30,7 +42,7 @@ services:
|
|||||||
command:
|
command:
|
||||||
- /bin/sh
|
- /bin/sh
|
||||||
- -c
|
- -c
|
||||||
- backup
|
- bkup --operation backup
|
||||||
volumes:
|
volumes:
|
||||||
- ./backup:/backup
|
- ./backup:/backup
|
||||||
environment:
|
environment:
|
||||||
@@ -55,11 +67,14 @@ services:
|
|||||||
mysql-bkup:
|
mysql-bkup:
|
||||||
image: jkaninda/mysql-bkup:latest
|
image: jkaninda/mysql-bkup:latest
|
||||||
container_name: mysql-bkup
|
container_name: mysql-bkup
|
||||||
command: ["restore"]
|
command:
|
||||||
|
- /bin/sh
|
||||||
|
- -c
|
||||||
|
- bkup --operation restore -file database_20231217_115621.sql
|
||||||
volumes:
|
volumes:
|
||||||
- ./backup:/backup
|
- ./backup:/backup
|
||||||
environment:
|
environment:
|
||||||
- FILE_NAME=mariadb_20231217_040238.sql
|
#- FILE_NAME=mariadb_20231217_040238.sql # Optional if file name is set from command
|
||||||
- DB_PORT=3306
|
- DB_PORT=3306
|
||||||
- DB_HOST=mariadb
|
- DB_HOST=mariadb
|
||||||
- DB_DATABASE=mariadb
|
- DB_DATABASE=mariadb
|
||||||
@@ -90,7 +105,7 @@ spec:
|
|||||||
command:
|
command:
|
||||||
- /bin/sh
|
- /bin/sh
|
||||||
- -c
|
- -c
|
||||||
- backup;
|
- bkup --operation backup
|
||||||
env:
|
env:
|
||||||
- name: DB_PORT
|
- name: DB_PORT
|
||||||
value: "3306"
|
value: "3306"
|
||||||
@@ -105,3 +120,31 @@ spec:
|
|||||||
value: "password"
|
value: "password"
|
||||||
restartPolicy: Never
|
restartPolicy: Never
|
||||||
```
|
```
|
||||||
|
## Backup to S3
|
||||||
|
Simple backup usage
|
||||||
|
```yaml
|
||||||
|
mysql-bkup:
|
||||||
|
image: jkaninda/mysql-bkup:latest
|
||||||
|
container_name: mysql-bkup
|
||||||
|
tty: true
|
||||||
|
privileged: true
|
||||||
|
devices:
|
||||||
|
- "/dev/fuse"
|
||||||
|
command:
|
||||||
|
- /bin/sh
|
||||||
|
- -c
|
||||||
|
- mysql_bkup --operation restore -d s3 -f database_20231217_115621.sql
|
||||||
|
volumes:
|
||||||
|
- ./backup:/backup
|
||||||
|
environment:
|
||||||
|
- DB_PORT=3306
|
||||||
|
- DB_HOST=mysql
|
||||||
|
- DB_DATABASE=bkup
|
||||||
|
- DB_USERNAME=jonas
|
||||||
|
- DB_PASSWORD=password
|
||||||
|
- ACCESS_KEY=${ACCESS_KEY}
|
||||||
|
- SECRET_KEY=${SECRET_KEY}
|
||||||
|
- BUCKETNAME=${BUCKETNAME}
|
||||||
|
- S3_ENDPOINT=${S3_ENDPOINT}
|
||||||
|
|
||||||
|
```
|
||||||
@@ -8,23 +8,19 @@ services:
|
|||||||
MYSQL_USER: mariadb
|
MYSQL_USER: mariadb
|
||||||
MYSQL_PASSWORD: password
|
MYSQL_PASSWORD: password
|
||||||
MYSQL_ROOT_PASSWORD: password
|
MYSQL_ROOT_PASSWORD: password
|
||||||
networks:
|
|
||||||
- web
|
|
||||||
mysql-bkup:
|
mysql-bkup:
|
||||||
image: jkaninda/mysql-bkup:latest
|
image: jkaninda/mysql-bkup:latest
|
||||||
container_name: mysql-bkup
|
container_name: mysql-bkup
|
||||||
command:
|
command:
|
||||||
- /bin/sh
|
- /bin/sh
|
||||||
- -c
|
- -c
|
||||||
- backup
|
- bkup --operation backup
|
||||||
volumes:
|
volumes:
|
||||||
- ./backup:/backup
|
- ./backup:/backup
|
||||||
environment:
|
environment:
|
||||||
- FILE_NAME=napata_20231217_051339.sql
|
#- FILE_NAME=mariadb_20231217_040238.sql # Optional if file name is set from command
|
||||||
- DB_PORT=3306
|
- DB_PORT=3306
|
||||||
- DB_HOST=mysql
|
- DB_HOST=mariadb
|
||||||
- DB_DATABASE=bkup
|
- DB_DATABASE=mariadb
|
||||||
- DB_USERNAME=jonas
|
- DB_USERNAME=mariadb
|
||||||
- DB_PASSWORD=password
|
- DB_PASSWORD=password
|
||||||
networks:
|
|
||||||
- web
|
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
#!/bin/sh
|
|
||||||
TIME=$(date +%Y%m%d_%H%M%S)
|
|
||||||
MY_SQL_DUMP=/usr/bin/mysqldump
|
|
||||||
#OPTION=${OPTION}
|
|
||||||
set -e
|
|
||||||
if [ -z "${DB_HOST}"] || [ -z "${DB_DATABASE}"] || [ -z "${DB_USERNAME}"] || [ -z "${DB_PASSWORD}"]; then
|
|
||||||
echo "Please make sure all environment variables are set "
|
|
||||||
else
|
|
||||||
## Backup database
|
|
||||||
mysqldump -h ${DB_HOST} -P ${DB_PORT} -u ${DB_USERNAME} --password=${DB_PASSWORD} ${DB_DATABASE} > /backup/${DB_DATABASE}_${TIME}.sql
|
|
||||||
echo "Database has been saved"
|
|
||||||
fi
|
|
||||||
exit
|
|
||||||
@@ -4,7 +4,8 @@ ENV DB_DATABASE=""
|
|||||||
ENV DB_USERNAME=""
|
ENV DB_USERNAME=""
|
||||||
ENV DB_PASSWORD=""
|
ENV DB_PASSWORD=""
|
||||||
ENV DB_PORT="3306"
|
ENV DB_PORT="3306"
|
||||||
ENV STORAGE=local
|
ENV DESTINATION=local
|
||||||
|
ENV SOURCE=local
|
||||||
ENV BUCKETNAME=""
|
ENV BUCKETNAME=""
|
||||||
ENV ACCESS_KEY=""
|
ENV ACCESS_KEY=""
|
||||||
ENV SECRET_KEY=""
|
ENV SECRET_KEY=""
|
||||||
@@ -22,13 +23,11 @@ RUN mkdir /tmp/s3cache
|
|||||||
RUN chmod 777 /s3mnt
|
RUN chmod 777 /s3mnt
|
||||||
RUN chmod 777 /tmp/s3cache
|
RUN chmod 777 /tmp/s3cache
|
||||||
|
|
||||||
COPY src/backup.sh /usr/local/bin/
|
COPY src/mysql_bkup.sh /usr/local/bin/
|
||||||
COPY src/restore.sh /usr/local/bin/
|
RUN chmod +x /usr/local/bin/mysql_bkup.sh
|
||||||
RUN chmod +x /usr/local/bin/backup.sh
|
|
||||||
RUN chmod +x /usr/local/bin/restore.sh
|
|
||||||
|
|
||||||
RUN ln -s /usr/local/bin/backup.sh /usr/local/bin/backup
|
RUN ln -s /usr/local/bin/mysql_bkup.sh /usr/local/bin/mysql_bkup
|
||||||
RUN ln -s /usr/local/bin/restore.sh /usr/local/bin/restore
|
RUN ln -s /usr/local/bin/mysql_bkup.sh /usr/local/bin/bkup
|
||||||
|
|
||||||
RUN mkdir /backup
|
RUN mkdir /backup
|
||||||
WORKDIR /backup
|
WORKDIR /backup
|
||||||
@@ -1,29 +0,0 @@
|
|||||||
#!/bin/sh
|
|
||||||
TIME=$(date +%Y%m%d_%H%M%S)
|
|
||||||
MY_SQL_DUMP=/usr/bin/mysqldump
|
|
||||||
set -e
|
|
||||||
|
|
||||||
if [ -z "${DB_HOST}"] || [ -z "${DB_DATABASE}"] || [ -z "${DB_USERNAME}"] || [ -z "${DB_PASSWORD}"]; then
|
|
||||||
echo "Please make sure all environment variables are set "
|
|
||||||
else
|
|
||||||
if [ $OPTION != 'backup' ]
|
|
||||||
then
|
|
||||||
## Restore databas
|
|
||||||
echo "Restoring database..."
|
|
||||||
if [ -f "/backup/$FILE_NAME" ]; then
|
|
||||||
cat /backup/${FILE_NAME} | mysql -h ${DB_HOST} -P ${DB_PORT} -u ${DB_USERNAME} --password=${DB_PASSWORD} ${DB_DATABASE}
|
|
||||||
echo "Database has been restored"
|
|
||||||
|
|
||||||
else
|
|
||||||
echo "Error, file not found in /backup folder"
|
|
||||||
fi
|
|
||||||
else
|
|
||||||
## Backup database
|
|
||||||
echo "Start backup database..."
|
|
||||||
mysqldump -h ${DB_HOST} -P ${DB_PORT} -u ${DB_USERNAME} --password=${DB_PASSWORD} ${DB_DATABASE} > /backup/${DB_DATABASE}_${TIME}.sql
|
|
||||||
echo "Database has been saved"
|
|
||||||
|
|
||||||
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
bash
|
|
||||||
171
src/mysql_bkup.sh
Executable file
171
src/mysql_bkup.sh
Executable file
@@ -0,0 +1,171 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
set -e
|
||||||
|
TIME=$(date +%Y%m%d_%H%M%S)
|
||||||
|
MY_SQL_DUMP=/usr/bin/mysqldump
|
||||||
|
arg0=$(basename "$0" .sh)
|
||||||
|
blnk=$(echo "$arg0" | sed 's/./ /g')
|
||||||
|
export OPERATION=backup
|
||||||
|
export DESTINATION=local
|
||||||
|
export DESTINATION_DIR=/backup
|
||||||
|
export SOURCE=local
|
||||||
|
usage_info()
|
||||||
|
{
|
||||||
|
echo "Usage: \\"
|
||||||
|
echo " $blnk Backup: mysql_bkup -o backup -d s3 \\"
|
||||||
|
echo " $blnk Restore: mysql_bkup -o restore -s s3 -f my_db.sql \\"
|
||||||
|
echo " $blnk [-o|--operation] [{-d|--destination} ] [{-f|--file} ] [{-s|--source} ] [{-h|--help} ] \\"
|
||||||
|
|
||||||
|
}
|
||||||
|
version_info()
|
||||||
|
{
|
||||||
|
echo "Version: 1.0"
|
||||||
|
exit 0
|
||||||
|
}
|
||||||
|
usage()
|
||||||
|
{
|
||||||
|
exec 1>2 # Send standard output to standard error
|
||||||
|
usage_info
|
||||||
|
exit 0
|
||||||
|
}
|
||||||
|
|
||||||
|
error()
|
||||||
|
{
|
||||||
|
echo "$arg0: $*" >&2
|
||||||
|
exit 0
|
||||||
|
}
|
||||||
|
|
||||||
|
help()
|
||||||
|
{
|
||||||
|
echo
|
||||||
|
echo " -o|--operation -- Set operation (default: backup)"
|
||||||
|
echo " -d|--destination -- Set destination (default: local)"
|
||||||
|
echo " -s|--source -- Set source (default: local)"
|
||||||
|
echo " -s|--file -- Set file name "
|
||||||
|
echo " -t|--timeout -- Set timeout (default: 120s)"
|
||||||
|
echo " -h|--help -- Print this help message and exit"
|
||||||
|
echo " -v|--version -- Print version information and exit"
|
||||||
|
exit 0
|
||||||
|
}
|
||||||
|
|
||||||
|
flags()
|
||||||
|
{
|
||||||
|
while test $# -gt 0
|
||||||
|
do
|
||||||
|
case "$1" in
|
||||||
|
(-o|--operation)
|
||||||
|
shift
|
||||||
|
[ $# = 0 ] && error "No operation specified - restore or backup"
|
||||||
|
export OPERATION="$1"
|
||||||
|
shift;;
|
||||||
|
(-d|--destination)
|
||||||
|
shift
|
||||||
|
[ $# = 0 ] && error "No destination specified - local or s3 | default local"
|
||||||
|
export DESTINATION="$1"
|
||||||
|
export SOURCE="$1"
|
||||||
|
shift;;
|
||||||
|
(-s|--source)
|
||||||
|
shift
|
||||||
|
[ $# = 0 ] && error "No source specified - local or s3 | default local"
|
||||||
|
export SOURCE="$1"
|
||||||
|
export DESTINATION="$1"
|
||||||
|
shift;;
|
||||||
|
(-f|--file)
|
||||||
|
shift
|
||||||
|
[ $# = 0 ] && error "No file specified - file to restore"
|
||||||
|
export FILE_NAME="$1"
|
||||||
|
shift;;
|
||||||
|
(-t|--timeout)
|
||||||
|
shift
|
||||||
|
[ $# = 0 ] && error "No timeout specified"
|
||||||
|
export TIMEOUT="$1"
|
||||||
|
shift;;
|
||||||
|
(-h|--help)
|
||||||
|
help;;
|
||||||
|
(-V|--version)
|
||||||
|
version_info;;
|
||||||
|
(--)
|
||||||
|
help;;
|
||||||
|
(*) usage;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
|
backup()
|
||||||
|
{
|
||||||
|
if [ -z "${DB_HOST}"] || [ -z "${DB_DATABASE}"] || [ -z "${DB_USERNAME}"] || [ -z "${DB_PASSWORD}"]; then
|
||||||
|
echo "Please make sure all required options are set "
|
||||||
|
else
|
||||||
|
## Backup database
|
||||||
|
mysqldump -h ${DB_HOST} -P ${DB_PORT} -u ${DB_USERNAME} --password=${DB_PASSWORD} ${DB_DATABASE} > ${DESTINATION_DIR}/${DB_DATABASE}_${TIME}.sql
|
||||||
|
echo "Database has been saved"
|
||||||
|
fi
|
||||||
|
exit
|
||||||
|
}
|
||||||
|
|
||||||
|
restore()
|
||||||
|
{
|
||||||
|
if [ -z "${DB_HOST}" ] || [ -z "${DB_DATABASE}" ] || [ -z "${DB_USERNAME}" ] || [ -z "${DB_PASSWORD}" ]; then
|
||||||
|
echo "Please make sure all required options are set "
|
||||||
|
else
|
||||||
|
## Restore database
|
||||||
|
if [ -f "${DESTINATION_DIR}/$FILE_NAME" ]; then
|
||||||
|
cat ${DESTINATION_DIR}/${FILE_NAME} | mysql -h ${DB_HOST} -P ${DB_PORT} -u ${DB_USERNAME} --password=${DB_PASSWORD} ${DB_DATABASE}
|
||||||
|
echo "Database has been restored"
|
||||||
|
else
|
||||||
|
echo "Error, file not found in /backup folder"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
exit
|
||||||
|
}
|
||||||
|
|
||||||
|
s3_backup()
|
||||||
|
{
|
||||||
|
echo "S3 Backup"
|
||||||
|
mount_s3
|
||||||
|
backup
|
||||||
|
}
|
||||||
|
|
||||||
|
s3_restore()
|
||||||
|
{
|
||||||
|
echo "S3 Restore"
|
||||||
|
mount_s3
|
||||||
|
restore
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
mount_s3()
|
||||||
|
{
|
||||||
|
if [ -z "${ACCESS_KEY}"] || [ -z "${SECRET_KEY}"]; then
|
||||||
|
echo "Please make sure all environment variables are set "
|
||||||
|
echo "BUCKETNAME=$BUCKETNAME \nACCESS_KEY=$nACCESS_KEY \nSECRET_KEY=$SECRET_KEY"
|
||||||
|
else
|
||||||
|
echo "$ACCESS_KEY:$SECRET_KEY" | tee /etc/passwd-s3fs
|
||||||
|
chmod 600 /etc/passwd-s3fs
|
||||||
|
echo "Mounting Object storage in /s3mnt .... "
|
||||||
|
s3fs $BUCKETNAME /s3mnt -o passwd_file=/etc/passwd-s3fs -o use_cache=/tmp/s3cache -o allow_other -o url=$S3_ENDPOINT -o use_path_request_style
|
||||||
|
ls /s3mnt | wc -l
|
||||||
|
export DESTINATION_DIR=/s3mnt
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
flags "$@"
|
||||||
|
# ?
|
||||||
|
if [ $OPERATION != 'backup' ]
|
||||||
|
then
|
||||||
|
if [ $DESTINATION != 's3' ]
|
||||||
|
then
|
||||||
|
echo "Restore from local"
|
||||||
|
restore
|
||||||
|
else
|
||||||
|
echo "Restore from s3"
|
||||||
|
s3_restore
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
if [ $DESTINATION != 's3' ]
|
||||||
|
then
|
||||||
|
echo "Backup to local destination"
|
||||||
|
backup
|
||||||
|
else
|
||||||
|
echo "Restore from s3"
|
||||||
|
s3_backup
|
||||||
|
fi
|
||||||
|
fi
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
#!/bin/sh
|
|
||||||
TIME=$(date +%Y%m%d_%H%M%S)
|
|
||||||
MY_SQL_DUMP=/usr/bin/mysqldump
|
|
||||||
set -e
|
|
||||||
if [ -z "${DB_HOST}"] || [ -z "${DB_DATABASE}"] || [ -z "${DB_USERNAME}"] || [ -z "${DB_PASSWORD}"]; then
|
|
||||||
echo "Please make sure all environment variables are set "
|
|
||||||
else
|
|
||||||
## Restore database
|
|
||||||
if [ -f "/backup/$FILE_NAME" ]; then
|
|
||||||
cat /backup/${FILE_NAME} | mysql -h ${DB_HOST} -P ${DB_PORT} -u ${DB_USERNAME} --password=${DB_PASSWORD} ${DB_DATABASE}
|
|
||||||
echo "Database has been restored"
|
|
||||||
else
|
|
||||||
echo "Error, file not found in /backup folder"
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
exit
|
|
||||||
Reference in New Issue
Block a user