Compare commits

...

3 Commits

Author SHA1 Message Date
8860c2eb13
add readme 2025-04-03 08:25:12 -05:00
ff55c5e35e
add new env vars and error handling 2025-04-03 08:25:08 -05:00
20b9100624
add crontab file 2025-04-03 08:24:53 -05:00
4 changed files with 126 additions and 9 deletions

View File

@ -1,14 +1,14 @@
#!/bin/bash #!/bin/bash
echo "Beginning Hot-Backup of database..." echo "Beginning Hot-Backup of database..." >> /var/log/cron.log
# Check if MYSQL_HOST is defined # Check if MYSQL_HOST is defined
if [ -z "$MYSQL_HOST" ]; then if [ -z "$MYSQL_HOST" ]; then
echo "Error: MYSQL_HOST is not defined." echo "Error: MYSQL_HOST is not defined." >> /var/log/cron.log
exit 1 exit 1
fi fi
# Check if MYSQL_ROOT_PASS is defined # Check if MYSQL_ROOT_PASS is defined
if [ -z "$MYSQL_ROOT_PASS" ]; then if [ -z "$MYSQL_ROOT_PASSWORD" ]; then
echo "Error: MYSQL_ROOT_PASS is not defined." echo "Error: MYSQL_ROOT_PASS is not defined." >> /var/log/cron.log
exit 1 exit 1
fi fi
# Check if BACKUP_NAME is defined # Check if BACKUP_NAME is defined
@ -24,4 +24,5 @@ else
FILENAME="$BACKUP_NAME.sql" FILENAME="$BACKUP_NAME.sql"
fi fi
mysqldump -u root --password=$MYSQL_ROOT_PASS --host=$MYSQL_HOST --all-databases > "/backup/$FILENAME.sql" mysqldump -u root --password=$MYSQL_ROOT_PASSWORD --host=$MYSQL_HOST --all-databases > "/backup/$FILENAME"
echo "MySQL backup complete" >> /var/log/cron.log

View File

@ -1,4 +1,10 @@
FROM mysql:latest FROM mysql:8-debian
COPY backup.sh /usr/local/bin/ RUN apt-get update --allow-unauthenticated --allow-insecure-repositories
RUN chmod +x /usr/loca/bin/backup.sh RUN apt-get -y install -qq --force-yes cron
CMD ["cron","-f"] RUN echo "Log Start" >> /var/log/cron.log
RUN mkdir /backup
ADD backup.sh /usr/local/bin/
ADD mysqlCron /etc/cron.d/mysqlCron
RUN chmod 0644 /etc/cron.d/mysqlCron
RUN chmod 0744 /usr/local/bin/backup.sh
CMD cron && tail -f /var/log/cron.log

1
mysqlCron Normal file
View File

@ -0,0 +1 @@
50 23 * * * backup.sh >> /var/log/cron.log 2>&1

109
readme.md Normal file
View File

@ -0,0 +1,109 @@
# MySQL Automatic Backup
A sidecar container for backing up MySQL databases.
Every night at 23:50, internal cron runs `mysqldump` to dump all databases on the target server to a SQL file.
## Changing when the job runs
[mysqlCron](mysqlCron) defines when the job will be run. To change this, simply modify the values. Use the chart below as reference.
By default, the job is confirued to run at 23:50 every night.
> [!IMPORTANT]
> The crontab file (mysqlCron) must always contain an empty line at the end of the file.
```
50 23 * * * backup.sh >> /var/log/cron.log 2>&1
- - - - -
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)
```
# Usage
Simply start a container using this image.
The service needs to have network access to the target server on a network that can log into the target with a root password.
## Install
It's recommended to set up this container by adding it to the target service's docker compose.
### Examples
#### Simple Setup
```
version: "3"
services:
db:
image: mysql:latest
container_name: nasdb
restart: always
env_file: stack.env
volumes:
- "db:/var/lib/mysql"
networks:
- nastest
backup:
image: gitea.jv.com/audrey/mysql-backup-agent:test
env_file: stack.env
environment:
MYSQL_HOST: nasdb
BACKUP_NAME: nastest
APPEND_DATE: true
networks:
- nastest
volumes:
db:
driver: local
backup:
driver: local
networks:
nastest:
external: false
```
#### Backup to NAS
For increased data security, it's recommended to store database backups on the NAS where they can take advantage of increased storage, automatic snapshots, & further backups.
> [!IMPORTANT]
> If you intend to keep regular snapshots of this backup on the file server, you should set APPEND_DATE to false
```
version: "3"
services:
db:
image: mysql:latest
container_name: nasdb
restart: always
env_file: stack.env
volumes:
- "db:/var/lib/mysql"
networks:
- nastest
backup:
image: gitea.jv.com/audrey/mysql-backup-agent:test
env_file: stack.env
environment:
MYSQL_HOST: nasdb
BACKUP_NAME: nastest
APPEND_DATE: false
networks:
- nastest
volumes:
db:
driver: local
backup:
driver: local
driver_opts: #config to connect to an NFS share is defined using these options
type: nfs
o: addr=172.16.1.2,rw #If a direct link is available, use that
device: :/mnt/tank/Docker/Volumes/Intranet #this will usually be the path to the folder shared by NFS, relative to the file server.
networks:
nastest:
external: false
```
## Config
Some basic config options are available through environment variables.
### Required Vars
| Variable | Description | Example |
| :--- | :--- | :--- |
| MYSQL_HOST | The IP/identifier of the target server. Typically, the container name or ID | `MYSQL_HOST=websitedb` |
| MYSQL_ROOT_PASSWORD | The root password of the target server. Used for authentication | `MYSQL_ROOT_PASSWORD=someval` |
#### Optional Vars
|Variable|Description|Example|
| :--- | :--- | :---|
|APPEND_DATE|`Default: false` If set, appends the yyyy-mm-dd of the backup to the end of the file name. Leaving this unset will cause existing backups to be overwritten.| `APPEND_DATE=true`|
|BACKUP_NAME|`Default: Value of MYSQL_HOST`If set, the name of the file will be set to this value. Example: setting this to 'epicdb' will cause files to save as 'epicdb.sql'|`BACKUP_NAME='pubweb-backup'`|