mirror of
https://github.com/jkaninda/mysql-bkup.git
synced 2025-12-06 13:39:41 +01:00
Compare commits
74 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 2fa7e50485 | |||
| f53c68cd5c | |||
| 902695032c | |||
| 620801cb99 | |||
| e19643ebcb | |||
| c87201d08d | |||
| bf566af4f8 | |||
| 99ea4e18e5 | |||
| 748416af9b | |||
| bce9512d6a | |||
| 8eff38bdb1 | |||
| c12a6751ae | |||
| 05eda4213a | |||
| a919b161c8 | |||
| 4b07a78f29 | |||
| 4a0ad39d70 | |||
| 0be493d51f | |||
| cdb4b2017a | |||
| 09ba8f8981 | |||
| 2ae78fec57 | |||
| 2cd74167de | |||
| aaad8a010c | |||
| a086921242 | |||
| 3525a90b93 | |||
| 40f2a2c99d | |||
| 3537532d5f | |||
| b98191f586 | |||
| 502767bb12 | |||
| b3b4248fa0 | |||
| 8de463ad38 | |||
| 2f0375dee3 | |||
| 313e4c9525 | |||
| 164d8eda77 | |||
| 80923885c1 | |||
| 078d57fc0d | |||
| abd04c0a37 | |||
| 8b7d1576a4 | |||
| 452d77f5ee | |||
| 4630df0dd6 | |||
| 5d97ec0a4d | |||
| d9a86ca053 | |||
| 380ea59e95 | |||
| af037f195c | |||
| 45d397dfc8 | |||
| 72db75b9fb | |||
| 4b1501c095 | |||
| 2823848fd1 | |||
| d587c18e75 | |||
| 508ca68366 | |||
| a113b40126 | |||
| 1c2a9fccc6 | |||
| c3f17b3d85 | |||
| 2a8ad3a6e2 | |||
| cc15452ccf | |||
| bcfc69e7f9 | |||
| f3f859ae05 | |||
| 947b9fa888 | |||
| 142a2a2dc3 | |||
| 548ba17bbb | |||
| b245fe1a11 | |||
| 0355be59e1 | |||
| f5bed16951 | |||
| 25a32823b7 | |||
| 498f8ee545 | |||
| ae01fb0edb | |||
| 1f797e019d | |||
| 3ddc00dcbd | |||
| 3d4a245181 | |||
| df5ec79d85 | |||
| 53f7a791fa | |||
| 31d450e218 | |||
| f21f726464 | |||
| f1e116a38b | |||
| 5dde6feace |
4
.github/workflows/build.yml
vendored
4
.github/workflows/build.yml
vendored
@@ -32,8 +32,8 @@ jobs:
|
||||
uses: docker/build-push-action@v3
|
||||
with:
|
||||
push: true
|
||||
file: "./src/docker/Dockerfile"
|
||||
file: "./docker/Dockerfile"
|
||||
platforms: linux/amd64,linux/arm64
|
||||
tags: |
|
||||
"${{env.BUILDKIT_IMAGE}}:v0.6"
|
||||
"${{env.BUILDKIT_IMAGE}}:latest"
|
||||
"${{env.BUILDKIT_IMAGE}}:v0.3"
|
||||
|
||||
5
.gitignore
vendored
5
.gitignore
vendored
@@ -4,4 +4,7 @@ data
|
||||
compose.yaml
|
||||
.env
|
||||
test.md
|
||||
.DS_Store
|
||||
.DS_Store
|
||||
mysql-bkup
|
||||
/.DS_Store
|
||||
/.idea
|
||||
21
LICENSE
Normal file
21
LICENSE
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2023 Jonas Kaninda
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
144
README.md
144
README.md
@@ -1,10 +1,17 @@
|
||||
# MySQL Backup
|
||||
MySQL Backup tool, backup database to S3 or Object Storage
|
||||
MySQL Backup and Restoration tool. Backup database to AWS S3 storage or any S3 Alternatives for Object Storage.
|
||||
|
||||
[](https://github.com/jkaninda/mysql-bkup/actions/workflows/build.yml)
|
||||
[](https://goreportcard.com/report/github.com/jkaninda/mysql-bkup)
|
||||

|
||||

|
||||
|
||||
<p align="center">
|
||||
<a href="https://github.com/jkaninda/mysql-bkup">
|
||||
<img src="https://www.mysql.com/common/logos/logo-mysql-170x115.png" alt="Logo">
|
||||
</a>
|
||||
</p>
|
||||
|
||||
> Runs on:
|
||||
- Docker
|
||||
- Kubernetes
|
||||
@@ -23,42 +30,89 @@ MySQL Backup tool, backup database to S3 or Object Storage
|
||||
- local
|
||||
- s3
|
||||
- Object storage
|
||||
|
||||
## Volumes:
|
||||
|
||||
- /s3mnt => S3 mounting path
|
||||
- /backup => local storage mounting path
|
||||
|
||||
## Usage
|
||||
|
||||
| Options | Shorts | Usage |
|
||||
|---------------|--------|------------------------------------|
|
||||
| mysql_bkup | bkup | CLI utility |
|
||||
| --operation | -o | Set operation. backup or restore (default: backup) |
|
||||
| --storage | -s | Set storage. local or s3 (default: local) |
|
||||
| --file | -f | Set file name for restoration |
|
||||
| --path | | Set s3 path without file name. eg: /custom_path |
|
||||
| --dbname | -d | Set database name |
|
||||
| --port | -p | Set database port (default: 3306) |
|
||||
| --mode | -m | Set execution mode. default or scheduled (default: default) |
|
||||
| --period | | Set crontab period for scheduled mode only. (default: "0 1 * * *") |
|
||||
| --timeout | -t | Set timeout (default: 60s) |
|
||||
| --help | -h | Print this help message and exit |
|
||||
| --version | -V | Print version information and exit |
|
||||
| Options | Shorts | Usage |
|
||||
|-----------------------|--------|--------------------------------------------------------------------|
|
||||
| mysql-bkup | bkup | CLI utility |
|
||||
| backup | | Backup database operation |
|
||||
| restore | | Restore database operation |
|
||||
| history | | Show the history of backup |
|
||||
| --storage | -s | Set storage. local or s3 (default: local) |
|
||||
| --file | -f | Set file name for restoration |
|
||||
| --path | | Set s3 path without file name. eg: /custom_path |
|
||||
| --dbname | -d | Set database name |
|
||||
| --port | -p | Set database port (default: 3306) |
|
||||
| --mode | -m | Set execution mode. default or scheduled (default: default) |
|
||||
| --disable-compression | | Disable database backup compression |
|
||||
| --period | | Set crontab period for scheduled mode only. (default: "0 1 * * *") |
|
||||
| --timeout | -t | Set timeout (default: 60s) |
|
||||
| --help | -h | Print this help message and exit |
|
||||
| --version | -V | Print version information and exit |
|
||||
|
||||
|
||||
## Environment variables
|
||||
|
||||
| Name | Requirement | Description |
|
||||
|-------------|--------------------------------------------------|----------------------|
|
||||
| DB_PORT | Optional, default 3306 | Database port number |
|
||||
| DB_HOST | Required | Database host |
|
||||
| DB_NAME | Optional if it was provided from the -d flag | Database name |
|
||||
| DB_USERNAME | Required | Database user name |
|
||||
| DB_PASSWORD | Required | Database password |
|
||||
| ACCESS_KEY | Optional, required for S3 storage | AWS S3 Access Key |
|
||||
| SECRET_KEY | Optional, required for S3 storage | AWS S3 Secret Key |
|
||||
| BUCKET_NAME | Optional, required for S3 storage | AWS S3 Bucket Name |
|
||||
| S3_ENDPOINT | Optional, required for S3 storage | AWS S3 Endpoint |
|
||||
| FILE_NAME | Optional if it was provided from the --file flag | File to restore |
|
||||
|
||||
## Note:
|
||||
|
||||
Creating a user for backup tasks who has read-only access is recommended!
|
||||
|
||||
> create read-only user
|
||||
|
||||
```sh
|
||||
mysql -u root -p
|
||||
```
|
||||
|
||||
```sql
|
||||
CREATE USER read_only_user IDENTIFIED BY 'your_strong_password';
|
||||
|
||||
```
|
||||
```sql
|
||||
GRANT SELECT, SHOW VIEW ON *.* TO read_only_user;
|
||||
```
|
||||
```sql
|
||||
FLUSH PRIVILEGES;
|
||||
|
||||
```
|
||||
|
||||
## Backup database :
|
||||
|
||||
Simple backup usage
|
||||
|
||||
```sh
|
||||
bkup --operation backup --dbname database_name
|
||||
mysql-bkup backup --dbname database_name
|
||||
```
|
||||
```sh
|
||||
bkup -o backup -d database_name
|
||||
mysql-bkup backup -d database_name
|
||||
```
|
||||
### S3
|
||||
|
||||
```sh
|
||||
bkup --operation backup --storage s3 --dbname database_name
|
||||
mysql-bkup backup --storage s3 --dbname database_name
|
||||
```
|
||||
## Docker run:
|
||||
|
||||
```sh
|
||||
docker run --rm --network your_network_name --name mysql-bkup -v $PWD/backup:/backup/ -e "DB_HOST=database_host_name" -e "DB_USERNAME=username" -e "DB_PASSWORD=password" jkaninda/mysql-bkup:latest bkup -o backup -d database_name
|
||||
docker run --rm --network your_network_name --name mysql-bkup -v $PWD/backup:/backup/ -e "DB_HOST=database_host_name" -e "DB_USERNAME=username" -e "DB_PASSWORD=password" jkaninda/mysql-bkup:latest mysql-bkup backup -d database_name
|
||||
```
|
||||
|
||||
## Docker compose file:
|
||||
@@ -79,7 +133,7 @@ services:
|
||||
command:
|
||||
- /bin/sh
|
||||
- -c
|
||||
- bkup --operation backup -d database_name
|
||||
- mysql-bkup backup -d database_name
|
||||
volumes:
|
||||
- ./backup:/backup
|
||||
environment:
|
||||
@@ -93,22 +147,22 @@ services:
|
||||
Simple database restore operation usage
|
||||
|
||||
```sh
|
||||
bkup --operation restore --dbname database_name --file database_20231217_115621.sql
|
||||
mysql-bkup restore --dbname database_name --file database_20231217_115621.sql
|
||||
```
|
||||
|
||||
```sh
|
||||
bkup -o restore -f database_20231217_115621.sql
|
||||
mysql-bkup restore -f database_20231217_115621.sql
|
||||
```
|
||||
### S3
|
||||
|
||||
```sh
|
||||
bkup --operation restore --storage s3 --file database_20231217_115621.sql
|
||||
mysql-bkup restore --storage s3 --file database_20231217_115621.sql
|
||||
```
|
||||
|
||||
## Docker run:
|
||||
|
||||
```sh
|
||||
docker run --rm --network your_network_name --name mysql-bkup -v $PWD/backup:/backup/ -e "DB_HOST=database_host_name" -e "DB_USERNAME=username" -e "DB_PASSWORD=password" jkaninda/mysql-bkup bkup -o backup -d database_name -f mydb_20231219_022941.sql.gz
|
||||
docker run --rm --network your_network_name --name mysql-bkup -v $PWD/backup:/backup/ -e "DB_HOST=database_host_name" -e "DB_USERNAME=username" -e "DB_PASSWORD=password" jkaninda/mysql-bkup mysql-bkup backup -d database_name -f db_20231219_022941.sql.gz
|
||||
```
|
||||
|
||||
## Docker compose file:
|
||||
@@ -130,7 +184,7 @@ services:
|
||||
command:
|
||||
- /bin/sh
|
||||
- -c
|
||||
- bkup --operation restore --file database_20231217_115621.sql --dbname database_name
|
||||
- mysql-bkup restore --file database_20231217_115621.sql --dbname database_name
|
||||
volumes:
|
||||
- ./backup:/backup
|
||||
environment:
|
||||
@@ -149,14 +203,14 @@ docker-compose up -d
|
||||
## Backup to S3
|
||||
|
||||
```sh
|
||||
docker run --rm --privileged --device /dev/fuse --name mysql-bkup -e "DB_HOST=db_hostname" -e "DB_USERNAME=username" -e "DB_PASSWORD=password" -e "ACCESS_KEY=your_access_key" -e "SECRET_KEY=your_secret_key" -e "BUCKETNAME=your_bucket_name" -e "S3_ENDPOINT=https://eu2.contabostorage.com" jkaninda/mysql-bkup bkup -o backup -s s3 -d database_name
|
||||
docker run --rm --privileged --device /dev/fuse --name mysql-bkup -e "DB_HOST=db_hostname" -e "DB_USERNAME=username" -e "DB_PASSWORD=password" -e "ACCESS_KEY=your_access_key" -e "SECRET_KEY=your_secret_key" -e "BUCKETNAME=your_bucket_name" -e "S3_ENDPOINT=https://s3.us-west-2.amazonaws.com" jkaninda/mysql-bkup mysql-bkup backup -s s3 -d database_name
|
||||
```
|
||||
> To change s3 backup path add this flag : --path /myPath . default path is /mysql_bkup
|
||||
|
||||
Simple S3 backup usage
|
||||
|
||||
```sh
|
||||
bkup --operation backup --storage s3 --dbname mydatabase
|
||||
bkup backup --storage s3 --dbname mydatabase
|
||||
```
|
||||
```yaml
|
||||
version: '3'
|
||||
@@ -170,7 +224,7 @@ services:
|
||||
command:
|
||||
- /bin/sh
|
||||
- -c
|
||||
- mysql_bkup --operation restore --storage s3 -f database_20231217_115621.sql.gz
|
||||
- mysql-bkup restore --storage s3 -f database_20231217_115621.sql.gz
|
||||
environment:
|
||||
- DB_PORT=3306
|
||||
- DB_HOST=mysql
|
||||
@@ -179,7 +233,7 @@ services:
|
||||
- DB_PASSWORD=password
|
||||
- ACCESS_KEY=${ACCESS_KEY}
|
||||
- SECRET_KEY=${SECRET_KEY}
|
||||
- BUCKETNAME=${BUCKETNAME}
|
||||
- BUCKET_NAME=${BUCKET_NAME}
|
||||
- S3_ENDPOINT=${S3_ENDPOINT}
|
||||
|
||||
```
|
||||
@@ -237,7 +291,7 @@ Easy to remember format:
|
||||
> Docker run :
|
||||
|
||||
```sh
|
||||
docker run --rm --name mysql-bkup -v $BACKUP_DIR:/backup/ -e "DB_HOST=$DB_HOST" -e "DB_USERNAME=$DB_USERNAME" -e "DB_PASSWORD=$DB_PASSWORD" jkaninda/mysql-bkup bkup --operation backup --dbname $DB_NAME --mode scheduled --period "0 1 * * *"
|
||||
docker run --rm --name mysql-bkup -v $BACKUP_DIR:/backup/ -e "DB_HOST=$DB_HOST" -e "DB_USERNAME=$DB_USERNAME" -e "DB_PASSWORD=$DB_PASSWORD" jkaninda/mysql-bkup mysql-bkup backup --dbname $DB_NAME --mode scheduled --period "0 1 * * *"
|
||||
```
|
||||
|
||||
> With Docker compose
|
||||
@@ -254,7 +308,7 @@ services:
|
||||
command:
|
||||
- /bin/sh
|
||||
- -c
|
||||
- bkup --operation backup --storage s3 --path /mys3_custome_path --dbname database_name --mode scheduled --period "*/30 * * * *"
|
||||
- mysql-bkup backup --storage s3 --path /mys3_custome_path --dbname database_name --mode scheduled --period "*/30 * * * *"
|
||||
environment:
|
||||
- DB_PORT=3306
|
||||
- DB_HOST=mysqlhost
|
||||
@@ -262,7 +316,7 @@ services:
|
||||
- DB_PASSWORD=${DB_PASSWORD}
|
||||
- ACCESS_KEY=${ACCESS_KEY}
|
||||
- SECRET_KEY=${SECRET_KEY}
|
||||
- BUCKETNAME=${BUCKETNAME}
|
||||
- BUCKET_NAME=${BUCKET_NAME}
|
||||
- S3_ENDPOINT=${S3_ENDPOINT}
|
||||
```
|
||||
|
||||
@@ -291,7 +345,7 @@ spec:
|
||||
command:
|
||||
- /bin/sh
|
||||
- -c
|
||||
- bkup -o backup -s s3 --path /custom_path
|
||||
- mysql-bkup backup -s s3 --path /custom_path
|
||||
env:
|
||||
- name: DB_PORT
|
||||
value: "3306"
|
||||
@@ -308,9 +362,27 @@ spec:
|
||||
value: ""
|
||||
- name: SECRET_KEY
|
||||
value: ""
|
||||
- name: BUCKETNAME
|
||||
- name: BUCKET_NAME
|
||||
value: ""
|
||||
- name: S3_ENDPOINT
|
||||
value: "https://s3.amazonaws.com"
|
||||
value: "https://s3.us-west-2.amazonaws.com"
|
||||
restartPolicy: Never
|
||||
```
|
||||
```
|
||||
|
||||
## Contributing
|
||||
|
||||
Contributions are welcome! If you encounter any issues or have suggestions for improvements, please create an issue or submit a pull request.
|
||||
Make sure to follow the existing coding style and provide tests for your changes.
|
||||
|
||||
## License
|
||||
|
||||
This project is licensed under the MIT License. See the LICENSE file for details.
|
||||
|
||||
## Authors
|
||||
|
||||
**Jonas Kaninda**
|
||||
- <https://github.com/jkaninda>
|
||||
|
||||
## Copyright
|
||||
|
||||
Copyright (c) [2023] [Jonas Kaninda]
|
||||
|
||||
7
build.sh
7
build.sh
@@ -6,6 +6,9 @@ if [ $# -eq 0 ]
|
||||
tag=$1
|
||||
fi
|
||||
|
||||
docker build -f src/docker/Dockerfile -t jkaninda/mysql-bkup:$tag .
|
||||
#go build
|
||||
CGO_ENABLED=0 GOOS=linux go build
|
||||
|
||||
docker compose up -d
|
||||
docker build -f docker/Dockerfile -t jkaninda/mysql-bkup:$tag .
|
||||
|
||||
#docker compose up -d --force-recreate
|
||||
28
cmd/backup.go
Normal file
28
cmd/backup.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/jkaninda/mysql-bkup/pkg"
|
||||
"github.com/jkaninda/mysql-bkup/utils"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var BackupCmd = &cobra.Command{
|
||||
Use: "backup ",
|
||||
Short: "Backup database operation",
|
||||
Example: utils.BackupExample,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
if len(args) == 0 {
|
||||
pkg.StartBackup(cmd)
|
||||
} else {
|
||||
utils.Fatal("Error, no argument required")
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
//Backup
|
||||
BackupCmd.PersistentFlags().StringP("mode", "m", "default", "Set execution mode. default or scheduled")
|
||||
BackupCmd.PersistentFlags().StringP("period", "", "0 1 * * *", "Set schedule period time")
|
||||
BackupCmd.PersistentFlags().BoolP("disable-compression", "", false, "Disable backup compression")
|
||||
|
||||
}
|
||||
14
cmd/history.go
Normal file
14
cmd/history.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/jkaninda/mysql-bkup/utils"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var HistoryCmd = &cobra.Command{
|
||||
Use: "history",
|
||||
Short: "Show the history of backup",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
utils.ShowHistory()
|
||||
},
|
||||
}
|
||||
28
cmd/restore.go
Normal file
28
cmd/restore.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/jkaninda/mysql-bkup/pkg"
|
||||
"github.com/jkaninda/mysql-bkup/utils"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var RestoreCmd = &cobra.Command{
|
||||
Use: "restore",
|
||||
Short: "Restore database operation",
|
||||
Example: utils.RestoreExample,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
if len(args) == 0 {
|
||||
pkg.StartRestore(cmd)
|
||||
} else {
|
||||
utils.Fatal("Error, no argument required")
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
//Restore
|
||||
RestoreCmd.PersistentFlags().StringP("file", "f", "", "File name of database")
|
||||
|
||||
}
|
||||
62
cmd/root.go
Normal file
62
cmd/root.go
Normal file
@@ -0,0 +1,62 @@
|
||||
// Package cmd /*
|
||||
/*
|
||||
Copyright © 2024 Jonas Kaninda <jonaskaninda@gmail.com>
|
||||
*/
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/jkaninda/mysql-bkup/utils"
|
||||
"github.com/spf13/cobra"
|
||||
"os"
|
||||
)
|
||||
|
||||
// rootCmd represents the base command when called without any subcommands
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "mysql-bkup [Command]",
|
||||
Short: "MySQL Backup tool, backup database to S3 or Object Storage",
|
||||
Long: `MySQL Database backup and restoration tool. Backup database to AWS S3 storage or any S3 Alternatives for Object Storage.`,
|
||||
Example: utils.MainExample,
|
||||
Version: appVersion,
|
||||
//TODO: To remove
|
||||
//For old user || To remove
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
if operation != "" {
|
||||
if operation == "backup" || operation == "restore" {
|
||||
fmt.Println(utils.Notice)
|
||||
utils.Fatal("New config required, please check --help")
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
var operation = ""
|
||||
var s3Path = "/mysql-bkup"
|
||||
|
||||
// Execute adds all child commands to the root command and sets flags appropriately.
|
||||
// This is called by main.main(). It only needs to happen once to the rootCmd.
|
||||
func Execute() {
|
||||
err := rootCmd.Execute()
|
||||
if err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.PersistentFlags().StringP("storage", "s", "local", "Set storage. local or s3")
|
||||
rootCmd.PersistentFlags().StringP("path", "P", s3Path, "Set s3 path, without file name. for S3 storage only")
|
||||
rootCmd.PersistentFlags().StringP("dbname", "d", "", "Set database name")
|
||||
rootCmd.PersistentFlags().IntP("timeout", "t", 30, "Set timeout")
|
||||
rootCmd.PersistentFlags().IntP("port", "p", 3306, "Set database port")
|
||||
rootCmd.PersistentFlags().StringVarP(&operation, "operation", "o", "", "Set operation, for old version only")
|
||||
|
||||
rootCmd.PersistentFlags().StringP("mode", "m", "default", "Set execution mode. default or scheduled")
|
||||
rootCmd.PersistentFlags().StringP("period", "", "0 1 * * *", "Set schedule period time")
|
||||
rootCmd.PersistentFlags().BoolP("disable-compression", "", false, "Disable backup compression")
|
||||
rootCmd.PersistentFlags().StringP("file", "f", "", "File name of database")
|
||||
|
||||
rootCmd.AddCommand(VersionCmd)
|
||||
rootCmd.AddCommand(BackupCmd)
|
||||
rootCmd.AddCommand(RestoreCmd)
|
||||
rootCmd.AddCommand(S3MountCmd)
|
||||
rootCmd.AddCommand(HistoryCmd)
|
||||
}
|
||||
14
cmd/s3mount.go
Normal file
14
cmd/s3mount.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/jkaninda/mysql-bkup/pkg"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var S3MountCmd = &cobra.Command{
|
||||
Use: "s3mount",
|
||||
Short: "Mount AWS S3 storage",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
pkg.S3Mount()
|
||||
},
|
||||
}
|
||||
26
cmd/version.go
Normal file
26
cmd/version.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package cmd
|
||||
|
||||
/*
|
||||
Copyright © 2024 Jonas Kaninda <jonaskaninda@gmail.com>
|
||||
*/
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/spf13/cobra"
|
||||
"os"
|
||||
)
|
||||
|
||||
var appVersion = os.Getenv("VERSION")
|
||||
|
||||
var VersionCmd = &cobra.Command{
|
||||
Use: "version",
|
||||
Short: "Show version",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
Version()
|
||||
},
|
||||
}
|
||||
|
||||
func Version() {
|
||||
fmt.Printf("Version: %s \n", appVersion)
|
||||
fmt.Println()
|
||||
}
|
||||
50
docker/Dockerfile
Normal file
50
docker/Dockerfile
Normal file
@@ -0,0 +1,50 @@
|
||||
FROM golang:1.21.0 AS build
|
||||
WORKDIR /app
|
||||
|
||||
# Copy the source code.
|
||||
COPY . .
|
||||
# Installs Go dependencies
|
||||
RUN go mod download
|
||||
|
||||
# Build
|
||||
RUN CGO_ENABLED=0 GOOS=linux go build -o /app/mysql-bkup
|
||||
|
||||
FROM ubuntu:24.04
|
||||
ENV DB_HOST=""
|
||||
ENV DB_NAME=""
|
||||
ENV DB_USERNAME=""
|
||||
ENV DB_PASSWORD=""
|
||||
ENV DB_PORT="3306"
|
||||
ENV STORAGE=local
|
||||
ENV BUCKETNAME=""
|
||||
ENV ACCESS_KEY=""
|
||||
ENV SECRET_KEY=""
|
||||
ENV S3_ENDPOINT=https://s3.amazonaws.com
|
||||
ARG DEBIAN_FRONTEND=noninteractive
|
||||
ENV VERSION="v0.6"
|
||||
LABEL authors="Jonas Kaninda"
|
||||
|
||||
RUN apt-get update -qq
|
||||
#RUN apt-get install build-essential libcurl4-openssl-dev libxml2-dev mime-support -y
|
||||
RUN apt install s3fs mysql-client supervisor cron -y
|
||||
|
||||
# Clear cache
|
||||
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
|
||||
|
||||
RUN mkdir /s3mnt
|
||||
RUN mkdir /tmp/s3cache
|
||||
RUN chmod 777 /s3mnt
|
||||
RUN chmod 777 /tmp/s3cache
|
||||
|
||||
COPY --from=build /app/mysql-bkup /usr/local/bin/mysql-bkup
|
||||
RUN chmod +x /usr/local/bin/mysql-bkup
|
||||
|
||||
RUN ln -s /usr/local/bin/mysql-bkup /usr/local/bin/bkup
|
||||
RUN ln -s /usr/local/bin/mysql-bkup /usr/local/bin/mysql_bkup
|
||||
|
||||
|
||||
ADD docker/supervisord.conf /etc/supervisor/supervisord.conf
|
||||
|
||||
|
||||
RUN mkdir /backup
|
||||
WORKDIR /backup
|
||||
21
examples/docker-compose.s3.yaml
Normal file
21
examples/docker-compose.s3.yaml
Normal file
@@ -0,0 +1,21 @@
|
||||
version: "3"
|
||||
services:
|
||||
mysql-bkup:
|
||||
image: jkaninda/mysql-bkup
|
||||
container_name: mysql-bkup
|
||||
privileged: true
|
||||
devices:
|
||||
- "/dev/fuse"
|
||||
command:
|
||||
- /bin/sh
|
||||
- -c
|
||||
- mysql-bkup backup --storage s3 --path /mys3_custom_path --dbname database_name
|
||||
environment:
|
||||
- DB_PORT=3306
|
||||
- DB_HOST=mysqlhost
|
||||
- DB_USERNAME=userName
|
||||
- DB_PASSWORD=${DB_PASSWORD}
|
||||
- ACCESS_KEY=${ACCESS_KEY}
|
||||
- SECRET_KEY=${SECRET_KEY}
|
||||
- BUCKET_NAME=${BUCKET_NAME}
|
||||
- S3_ENDPOINT=https://s3.us-west-2.amazonaws.com
|
||||
16
examples/docker-compose.scheduled.local.yaml
Normal file
16
examples/docker-compose.scheduled.local.yaml
Normal file
@@ -0,0 +1,16 @@
|
||||
version: "3"
|
||||
services:
|
||||
mysql-bkup:
|
||||
image: jkaninda/mysql-bkup
|
||||
container_name: mysql-bkup
|
||||
command:
|
||||
- /bin/sh
|
||||
- -c
|
||||
- mysql-bkup backup --dbname database_name --mode scheduled --period "0 1 * * *"
|
||||
volumes:
|
||||
- ./backup:/backup
|
||||
environment:
|
||||
- DB_PORT=3306
|
||||
- DB_HOST=mysqlhost
|
||||
- DB_USERNAME=userName
|
||||
- DB_PASSWORD=${DB_PASSWORD}
|
||||
21
examples/docker-compose.scheduled.s3.yaml
Normal file
21
examples/docker-compose.scheduled.s3.yaml
Normal file
@@ -0,0 +1,21 @@
|
||||
version: "3"
|
||||
services:
|
||||
mysql-bkup:
|
||||
image: jkaninda/mysql-bkup
|
||||
container_name: mysql-bkup
|
||||
privileged: true
|
||||
devices:
|
||||
- "/dev/fuse"
|
||||
command:
|
||||
- /bin/sh
|
||||
- -c
|
||||
- mysql-bkup backup --storage s3 --path /mys3_custom_path --dbname database_name --mode scheduled --period "0 1 * * *"
|
||||
environment:
|
||||
- DB_PORT=3306
|
||||
- DB_HOST=mysqlhost
|
||||
- DB_USERNAME=userName
|
||||
- DB_PASSWORD=${DB_PASSWORD}
|
||||
- ACCESS_KEY=${ACCESS_KEY}
|
||||
- SECRET_KEY=${SECRET_KEY}
|
||||
- BUCKET_NAME=${BUCKET_NAME}
|
||||
- S3_ENDPOINT=https://s3.us-west-2.amazonaws.com
|
||||
16
examples/docker-compose.yaml
Normal file
16
examples/docker-compose.yaml
Normal file
@@ -0,0 +1,16 @@
|
||||
version: "3"
|
||||
services:
|
||||
mysql-bkup:
|
||||
image: jkaninda/mysql-bkup
|
||||
container_name: mysql-bkup
|
||||
command:
|
||||
- /bin/sh
|
||||
- -c
|
||||
- mysql-bkup backup --dbname database_name
|
||||
volumes:
|
||||
- ./backup:/backup
|
||||
environment:
|
||||
- DB_PORT=3306
|
||||
- DB_HOST=mysqlhost
|
||||
- DB_USERNAME=userName
|
||||
- DB_PASSWORD=${DB_PASSWORD}
|
||||
@@ -16,7 +16,7 @@ spec:
|
||||
command:
|
||||
- /bin/sh
|
||||
- -c
|
||||
- bkup -o backup -s s3 --path /custom_path
|
||||
- mysql-bkup backup --storage s3 --path /custom_path
|
||||
env:
|
||||
- name: DB_PORT
|
||||
value: "3306"
|
||||
@@ -36,5 +36,5 @@ spec:
|
||||
- name: BUCKETNAME
|
||||
value: ""
|
||||
- name: S3_ENDPOINT
|
||||
value: "https://s3.amazonaws.com"
|
||||
value: "https://s3.us-west-2.amazonaws.com"
|
||||
restartPolicy: Never
|
||||
10
go.mod
Normal file
10
go.mod
Normal file
@@ -0,0 +1,10 @@
|
||||
module github.com/jkaninda/mysql-bkup
|
||||
|
||||
go 1.21.0
|
||||
|
||||
require github.com/spf13/pflag v1.0.5
|
||||
|
||||
require (
|
||||
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
||||
github.com/spf13/cobra v1.8.0 // indirect
|
||||
)
|
||||
10
go.sum
Normal file
10
go.sum
Normal file
@@ -0,0 +1,10 @@
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
||||
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
|
||||
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
|
||||
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
|
||||
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
|
||||
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
|
||||
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
16
main.go
Normal file
16
main.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package main
|
||||
|
||||
//main
|
||||
/*****
|
||||
* MySQL Backup & Restore
|
||||
* @author Jonas Kaninda
|
||||
* @license MIT License <https://opensource.org/licenses/MIT>
|
||||
* @link https://github.com/jkaninda/mysql-bkup
|
||||
**/
|
||||
import "github.com/jkaninda/mysql-bkup/cmd"
|
||||
|
||||
func main() {
|
||||
|
||||
cmd.Execute()
|
||||
|
||||
}
|
||||
162
pkg/backup.go
Normal file
162
pkg/backup.go
Normal file
@@ -0,0 +1,162 @@
|
||||
// Package pkg /*
|
||||
/*
|
||||
Copyright © 2024 Jonas Kaninda <jonaskaninda.gmail.com>
|
||||
*/
|
||||
package pkg
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/jkaninda/mysql-bkup/utils"
|
||||
"github.com/spf13/cobra"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"time"
|
||||
)
|
||||
|
||||
func StartBackup(cmd *cobra.Command) {
|
||||
_, _ = cmd.Flags().GetString("operation")
|
||||
|
||||
//Set env
|
||||
utils.SetEnv("STORAGE_PATH", storagePath)
|
||||
utils.GetEnv(cmd, "dbname", "DB_NAME")
|
||||
utils.GetEnv(cmd, "port", "DB_PORT")
|
||||
utils.GetEnv(cmd, "period", "SCHEDULE_PERIOD")
|
||||
|
||||
//Get flag value and set env
|
||||
s3Path = utils.GetEnv(cmd, "path", "S3_PATH")
|
||||
storage = utils.GetEnv(cmd, "storage", "STORAGE")
|
||||
file = utils.GetEnv(cmd, "file", "FILE_NAME")
|
||||
disableCompression, _ = cmd.Flags().GetBool("disable-compression")
|
||||
executionMode, _ = cmd.Flags().GetString("mode")
|
||||
|
||||
if executionMode == "default" {
|
||||
if storage == "s3" {
|
||||
utils.Info("Backup database to s3 storage")
|
||||
s3Backup(disableCompression, s3Path)
|
||||
} else {
|
||||
utils.Info("Backup database to local storage")
|
||||
BackupDatabase(disableCompression)
|
||||
|
||||
}
|
||||
} else if executionMode == "scheduled" {
|
||||
scheduledMode()
|
||||
} else {
|
||||
utils.Fatal("Error, unknown execution mode!")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Run in scheduled mode
|
||||
func scheduledMode() {
|
||||
|
||||
fmt.Println()
|
||||
fmt.Println("**********************************")
|
||||
fmt.Println(" Starting MySQL Bkup... ")
|
||||
fmt.Println("***********************************")
|
||||
utils.Info("Running in Scheduled mode")
|
||||
utils.Info("Log file in /var/log/mysql-bkup.log")
|
||||
utils.Info("Execution period ", os.Getenv("SCHEDULE_PERIOD"))
|
||||
|
||||
//Test database connexion
|
||||
utils.TestDatabaseConnection()
|
||||
|
||||
utils.Info("Creating backup job...")
|
||||
CreateCrontabScript(disableCompression, storage)
|
||||
|
||||
//Start Supervisor
|
||||
supervisordCmd := exec.Command("supervisord", "-c", "/etc/supervisor/supervisord.conf")
|
||||
if err := supervisordCmd.Run(); err != nil {
|
||||
utils.Fatalf("Error starting supervisord: %v\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
// BackupDatabase backup database
|
||||
func BackupDatabase(disableCompression bool) {
|
||||
dbHost = os.Getenv("DB_HOST")
|
||||
dbPassword := os.Getenv("DB_PASSWORD")
|
||||
dbUserName := os.Getenv("DB_USERNAME")
|
||||
dbName = os.Getenv("DB_NAME")
|
||||
dbPort = os.Getenv("DB_PORT")
|
||||
storagePath = os.Getenv("STORAGE_PATH")
|
||||
|
||||
if os.Getenv("DB_HOST") == "" || os.Getenv("DB_NAME") == "" || os.Getenv("DB_USERNAME") == "" || os.Getenv("DB_PASSWORD") == "" {
|
||||
utils.Fatal("Please make sure all required environment variables for database are set")
|
||||
} else {
|
||||
utils.TestDatabaseConnection()
|
||||
// Backup Database database
|
||||
utils.Info("Backing up database...")
|
||||
//Generate file name
|
||||
bkFileName := fmt.Sprintf("%s_%s.sql.gz", dbName, time.Now().Format("20060102_150405"))
|
||||
|
||||
// Verify is compression is disabled
|
||||
if disableCompression {
|
||||
//Generate file name
|
||||
bkFileName = fmt.Sprintf("%s_%s.sql", dbName, time.Now().Format("20060102_150405"))
|
||||
// Execute mysqldump
|
||||
cmd := exec.Command("mysqldump",
|
||||
"-h", dbHost,
|
||||
"-P", dbPort,
|
||||
"-u", dbUserName,
|
||||
"--password="+dbPassword,
|
||||
dbName,
|
||||
)
|
||||
output, err := cmd.Output()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// save output
|
||||
file, err := os.Create(fmt.Sprintf("%s/%s", storagePath, bkFileName))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
_, err = file.Write(output)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
utils.Done("Database has been backed up")
|
||||
|
||||
} else {
|
||||
// Execute mysqldump
|
||||
cmd := exec.Command("mysqldump", "-h", dbHost, "-P", dbPort, "-u", dbUserName, "--password="+dbPassword, dbName)
|
||||
stdout, err := cmd.StdoutPipe()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
gzipCmd := exec.Command("gzip")
|
||||
gzipCmd.Stdin = stdout
|
||||
gzipCmd.Stdout, err = os.Create(fmt.Sprintf("%s/%s", storagePath, bkFileName))
|
||||
gzipCmd.Start()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if err := cmd.Run(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if err := gzipCmd.Wait(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
utils.Done("Database has been backed up")
|
||||
|
||||
}
|
||||
|
||||
historyFile, err := os.OpenFile(fmt.Sprintf("%s/history.txt", storagePath), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer historyFile.Close()
|
||||
if _, err := historyFile.WriteString(bkFileName + "\n"); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func s3Backup(disableCompression bool, s3Path string) {
|
||||
// Backup Database to S3 storage
|
||||
MountS3Storage(s3Path)
|
||||
BackupDatabase(disableCompression)
|
||||
}
|
||||
86
pkg/restore.go
Normal file
86
pkg/restore.go
Normal file
@@ -0,0 +1,86 @@
|
||||
package pkg
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/jkaninda/mysql-bkup/utils"
|
||||
"github.com/spf13/cobra"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func StartRestore(cmd *cobra.Command) {
|
||||
|
||||
//Set env
|
||||
utils.SetEnv("STORAGE_PATH", storagePath)
|
||||
utils.GetEnv(cmd, "dbname", "DB_NAME")
|
||||
utils.GetEnv(cmd, "port", "DB_PORT")
|
||||
|
||||
//Get flag value and set env
|
||||
s3Path = utils.GetEnv(cmd, "path", "S3_PATH")
|
||||
storage = utils.GetEnv(cmd, "storage", "STORAGE")
|
||||
file = utils.GetEnv(cmd, "file", "FILE_NAME")
|
||||
executionMode, _ = cmd.Flags().GetString("mode")
|
||||
|
||||
if storage == "s3" {
|
||||
utils.Info("Restore database from s3")
|
||||
s3Restore(file, s3Path)
|
||||
} else {
|
||||
utils.Info("Restore database from local")
|
||||
RestoreDatabase(file)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// RestoreDatabase restore database
|
||||
func RestoreDatabase(file string) {
|
||||
dbHost = os.Getenv("DB_HOST")
|
||||
dbName = os.Getenv("DB_NAME")
|
||||
dbPort = os.Getenv("DB_PORT")
|
||||
storagePath = os.Getenv("STORAGE_PATH")
|
||||
if file == "" {
|
||||
utils.Fatal("Error, file required")
|
||||
}
|
||||
|
||||
if os.Getenv("DB_HOST") == "" || os.Getenv("DB_NAME") == "" || os.Getenv("DB_USERNAME") == "" || os.Getenv("DB_PASSWORD") == "" || file == "" {
|
||||
utils.Fatal("Please make sure all required environment variables are set")
|
||||
} else {
|
||||
|
||||
if utils.FileExists(fmt.Sprintf("%s/%s", storagePath, file)) {
|
||||
utils.TestDatabaseConnection()
|
||||
|
||||
extension := filepath.Ext(fmt.Sprintf("%s/%s", storagePath, file))
|
||||
// Restore from compressed file / .sql.gz
|
||||
if extension == ".gz" {
|
||||
str := "zcat " + fmt.Sprintf("%s/%s", storagePath, file) + " | mysql -h " + os.Getenv("DB_HOST") + " -P " + os.Getenv("DB_PORT") + " -u " + os.Getenv("DB_USERNAME") + " --password=" + os.Getenv("DB_PASSWORD") + " " + os.Getenv("DB_NAME")
|
||||
_, err := exec.Command("bash", "-c", str).Output()
|
||||
if err != nil {
|
||||
utils.Fatal("Error, in restoring the database")
|
||||
}
|
||||
|
||||
utils.Done("Database has been restored")
|
||||
|
||||
} else if extension == ".sql" {
|
||||
//Restore from sql file
|
||||
str := "cat " + fmt.Sprintf("%s/%s", storagePath, file) + " | mysql -h " + os.Getenv("DB_HOST") + " -P " + os.Getenv("DB_PORT") + " -u " + os.Getenv("DB_USERNAME") + " --password=" + os.Getenv("DB_PASSWORD") + " " + os.Getenv("DB_NAME")
|
||||
_, err := exec.Command("bash", "-c", str).Output()
|
||||
if err != nil {
|
||||
utils.Fatal("Error, in restoring the database", err)
|
||||
}
|
||||
|
||||
utils.Done("Database has been restored")
|
||||
} else {
|
||||
utils.Fatal("Unknown file extension ", extension)
|
||||
}
|
||||
|
||||
} else {
|
||||
utils.Fatal("File not found in ", fmt.Sprintf("%s/%s", storagePath, file))
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
func s3Restore(file, s3Path string) {
|
||||
// Restore database from S3
|
||||
MountS3Storage(s3Path)
|
||||
RestoreDatabase(file)
|
||||
}
|
||||
80
pkg/s3fs.go
Normal file
80
pkg/s3fs.go
Normal file
@@ -0,0 +1,80 @@
|
||||
// Package pkg /*
|
||||
/*
|
||||
Copyright © 2024 Jonas Kaninda <jonaskaninda.gmail.com>
|
||||
*/
|
||||
package pkg
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/jkaninda/mysql-bkup/utils"
|
||||
"os"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
var (
|
||||
accessKey = ""
|
||||
secretKey = ""
|
||||
bucketName = ""
|
||||
s3Endpoint = ""
|
||||
)
|
||||
|
||||
func S3Mount() {
|
||||
MountS3Storage(s3Path)
|
||||
}
|
||||
|
||||
// MountS3Storage Mount s3 storage using s3fs
|
||||
func MountS3Storage(s3Path string) {
|
||||
accessKey = os.Getenv("ACCESS_KEY")
|
||||
secretKey = os.Getenv("SECRET_KEY")
|
||||
bucketName = os.Getenv("BUCKET_NAME")
|
||||
if bucketName == "" {
|
||||
bucketName = os.Getenv("BUCKETNAME")
|
||||
}
|
||||
s3Endpoint = os.Getenv("S3_ENDPOINT")
|
||||
|
||||
if accessKey == "" || secretKey == "" || bucketName == "" {
|
||||
utils.Fatal("Please make sure all environment variables are set for S3")
|
||||
} else {
|
||||
storagePath := fmt.Sprintf("%s%s", s3MountPath, s3Path)
|
||||
err := os.Setenv("STORAGE_PATH", storagePath)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
//Write file
|
||||
err = utils.WriteToFile(s3fsPasswdFile, fmt.Sprintf("%s:%s", accessKey, secretKey))
|
||||
if err != nil {
|
||||
utils.Fatal("Error creating file")
|
||||
}
|
||||
//Change file permission
|
||||
utils.ChangePermission(s3fsPasswdFile, 0600)
|
||||
|
||||
//Mount object storage
|
||||
utils.Info("Mounting Object storage in ", s3MountPath)
|
||||
if isEmpty, _ := utils.IsDirEmpty(s3MountPath); isEmpty {
|
||||
cmd := exec.Command("s3fs", bucketName, s3MountPath,
|
||||
"-o", "passwd_file="+s3fsPasswdFile,
|
||||
"-o", "use_cache=/tmp/s3cache",
|
||||
"-o", "allow_other",
|
||||
"-o", "url="+s3Endpoint,
|
||||
"-o", "use_path_request_style",
|
||||
)
|
||||
|
||||
if err := cmd.Run(); err != nil {
|
||||
utils.Fatal("Error mounting Object storage:", err)
|
||||
}
|
||||
|
||||
if err := os.MkdirAll(storagePath, os.ModePerm); err != nil {
|
||||
utils.Fatalf("Error creating directory %v %v", storagePath, err)
|
||||
}
|
||||
|
||||
} else {
|
||||
utils.Info("Object storage already mounted in " + s3MountPath)
|
||||
if err := os.MkdirAll(storagePath, os.ModePerm); err != nil {
|
||||
utils.Fatal("Error creating directory "+storagePath, err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
76
pkg/scripts.go
Normal file
76
pkg/scripts.go
Normal file
@@ -0,0 +1,76 @@
|
||||
package pkg
|
||||
|
||||
// Package pkg /*
|
||||
/*
|
||||
Copyright © 2024 Jonas Kaninda <jonaskaninda.gmail.com>
|
||||
*/
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/jkaninda/mysql-bkup/utils"
|
||||
"os"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
const cronLogFile = "/var/log/mysql-bkup.log"
|
||||
const backupCronFile = "/usr/local/bin/backup_cron.sh"
|
||||
|
||||
func CreateCrontabScript(disableCompression bool, storage string) {
|
||||
//task := "/usr/local/bin/backup_cron.sh"
|
||||
touchCmd := exec.Command("touch", backupCronFile)
|
||||
if err := touchCmd.Run(); err != nil {
|
||||
utils.Fatalf("Error creating file %s: %v\n", backupCronFile, err)
|
||||
}
|
||||
var disableC = ""
|
||||
if disableCompression {
|
||||
disableC = "--disable-compression"
|
||||
}
|
||||
|
||||
var scriptContent string
|
||||
|
||||
if storage == "s3" {
|
||||
scriptContent = fmt.Sprintf(`#!/usr/bin/env bash
|
||||
set -e
|
||||
bkup backup --dbname %s --port %s --storage s3 --path %s %v
|
||||
`, os.Getenv("DB_NAME"), os.Getenv("DB_PORT"), os.Getenv("S3_PATH"), disableC)
|
||||
} else {
|
||||
scriptContent = fmt.Sprintf(`#!/usr/bin/env bash
|
||||
set -e
|
||||
bkup backup --dbname %s --port %s %v
|
||||
`, os.Getenv("DB_NAME"), os.Getenv("DB_PORT"), disableC)
|
||||
}
|
||||
|
||||
if err := utils.WriteToFile(backupCronFile, scriptContent); err != nil {
|
||||
utils.Fatalf("Error writing to %s: %v\n", backupCronFile, err)
|
||||
}
|
||||
|
||||
chmodCmd := exec.Command("chmod", "+x", "/usr/local/bin/backup_cron.sh")
|
||||
if err := chmodCmd.Run(); err != nil {
|
||||
utils.Fatalf("Error changing permissions of %s: %v\n", backupCronFile, err)
|
||||
}
|
||||
|
||||
lnCmd := exec.Command("ln", "-s", "/usr/local/bin/backup_cron.sh", "/usr/local/bin/backup_cron")
|
||||
if err := lnCmd.Run(); err != nil {
|
||||
utils.Fatalf("Error creating symbolic link: %v\n", err)
|
||||
|
||||
}
|
||||
|
||||
cronJob := "/etc/cron.d/backup_cron"
|
||||
touchCronCmd := exec.Command("touch", cronJob)
|
||||
if err := touchCronCmd.Run(); err != nil {
|
||||
utils.Fatalf("Error creating file %s: %v\n", cronJob, err)
|
||||
}
|
||||
|
||||
cronContent := fmt.Sprintf(`%s root exec /bin/bash -c ". /run/supervisord.env; /usr/local/bin/backup_cron.sh >> %s"
|
||||
`, os.Getenv("SCHEDULE_PERIOD"), cronLogFile)
|
||||
|
||||
if err := utils.WriteToFile(cronJob, cronContent); err != nil {
|
||||
utils.Fatalf("Error writing to %s: %v\n", cronJob, err)
|
||||
}
|
||||
utils.ChangePermission("/etc/cron.d/backup_cron", 0644)
|
||||
|
||||
crontabCmd := exec.Command("crontab", "/etc/cron.d/backup_cron")
|
||||
if err := crontabCmd.Run(); err != nil {
|
||||
utils.Fatal("Error updating crontab: ", err)
|
||||
}
|
||||
utils.Info("Starting backup in scheduled mode")
|
||||
}
|
||||
16
pkg/var.go
Normal file
16
pkg/var.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package pkg
|
||||
|
||||
const s3MountPath string = "/s3mnt"
|
||||
const s3fsPasswdFile string = "/etc/passwd-s3fs"
|
||||
|
||||
var (
|
||||
storage = "local"
|
||||
file = ""
|
||||
s3Path = "/mysql-bkup"
|
||||
dbName = ""
|
||||
dbHost = ""
|
||||
dbPort = "3306"
|
||||
executionMode = "default"
|
||||
storagePath = "/backup"
|
||||
disableCompression = false
|
||||
)
|
||||
@@ -5,4 +5,4 @@ DB_HOST='db_hostname'
|
||||
DB_NAME='db_name'
|
||||
BACKUP_DIR="$PWD/backup"
|
||||
|
||||
docker run --rm --name mysql-bkup -v $BACKUP_DIR:/backup/ -e "DB_HOST=$DB_HOST" -e "DB_USERNAME=$DB_USERNAME" -e "DB_PASSWORD=$DB_PASSWORD" jkaninda/mysql-bkup:latest bkup -o backup -db $DB_NAME
|
||||
docker run --rm --name mysql-bkup -v $BACKUP_DIR:/backup/ -e "DB_HOST=$DB_HOST" -e "DB_USERNAME=$DB_USERNAME" -e "DB_PASSWORD=$DB_PASSWORD" jkaninda/mysql-bkup:latest backup -d $DB_NAME
|
||||
@@ -1,36 +0,0 @@
|
||||
FROM ubuntu:24.04
|
||||
ENV DB_HOST=""
|
||||
ENV DB_NAME=""
|
||||
ENV DB_USERNAME=""
|
||||
ENV DB_PASSWORD=""
|
||||
ENV DB_PORT="3306"
|
||||
ENV STORAGE=local
|
||||
ENV BUCKETNAME=""
|
||||
ENV ACCESS_KEY=""
|
||||
ENV SECRET_KEY=""
|
||||
ENV S3_ENDPOINT=https://s3.amazonaws.com
|
||||
ARG DEBIAN_FRONTEND=noninteractive
|
||||
ENV VERSION="v0.3"
|
||||
|
||||
RUN apt-get update -qq
|
||||
RUN apt-get install build-essential libcurl4-openssl-dev libxml2-dev mime-support -y
|
||||
RUN apt install s3fs mysql-client supervisor cron -y
|
||||
|
||||
# Clear cache
|
||||
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
|
||||
|
||||
RUN mkdir /s3mnt
|
||||
RUN mkdir /tmp/s3cache
|
||||
RUN chmod 777 /s3mnt
|
||||
RUN chmod 777 /tmp/s3cache
|
||||
|
||||
COPY src/mysql_bkup.sh /usr/local/bin/
|
||||
RUN chmod +x /usr/local/bin/mysql_bkup.sh
|
||||
|
||||
ADD src/supervisord.conf /etc/supervisor/supervisord.conf
|
||||
|
||||
RUN ln -s /usr/local/bin/mysql_bkup.sh /usr/local/bin/mysql_bkup
|
||||
RUN ln -s /usr/local/bin/mysql_bkup.sh /usr/local/bin/bkup
|
||||
|
||||
RUN mkdir /backup
|
||||
WORKDIR /backup
|
||||
@@ -1,272 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
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 STORAGE=local
|
||||
export STORAGE_PATH=/backup
|
||||
export S3_PATH=/mysql-bkup
|
||||
export TIMEOUT=60
|
||||
export EXECUTION_MODE="default"
|
||||
export SCHEDULE_PERIOD="0 1 * * *"
|
||||
export FILE_COMPRESION=true
|
||||
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] [{-f|--file} ] [{-s|--storage} ] [{-h|--help} ] \\"
|
||||
|
||||
}
|
||||
version_info()
|
||||
{
|
||||
echo "Version: $VERSION"
|
||||
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 " -s |--storage -- Set storage (default: local)"
|
||||
echo " -f |--file -- Set file name "
|
||||
echo " |--path -- Set s3 path, without file name"
|
||||
echo " -d |--dbname -- Set database name "
|
||||
echo " -p |--port -- Set database port (default: 3306)"
|
||||
echo " -m |--mode -- Set execution mode (default: default)"
|
||||
echo " |--period -- Set schedule period time (default: '0 1 * * *')"
|
||||
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|--dbname)
|
||||
shift
|
||||
[ $# = 0 ] && error "No database name specified"
|
||||
export DB_NAME="$1"
|
||||
shift;;
|
||||
(-s|--storage)
|
||||
shift
|
||||
[ $# = 0 ] && error "No storage specified - local or s3 | default local"
|
||||
export STORAGE="$1"
|
||||
shift;;
|
||||
(-f|--file)
|
||||
shift
|
||||
[ $# = 0 ] && error "No file specified - file to restore"
|
||||
export FILE_NAME="$1"
|
||||
shift;;
|
||||
(--path)
|
||||
shift
|
||||
[ $# = 0 ] && error "No s3 path specified - s3 path without file name"
|
||||
export S3_PATH="$1"
|
||||
shift;;
|
||||
(-db|--database)
|
||||
shift
|
||||
[ $# = 0 ] && error "No database name specified"
|
||||
export DB_NAME="$1"
|
||||
shift;;
|
||||
(-p|--port)
|
||||
shift
|
||||
[ $# = 0 ] && error "No database name specified"
|
||||
export DB_PORT="$1"
|
||||
shift;;
|
||||
(-m|--mode)
|
||||
shift
|
||||
[ $# = 0 ] && error "No execution mode specified"
|
||||
export EXECUTION_MODE="$1"
|
||||
shift;;
|
||||
(--period)
|
||||
shift
|
||||
[ $# = 0 ] && error "No schedule period entered"
|
||||
export SCHEDULE_PERIOD="$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_NAME ]] || [[ -z $DB_USERNAME ]] || [[ -z $DB_PASSWORD ]]; then
|
||||
echo "Please make sure all required environment variables are set "
|
||||
else
|
||||
## Test database connection
|
||||
mysql -h ${DB_HOST} -P ${DB_PORT} -u ${DB_USERNAME} --password=${DB_PASSWORD} ${DB_NAME} -e"quit"
|
||||
|
||||
## Backup database
|
||||
mysqldump -h ${DB_HOST} -P ${DB_PORT} -u ${DB_USERNAME} --password=${DB_PASSWORD} ${DB_NAME} | gzip > ${STORAGE_PATH}/${DB_NAME}_${TIME}.sql.gz
|
||||
echo "$TIME: ${DB_NAME}_${TIME}.sql.gz" | tee -a "${STORAGE_PATH}/history.txt"
|
||||
echo "Database has been saved"
|
||||
fi
|
||||
exit 0
|
||||
}
|
||||
|
||||
restore()
|
||||
{
|
||||
if [[ -z $DB_HOST ]] || [[ -z $DB_NAME ]] || [[ -z $DB_USERNAME ]] || [[ -z $DB_PASSWORD ]]; then
|
||||
echo "Please make sure all required environment variables are set "
|
||||
else
|
||||
## Restore database
|
||||
if [ -f "${STORAGE_PATH}/$FILE_NAME" ]; then
|
||||
if gzip -t ${STORAGE_PATH}/$FILE_NAME; then
|
||||
zcat ${STORAGE_PATH}/${FILE_NAME} | mysql -h ${DB_HOST} -P ${DB_PORT} -u ${DB_USERNAME} --password=${DB_PASSWORD} ${DB_NAME}
|
||||
else
|
||||
cat ${STORAGE_PATH}/${FILE_NAME} | mysql -h ${DB_HOST} -P ${DB_PORT} -u ${DB_USERNAME} --password=${DB_PASSWORD} ${DB_NAME}
|
||||
fi
|
||||
echo "Database has been restored"
|
||||
else
|
||||
echo "Error, file not found in ${STORAGE_PATH}/${FILE_NAME}"
|
||||
fi
|
||||
fi
|
||||
exit
|
||||
}
|
||||
|
||||
s3_backup()
|
||||
{
|
||||
mount_s3
|
||||
backup
|
||||
}
|
||||
|
||||
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 .... "
|
||||
if [ -z "$(ls -A /s3mnt)" ]; then
|
||||
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
|
||||
if [ ! -d "/s3mnt$S3_PATH" ]; then
|
||||
mkdir -p /s3mnt$S3_PATH
|
||||
fi
|
||||
else
|
||||
echo "Object storage already mounted in /s3mnt"
|
||||
fi
|
||||
export STORAGE_PATH=/s3mnt$S3_PATH
|
||||
fi
|
||||
}
|
||||
create_crontab_script()
|
||||
{
|
||||
TASK=/usr/local/bin/backup_cron.sh
|
||||
touch $TASK
|
||||
if [ $STORAGE == 's3' ]
|
||||
then
|
||||
cat > "$TASK" <<EOF
|
||||
#!/usr/bin/env bash
|
||||
set -e
|
||||
bkup --operation backup --dbname $DB_NAME --port $DB_PORT --storage s3 --path $S3_PATH
|
||||
EOF
|
||||
else
|
||||
cat > "$TASK" <<EOF
|
||||
#!/usr/bin/env bash
|
||||
set -e
|
||||
bkup --operation backup --dbname $DB_NAME --port $DB_PORT
|
||||
EOF
|
||||
fi
|
||||
|
||||
chmod +x /usr/local/bin/backup_cron.sh
|
||||
ln -s /usr/local/bin/backup_cron.sh /usr/local/bin/backup_cron
|
||||
## Create crontab job
|
||||
CRON_JOB=/etc/cron.d/backup_cron
|
||||
touch $CRON_JOB
|
||||
cat > "$CRON_JOB" <<EOF
|
||||
$SCHEDULE_PERIOD root exec /bin/bash -c ". /run/supervisord.env; /usr/local/bin/backup_cron.sh >> /var/log/mysql-bkup.log"
|
||||
EOF
|
||||
chmod 0644 /etc/cron.d/*
|
||||
crontab /etc/cron.d/backup_cron
|
||||
}
|
||||
scheduled_mode()
|
||||
{
|
||||
if [ $OPERATION == 'backup' ]
|
||||
then
|
||||
create_crontab_script
|
||||
echo ""
|
||||
echo "**********************************"
|
||||
echo " Starting MySQL Bkup... "
|
||||
echo "***********************************"
|
||||
echo "Running in Scheduled mode"
|
||||
echo "Log file in /var/log/mysql-bkup.log"
|
||||
echo "Execution period $SCHEDULE_PERIOD"
|
||||
supervisord -c /etc/supervisor/supervisord.conf
|
||||
else
|
||||
echo "Scheduled mode supports only backup operation"
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
flags "$@"
|
||||
# ?
|
||||
if [ $EXECUTION_MODE == 'default' ]
|
||||
then
|
||||
if [ $OPERATION != 'backup' ]
|
||||
then
|
||||
if [ $STORAGE != 's3' ]
|
||||
then
|
||||
echo "Restore from local"
|
||||
restore
|
||||
else
|
||||
echo "Restore from s3"
|
||||
s3_restore
|
||||
fi
|
||||
else
|
||||
if [ $STORAGE != 's3' ]
|
||||
then
|
||||
echo "Backup to local destination"
|
||||
backup
|
||||
else
|
||||
echo "Backup to s3 storage"
|
||||
s3_backup
|
||||
fi
|
||||
fi
|
||||
elif [ $EXECUTION_MODE == 'scheduled' ]
|
||||
then
|
||||
scheduled_mode
|
||||
else
|
||||
echo "Error, unknow execution mode!"
|
||||
exit 1
|
||||
fi
|
||||
16
utils/constant.go
Normal file
16
utils/constant.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package utils
|
||||
|
||||
const Notice = "Please remove --operation flag.\n" +
|
||||
"Use: \n" +
|
||||
"- backup for database backup operation [eg: bkup backup -d database_name ...]\n" +
|
||||
"- restore for database restore operation [eg. bkup restore -d database_name ...]\n" +
|
||||
"Example: bkup backup --storage s3 ...( instead of < bkup --operation backup >)\n" +
|
||||
"We are sorry for this inconvenient\n"
|
||||
const RestoreExample = "mysql-bkup restore --dbname database --file db_20231219_022941.sql.gz\n" +
|
||||
"bkup restore --dbname database --storage s3 --path /custom-path --file db_20231219_022941.sql.gz"
|
||||
const BackupExample = "mysql-bkup backup --dbname database --disable-compression\n" +
|
||||
"mysql-bkup backup --dbname database --storage s3 --path /custom-path --disable-compression"
|
||||
|
||||
const MainExample = "mysql-bkup backup --dbname database --disable-compression\n" +
|
||||
"mysql-bkup backup --dbname database --storage s3 --path /custom-path\n" +
|
||||
"mysql-bkup restore --dbname database --file db_20231219_022941.sql.gz"
|
||||
114
utils/utils.go
Normal file
114
utils/utils.go
Normal file
@@ -0,0 +1,114 @@
|
||||
package utils
|
||||
|
||||
/*****
|
||||
* MySQL Backup & Restore
|
||||
* @author Jonas Kaninda
|
||||
* @license MIT License <https://opensource.org/licenses/MIT>
|
||||
* @link https://github.com/jkaninda/mysql-bkup
|
||||
**/
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/spf13/cobra"
|
||||
"io/fs"
|
||||
"os"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
func Info(v ...any) {
|
||||
fmt.Println("⒤ ", fmt.Sprint(v...))
|
||||
}
|
||||
func Done(v ...any) {
|
||||
fmt.Println("✔ ", fmt.Sprint(v...))
|
||||
}
|
||||
func Fatal(v ...any) {
|
||||
fmt.Println("✘ ", fmt.Sprint(v...))
|
||||
os.Exit(1)
|
||||
}
|
||||
func Fatalf(msg string, v ...any) {
|
||||
fmt.Printf("✘ "+msg, v...)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
func FileExists(filename string) bool {
|
||||
info, err := os.Stat(filename)
|
||||
if os.IsNotExist(err) {
|
||||
return false
|
||||
}
|
||||
return !info.IsDir()
|
||||
}
|
||||
|
||||
func WriteToFile(filePath, content string) error {
|
||||
file, err := os.Create(filePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
_, err = file.WriteString(content)
|
||||
return err
|
||||
}
|
||||
func ChangePermission(filePath string, mod int) {
|
||||
if err := os.Chmod(filePath, fs.FileMode(mod)); err != nil {
|
||||
Fatalf("Error changing permissions of %s: %v\n", filePath, err)
|
||||
}
|
||||
|
||||
}
|
||||
func IsDirEmpty(name string) (bool, error) {
|
||||
f, err := os.Open(name)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
_, err = f.Readdirnames(1)
|
||||
if err == nil {
|
||||
return false, nil
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// TestDatabaseConnection tests the database connection
|
||||
func TestDatabaseConnection() {
|
||||
Info("Testing database connection...")
|
||||
// Test database connection
|
||||
cmd := exec.Command("mysql", "-h", os.Getenv("DB_HOST"), "-P", os.Getenv("DB_PORT"), "-u", os.Getenv("DB_USERNAME"), "--password="+os.Getenv("DB_PASSWORD"), os.Getenv("DB_NAME"), "-e", "quit")
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
Fatal("Error testing database connection:", err)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
func GetEnv(cmd *cobra.Command, flagName, envName string) string {
|
||||
value, _ := cmd.Flags().GetString(flagName)
|
||||
if value != "" {
|
||||
err := os.Setenv(envName, value)
|
||||
if err != nil {
|
||||
return value
|
||||
}
|
||||
}
|
||||
return os.Getenv(envName)
|
||||
}
|
||||
func FlagGetString(cmd *cobra.Command, flagName string) string {
|
||||
value, _ := cmd.Flags().GetString(flagName)
|
||||
if value != "" {
|
||||
return value
|
||||
|
||||
}
|
||||
return ""
|
||||
}
|
||||
func FlagGetBool(cmd *cobra.Command, flagName string) bool {
|
||||
value, _ := cmd.Flags().GetBool(flagName)
|
||||
return value
|
||||
}
|
||||
|
||||
func SetEnv(key, value string) {
|
||||
|
||||
err := os.Setenv(key, value)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func ShowHistory() {
|
||||
}
|
||||
Reference in New Issue
Block a user