Skip to content

Commit f3c5585

Browse files
authored
Merge pull request #74 from jkaninda/docs
Docs
2 parents c9f8a32 + 7163d03 commit f3c5585

File tree

18 files changed

+448
-161
lines changed

18 files changed

+448
-161
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ test.md
88
mysql-bkup
99
/.DS_Store
1010
/.idea
11-
bin
11+
bin
12+
Makefile

README.md

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,13 @@ For Kubernetes, you don't need to run it in scheduled mode. You can deploy it as
9595
apiVersion: batch/v1
9696
kind: Job
9797
metadata:
98-
name: backup
98+
name: backup-job
9999
spec:
100+
ttlSecondsAfterFinished: 100
100101
template:
101102
spec:
102103
containers:
103-
- name: mysql-bkup
104+
- name: pg-bkup
104105
# In production, it is advised to lock your image tag to a proper
105106
# release version instead of using `latest`.
106107
# Check https://github.com/jkaninda/mysql-bkup/releases
@@ -109,38 +110,26 @@ spec:
109110
command:
110111
- /bin/sh
111112
- -c
112-
- bkup
113-
- backup
114-
- --storage
115-
- s3
113+
- backup -d dbname
116114
resources:
117115
limits:
118116
memory: "128Mi"
119117
cpu: "500m"
120118
env:
121-
- name: DB_PORT
122-
value: "3306"
123119
- name: DB_HOST
124-
value: ""
125-
- name: DB_NAME
126-
value: "dbname"
120+
value: "mysql"
127121
- name: DB_USERNAME
128-
value: "username"
129-
# Please use secret!
122+
value: "user"
130123
- name: DB_PASSWORD
131-
value: ""
132-
- name: AWS_S3_ENDPOINT
133-
value: "https://s3.amazonaws.com"
134-
- name: AWS_S3_BUCKET_NAME
135-
value: "xxx"
136-
- name: AWS_REGION
137-
value: "us-west-2"
138-
- name: AWS_ACCESS_KEY
139-
value: "xxxx"
140-
- name: AWS_SECRET_KEY
141-
value: "xxxx"
142-
- name: AWS_DISABLE_SSL
143-
value: "false"
124+
value: "password"
125+
volumeMounts:
126+
- mountPath: /backup
127+
name: backup
128+
volumes:
129+
- name: backup
130+
hostPath:
131+
path: /home/toto/backup # directory location on host
132+
type: Directory # this field is optional
144133
restartPolicy: Never
145134
```
146135
## Available image registries

cmd/backup.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ var BackupCmd = &cobra.Command{
2121

2222
func init() {
2323
//Backup
24+
BackupCmd.PersistentFlags().StringP("storage", "s", "local", "Storage. local or s3")
25+
BackupCmd.PersistentFlags().StringP("path", "P", "", "AWS S3 path without file name. eg: /custom_path or ssh remote path `/home/foo/backup`")
2426
BackupCmd.PersistentFlags().StringP("mode", "m", "default", "Execution mode. default or scheduled")
2527
BackupCmd.PersistentFlags().StringP("period", "", "0 1 * * *", "Schedule period time")
2628
BackupCmd.PersistentFlags().BoolP("prune", "", false, "Delete old backup, default disabled")

cmd/migrate.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package cmd
2+
3+
import (
4+
"github.com/jkaninda/mysql-bkup/pkg"
5+
"github.com/jkaninda/mysql-bkup/utils"
6+
"github.com/spf13/cobra"
7+
)
8+
9+
var MigrateCmd = &cobra.Command{
10+
Use: "migrate",
11+
Short: "Migrate database from a source database to a target database",
12+
Run: func(cmd *cobra.Command, args []string) {
13+
if len(args) == 0 {
14+
pkg.StartMigration(cmd)
15+
} else {
16+
utils.Fatal("Error, no argument required")
17+
18+
}
19+
20+
},
21+
}

cmd/restore.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,7 @@ var RestoreCmd = &cobra.Command{
2424
func init() {
2525
//Restore
2626
RestoreCmd.PersistentFlags().StringP("file", "f", "", "File name of database")
27+
RestoreCmd.PersistentFlags().StringP("storage", "s", "local", "Storage. local or s3")
28+
RestoreCmd.PersistentFlags().StringP("path", "P", "", "AWS S3 path without file name. eg: /custom_path or ssh remote path `/home/foo/backup`")
2729

2830
}

cmd/root.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,11 @@ func Execute() {
3030
}
3131

3232
func init() {
33-
rootCmd.PersistentFlags().StringP("storage", "s", "local", "Storage. local or s3")
34-
rootCmd.PersistentFlags().StringP("path", "P", "", "AWS S3 path without file name. eg: /custom_path or ssh remote path `/home/foo/backup`")
3533
rootCmd.PersistentFlags().StringP("dbname", "d", "", "Database name")
36-
rootCmd.PersistentFlags().IntP("port", "p", 3306, "Database port")
3734
rootCmd.PersistentFlags().StringVarP(&operation, "operation", "o", "", "Set operation, for old version only")
38-
3935
rootCmd.AddCommand(VersionCmd)
4036
rootCmd.AddCommand(BackupCmd)
4137
rootCmd.AddCommand(RestoreCmd)
38+
rootCmd.AddCommand(MigrateCmd)
39+
4240
}

docker/Dockerfile

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ ENV DB_HOST=""
1414
ENV DB_NAME=""
1515
ENV DB_USERNAME=""
1616
ENV DB_PASSWORD=""
17-
ENV DB_PORT="3306"
17+
ENV DB_PORT=3306
1818
ENV STORAGE=local
1919
ENV AWS_S3_ENDPOINT=""
2020
ENV AWS_S3_BUCKET_NAME=""
@@ -30,11 +30,15 @@ ENV SSH_PASSWORD=""
3030
ENV SSH_HOST_NAME=""
3131
ENV SSH_IDENTIFY_FILE=""
3232
ENV SSH_PORT="22"
33+
ENV SOURCE_DB_HOST=""
34+
ENV SOURCE_DB_PORT=3306
35+
ENV SOURCE_DB_NAME=""
36+
ENV SOURCE_DB_USERNAME=""
37+
ENV SOURCE_DB_PASSWORD=""
3338
ARG DEBIAN_FRONTEND=noninteractive
34-
ENV VERSION="v1.2.2"
39+
ENV VERSION="v1.2.3"
3540
ENV BACKUP_CRON_EXPRESSION=""
36-
ENV GNUPGHOME="/tmp/gnupg"
37-
ARG WORKDIR="/app"
41+
ARG WORKDIR="/config"
3842
ARG BACKUPDIR="/backup"
3943
ARG BACKUP_TMP_DIR="/tmp/backup"
4044
ARG BACKUP_CRON="/etc/cron.d/backup_cron"
@@ -49,16 +53,14 @@ RUN apt-get clean && rm -rf /var/lib/apt/lists/*
4953

5054
RUN mkdir $WORKDIR
5155
RUN mkdir $BACKUPDIR
52-
RUN mkdir -p $BACKUP_TMP_DIR && \
53-
mkdir -p $GNUPGHOME
56+
RUN mkdir -p $BACKUP_TMP_DIR
5457
RUN chmod 777 $WORKDIR
5558
RUN chmod 777 $BACKUPDIR
5659
RUN chmod 777 $BACKUP_TMP_DIR
5760
RUN touch $BACKUP_CRON && \
5861
touch $BACKUP_CRON_SCRIPT && \
5962
chmod 777 $BACKUP_CRON && \
60-
chmod 777 $BACKUP_CRON_SCRIPT && \
61-
chmod 777 $GNUPGHOME
63+
chmod 777 $BACKUP_CRON_SCRIPT
6264

6365
COPY --from=build /app/mysql-bkup /usr/local/bin/mysql-bkup
6466
RUN chmod +x /usr/local/bin/mysql-bkup
@@ -67,19 +69,15 @@ RUN ln -s /usr/local/bin/mysql-bkup /usr/local/bin/bkup
6769

6870
ADD docker/supervisord.conf /etc/supervisor/supervisord.conf
6971

70-
WORKDIR $WORKDIR
71-
# Create backup shell script
72-
COPY <<EOF /usr/local/bin/backup
73-
#!/bin/sh
74-
# shellcheck disable=SC2068
75-
/usr/local/bin/mysql-bkup backup $@
76-
EOF
77-
# Create restore shell script
78-
COPY <<EOF /usr/local/bin/restore
79-
#!/bin/sh
80-
# shellcheck disable=SC2068
81-
/usr/local/bin/mysql-bkup restore $@
82-
EOF
83-
RUN chmod +x /usr/local/bin/backup && \
72+
# Create backup script and make it executable
73+
RUN echo '#!/bin/sh\n/usr/local/bin/mysql-bkup backup "$@"' > /usr/local/bin/backup && \
74+
chmod +x /usr/local/bin/backup
75+
# Create restore script and make it executable
76+
RUN echo '#!/bin/sh\n/usr/local/bin/mysql-bkup restore "$@"' > /usr/local/bin/restore && \
8477
chmod +x /usr/local/bin/restore
78+
# Create migrate script and make it executable
79+
RUN echo '#!/bin/sh\n/usr/local/bin/mysql-bkup migrate "$@"' > /usr/local/bin/migrate && \
80+
chmod +x /usr/local/bin/migrate
81+
82+
WORKDIR $WORKDIR
8583
ENTRYPOINT ["/usr/local/bin/mysql-bkup"]

docs/how-tos/migrate.md

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
---
2+
title: Migrate database
3+
layout: default
4+
parent: How Tos
5+
nav_order: 9
6+
---
7+
8+
# Migrate database
9+
10+
To migrate the database, you need to add `migrate` command.
11+
12+
{: .note }
13+
The Mysql backup has another great feature: migrating your database from a source database to another.
14+
15+
As you know, to restore a database from a source to a target database, you need 2 operations: which is to start by backing up the source database and then restoring the source backed database to the target database.
16+
Instead of proceeding like that, you can use the integrated feature `(migrate)`, which will help you migrate your database by doing only one operation.
17+
18+
19+
### Docker compose
20+
```yml
21+
services:
22+
mysql-bkup:
23+
# In production, it is advised to lock your image tag to a proper
24+
# release version instead of using `latest`.
25+
# Check https://github.com/jkaninda/mysql-bkup/releases
26+
# for a list of available releases.
27+
image: jkaninda/mysql-bkup
28+
container_name: mysql-bkup
29+
command: migrate
30+
volumes:
31+
- ./backup:/backup
32+
environment:
33+
## Target database
34+
- DB_PORT=3306
35+
- DB_HOST=mysql
36+
- DB_NAME=database
37+
- DB_USERNAME=username
38+
- DB_PASSWORD=password
39+
## Source database
40+
- SOURCE_DB_HOST=mysql2
41+
- SOURCE_DB_PORT=3306
42+
- SOURCE_DB_NAME=sourcedb
43+
- SOURCE_DB_USERNAME=jonas
44+
- SOURCE_DB_PASSWORD=password
45+
# mysql-bkup container must be connected to the same network with your database
46+
networks:
47+
- web
48+
networks:
49+
web:
50+
```
51+
52+
### Migrate database using Docker CLI
53+
54+
55+
```
56+
## Target database
57+
DB_PORT=3306
58+
DB_HOST=mysql
59+
DB_NAME=targetdb
60+
DB_USERNAME=targetuser
61+
DB_PASSWORD=password
62+
63+
## Source database
64+
SOURCE_DB_HOST=mysql2
65+
SOURCE_DB_PORT=3306
66+
SOURCE_DB_NAME=sourcedb
67+
SOURCE_DB_USERNAME=sourceuser
68+
SOURCE_DB_PASSWORD=password
69+
```
70+
71+
```shell
72+
docker run --rm --network your_network_name \
73+
--env-file your-env
74+
-v $PWD/backup:/backup/ \
75+
jkaninda/mysql-bkup migrate -d database_name
76+
```
77+
78+
## Kubernetes
79+
80+
```yaml
81+
apiVersion: batch/v1
82+
kind: Job
83+
metadata:
84+
name: migrate-db
85+
spec:
86+
ttlSecondsAfterFinished: 100
87+
template:
88+
spec:
89+
containers:
90+
- name: mysql-bkup
91+
# In production, it is advised to lock your image tag to a proper
92+
# release version instead of using `latest`.
93+
# Check https://github.com/jkaninda/mysql-bkup/releases
94+
# for a list of available releases.
95+
image: jkaninda/mysql-bkup
96+
command:
97+
- /bin/sh
98+
- -c
99+
- migrate -d targetdb
100+
resources:
101+
limits:
102+
memory: "128Mi"
103+
cpu: "500m"
104+
env:
105+
## Target DB
106+
- name: DB_HOST
107+
value: "postgres-target"
108+
- name: DB_USERNAME
109+
value: "mysql"
110+
- name: DB_PASSWORD
111+
value: "password"
112+
## Source DB
113+
- name: SOURCE_DB_HOST
114+
value: "postgres-source"
115+
- name: SOURCE_DB_NAME
116+
value: "sourcedb"
117+
- name: SOURCE_DB_USERNAME
118+
value: "postgres"
119+
# Please use secret!
120+
- name: SOURCE_DB_PASSWORD
121+
value: "password"
122+
restartPolicy: Never
123+
```

docs/index.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,49 @@ services:
7878
networks:
7979
web:
8080
```
81+
## Kubernetes
82+
83+
```yaml
84+
apiVersion: batch/v1
85+
kind: Job
86+
metadata:
87+
name: backup-job
88+
spec:
89+
ttlSecondsAfterFinished: 100
90+
template:
91+
spec:
92+
containers:
93+
- name: mysql-bkup
94+
# In production, it is advised to lock your image tag to a proper
95+
# release version instead of using `latest`.
96+
# Check https://github.com/jkaninda/mysql-bkup/releases
97+
# for a list of available releases.
98+
image: jkaninda/mysql-bkup
99+
command:
100+
- /bin/sh
101+
- -c
102+
- backup -d dbname
103+
resources:
104+
limits:
105+
memory: "128Mi"
106+
cpu: "500m"
107+
env:
108+
- name: DB_HOST
109+
value: "mysql"
110+
- name: DB_USERNAME
111+
value: "user"
112+
- name: DB_PASSWORD
113+
value: "password"
114+
volumeMounts:
115+
- mountPath: /backup
116+
name: backup
117+
volumes:
118+
- name: backup
119+
hostPath:
120+
path: /home/toto/backup # directory location on host
121+
type: Directory # this field is optional
122+
restartPolicy: Never
123+
```
81124
82125
## Available image registries
83126

0 commit comments

Comments
 (0)