Skip to content

Commit

Permalink
add mysql support
Browse files Browse the repository at this point in the history
  • Loading branch information
Thiritin committed May 31, 2024
1 parent 7022a6d commit 6354a32
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 22 deletions.
Empty file modified .github/workflows/docker.yml
100644 → 100755
Empty file.
Empty file modified .gitignore
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion Dockerfile
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ RUN apt-get update \
&& echo "deb http://repo.mongodb.org/apt/debian bullseye/mongodb-org/6.0 main" | tee /etc/apt/sources.list.d/mongodb-org-6.0.list \
&& echo "deb http://download.proxmox.com/debian/pbs-client bullseye main" | tee /etc/apt/sources.list.d/pbs.list \
&& apt-get update \
&& apt-get --no-install-recommends install postgresql-client-13 mongodb-org-tools proxmox-backup-client -y \
&& apt-get --no-install-recommends install postgresql-client-13 mongodb-org-tools mariadb-client proxmox-backup-client -y \
&& apt-get remove wget gnupg -y \
&& apt-get autoremove -y \
&& apt-get clean
Expand Down
Empty file modified LICENSE.md
100644 → 100755
Empty file.
42 changes: 24 additions & 18 deletions README.md
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,25 @@ This Docker image contains a backup script to export data from several sources i

The script uses the following environment variables:

| Variable name | Purpose |
| --- | --- |
| `PBS_REPOSITORY` | The repository to store the backup. |
| `PBS_PASSWORD` | The password for the backup repository. |
| `PBS_ENCRYPTION_PASSWORD` | The encryption password for the backup. |
| `PBS_FINGERPRINT` | The fingerprint of the backup repository. |
| `PGHOST` | The hostname or IP address of the PostgreSQL server. |
| `PGPORT` | The port number on which the PostgreSQL server is listening. |
| `PGDATABASE` | The name of the PostgreSQL database. |
| `PGPASSWORD` | The password for connecting to the PostgreSQL server. |
| `PGUSER` | The username for connecting to the PostgreSQL server. |
| Variable name | Purpose |
| --- |--------------------------------------------------------------------|
| `PBS_REPOSITORY` | The repository to store the backup. |
| `PBS_PASSWORD` | The password for the backup repository. |
| `PBS_ENCRYPTION_PASSWORD` | The encryption password for the backup. |
| `PBS_FINGERPRINT` | The fingerprint of the backup repository. |
| `PGHOST` | The hostname or IP address of the PostgreSQL server. |
| `PGPORT` | The port number on which the PostgreSQL server is listening. |
| `PGDATABASE` | The name of the PostgreSQL database. |
| `PGPASSWORD` | The password for connecting to the PostgreSQL server. |
| `PGUSER` | The username for connecting to the PostgreSQL server. |
| `PGDBSELECT` | The SQL command used to select the PostgreSQL databases to backup. |
| `S3_ENDPOINT` | The endpoint of the S3-compatible storage. |
| `S3_ACCESS` | The access key for the S3-compatible storage. |
| `S3_SECRET` | The secret key for the S3-compatible storage. |
| `MONGO_URL` | The URL for the MongoDB server. |
| `BACKUP_ID` | The ID for the backup job. |
| `BACKUP_SOURCE` | The source for the backup job (e.g. PostgreSQL, MongoDB). |
| `PBC_NAMESPACE` | The namespace for the backup job.
| `S3_ENDPOINT` | The endpoint of the S3-compatible storage. |
| `S3_ACCESS` | The access key for the S3-compatible storage. |
| `S3_SECRET` | The secret key for the S3-compatible storage. |
| `MONGO_URL` | The URL for the MongoDB server. |
| `BACKUP_ID` | The ID for the backup job. |
| `BACKUP_SOURCE` | The source for the backup job (e.g. PostgreSQL, MongoDB, MySQL). |
| `PBC_NAMESPACE` | The namespace for the backup job.

### Supported Backup Sources

Expand Down Expand Up @@ -54,6 +54,12 @@ docker run -d
-e PGPASSWORD="mypgpassword"
-e PGUSER="mypguser"
-e PGDBSELECT="SELECT datname FROM pg_database WHERE datistemplate = false;"
-e MYSQL_HOST="mysql.example.com"
-e MYSQL_PORT="3306"
-e MYSQL_DATABASE="mydatabase"
-e MYSQL_USER="myuser"
-e MYSQL_PASSWORD="mypassword"
-e MYSQL_DBSELECT="SHOW DATABASES;"
-e S3_ENDPOINT="https://s3.mybackup.com/"
-e S3_ACCESS="mys3accesskey"
-e S3_SECRET="mys3secretkey"
Expand Down
41 changes: 38 additions & 3 deletions backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,52 @@ case $BACKUP_SOURCE in
# database is not empty
if [ -n "$database" ]; then
# Use the pg_dump tool to create a backup of the database
pg_dump -Fd -v -d "$database" -j 4 -Z0 -f "./backup/$database"
pg_dump -Fd -v -d "$database" -j 4 -Z0 -f "/app/backup/$database"
fi
done
;;
MYSQL_MULTI)
# echo mysql result
mysql --host=$MYSQL_HOST --user=$MYSQL_USER --password="$MYSQL_PASSWORD" -s -N -e "$MYSQL_DBSELECT" | while read -r database; do
# Skip information_schema, performance_schema, and sys databases
if [ "$database" = "information_schema" ] || [ "$database" = "performance_schema" ] || [ "$database" = "sys" ]; then
continue
fi
echo "Backing up $database"
# database is not empty
if [ -n "$database" ]; then
# Use the mysqldump tool to create a backup of the database
mysqldump --host=$MYSQL_HOST --user=$MYSQL_USER --single-transaction --password="$MYSQL_PASSWORD" --quick --lock-tables=false "$database" > "/app/backup/$database.sql"
# Check that the file exists, otherwise throw error
if [ ! -f "/app/backup/$database.sql" ]; then
echo "Backup failed for $database"
exit 1
fi
fi
done
## if no database is found, exit with error
if [ ! "$(ls -A /app/backup)" ]; then
echo "No databases found"
exit 1
fi
;;
MYSQL_SINGLE)
echo "Backing up $MYSQL_DATABASE"
# Use the mysqldump tool to create a backup of the database
mysqldump --host=$MYSQL_HOST --user=$MYSQL_USER --single-transaction --password="$MYSQL_PASSWORD" --quick --lock-tables=false "$MYSQL_DATABASE" > "/app/backup/$MYSQL_DATABASE.sql"
# Check that the file exists, otherwise throw error
if [ ! -f "/app/backup/$MYSQL_DATABASE.sql" ]; then
echo "Backup failed for $MYSQL_DATABASE"
exit 1
fi
;;
MINIO_MULTI)
# Use the minio client (mc) to create a backup of the bucket
# shellcheck disable=SC2086
mc alias set crewzone $S3_ENDPOINT "$S3_ACCESS" $S3_SECRET --api S3v4
# mirror each s3 bucket to a local directory
mc ls crewzone | awk '{print $5}' | while read bucket; do
mc mirror --overwrite crewzone/$bucket ./backup/$bucket
mc mirror --overwrite crewzone/$bucket /app/backup/$bucket
done
;;
MONGO_SINGLE)
Expand All @@ -62,4 +97,4 @@ if [ -n "$status_url" ]; then
fi

# Clean up the backup file
rm -rf ./backup/*
rm -rf /app/backup/*

0 comments on commit 6354a32

Please sign in to comment.