-
Notifications
You must be signed in to change notification settings - Fork 86
/
backup_pg.go
187 lines (166 loc) Β· 8.01 KB
/
backup_pg.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package cmds
import (
"fmt"
"os/exec"
"path/filepath"
"time"
"github.com/appscode/go/flags"
"github.com/appscode/go/log"
"github.com/appscode/stash/pkg/restic"
"github.com/appscode/stash/pkg/util"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/errors"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
appcatalog_cs "kmodules.xyz/custom-resources/client/clientset/versioned"
)
const (
JobPGBackup = "stash-pg-backup"
PostgresUser = "POSTGRES_USER"
PostgresPassword = "POSTGRES_PASSWORD"
EnvPgPassword = "PGPASSWORD"
PgDumpFile = "dumpfile.sql"
PgDumpCMD = "pg_dumpall"
PgRestoreCMD = "psql"
)
func NewCmdBackupPG() *cobra.Command {
var (
masterURL string
kubeconfigPath string
namespace string
appBindingName string
pgArgs string
outputDir string
setupOpt = restic.SetupOptions{
ScratchDir: restic.DefaultScratchDir,
EnableCache: false,
}
backupOpt = restic.BackupOptions{
Host: restic.DefaultHost,
StdinFileName: PgDumpFile,
}
metrics = restic.MetricsOptions{
JobName: JobPGBackup,
}
)
cmd := &cobra.Command{
Use: "backup-pg",
Short: "Takes a backup of Postgres DB",
DisableAutoGenTag: true,
RunE: func(cmd *cobra.Command, args []string) error {
flags.EnsureRequiredFlags(cmd, "app-binding", "provider", "secret-dir")
// apply nice, ionice settings from env
var err error
setupOpt.Nice, err = util.NiceSettingsFromEnv()
if err != nil {
return handleResticError(outputDir, restic.DefaultOutputFileName, err)
}
setupOpt.IONice, err = util.IONiceSettingsFromEnv()
if err != nil {
return handleResticError(outputDir, restic.DefaultOutputFileName, err)
}
// prepare client
config, err := clientcmd.BuildConfigFromFlags(masterURL, kubeconfigPath)
if err != nil {
return err
}
kubeClient, err := kubernetes.NewForConfig(config)
if err != nil {
return err
}
appCatalogClient, err := appcatalog_cs.NewForConfig(config)
if err != nil {
return err
}
// get app binding
appBinding, err := appCatalogClient.AppcatalogV1alpha1().AppBindings(namespace).Get(appBindingName, metav1.GetOptions{})
if err != nil {
return err
}
// get secret
appBindingSecret, err := kubeClient.CoreV1().Secrets(namespace).Get(appBinding.Spec.Secret.Name, metav1.GetOptions{})
if err != nil {
return err
}
// init restic wrapper
resticWrapper, err := restic.NewResticWrapper(setupOpt)
if err != nil {
return err
}
// set env for pg_dump
resticWrapper.SetEnv(EnvPgPassword, string(appBindingSecret.Data[PostgresPassword]))
// setup pipe command
backupOpt.StdinPipeCommand = restic.Command{
Name: PgDumpCMD,
Args: []interface{}{
"-U", string(appBindingSecret.Data[PostgresUser]),
"-h", appBinding.Spec.ClientConfig.Service.Name,
},
}
if pgArgs != "" {
backupOpt.StdinPipeCommand.Args = append(backupOpt.StdinPipeCommand.Args, pgArgs)
}
// wait for DB ready
waitForDBReady(appBinding.Spec.ClientConfig.Service.Name, appBinding.Spec.ClientConfig.Service.Port)
// Run backup
backupOutput, backupErr := resticWrapper.RunBackup(backupOpt)
// If metrics are enabled then generate metrics
if metrics.Enabled {
err := backupOutput.HandleMetrics(&metrics, backupErr)
if err != nil {
return errors.NewAggregate([]error{backupErr, err})
}
}
// If output directory specified, then write the output in "output.json" file in the specified directory
if backupErr == nil && outputDir != "" {
err := backupOutput.WriteOutput(filepath.Join(outputDir, restic.DefaultOutputFileName))
if err != nil {
return err
}
}
return backupErr
},
}
cmd.Flags().StringVar(&pgArgs, "pg-args", pgArgs, "Additional arguments")
cmd.Flags().StringVar(&masterURL, "master", masterURL, "The address of the Kubernetes API server (overrides any value in kubeconfig)")
cmd.Flags().StringVar(&kubeconfigPath, "kubeconfig", kubeconfigPath, "Path to kubeconfig file with authorization information (the master location is set by the master flag).")
cmd.Flags().StringVar(&namespace, "namespace", "default", "Namespace of Backup/Restore Session")
cmd.Flags().StringVar(&appBindingName, "app-binding", appBindingName, "Name of the app binding")
cmd.Flags().StringVar(&setupOpt.Provider, "provider", setupOpt.Provider, "Backend provider (i.e. gcs, s3, azure etc)")
cmd.Flags().StringVar(&setupOpt.Bucket, "bucket", setupOpt.Bucket, "Name of the cloud bucket/container (keep empty for local backend)")
cmd.Flags().StringVar(&setupOpt.Endpoint, "endpoint", setupOpt.Endpoint, "Endpoint for s3/s3 compatible backend")
cmd.Flags().StringVar(&setupOpt.URL, "rest-server-url", setupOpt.URL, "URL for rest backend")
cmd.Flags().StringVar(&setupOpt.Path, "path", setupOpt.Path, "Directory inside the bucket where backup will be stored")
cmd.Flags().StringVar(&setupOpt.SecretDir, "secret-dir", setupOpt.SecretDir, "Directory where storage secret has been mounted")
cmd.Flags().StringVar(&setupOpt.ScratchDir, "scratch-dir", setupOpt.ScratchDir, "Temporary directory")
cmd.Flags().BoolVar(&setupOpt.EnableCache, "enable-cache", setupOpt.EnableCache, "Specify weather to enable caching for restic")
cmd.Flags().IntVar(&setupOpt.MaxConnections, "max-connections", setupOpt.MaxConnections, "Specify maximum concurrent connections for GCS, Azure and B2 backend")
cmd.Flags().StringVar(&backupOpt.Host, "hostname", backupOpt.Host, "Name of the host machine")
cmd.Flags().IntVar(&backupOpt.RetentionPolicy.KeepLast, "retention-keep-last", backupOpt.RetentionPolicy.KeepLast, "Specify value for retention strategy")
cmd.Flags().IntVar(&backupOpt.RetentionPolicy.KeepHourly, "retention-keep-hourly", backupOpt.RetentionPolicy.KeepHourly, "Specify value for retention strategy")
cmd.Flags().IntVar(&backupOpt.RetentionPolicy.KeepDaily, "retention-keep-daily", backupOpt.RetentionPolicy.KeepDaily, "Specify value for retention strategy")
cmd.Flags().IntVar(&backupOpt.RetentionPolicy.KeepWeekly, "retention-keep-weekly", backupOpt.RetentionPolicy.KeepWeekly, "Specify value for retention strategy")
cmd.Flags().IntVar(&backupOpt.RetentionPolicy.KeepMonthly, "retention-keep-monthly", backupOpt.RetentionPolicy.KeepMonthly, "Specify value for retention strategy")
cmd.Flags().IntVar(&backupOpt.RetentionPolicy.KeepYearly, "retention-keep-yearly", backupOpt.RetentionPolicy.KeepYearly, "Specify value for retention strategy")
cmd.Flags().StringSliceVar(&backupOpt.RetentionPolicy.KeepTags, "retention-keep-tags", backupOpt.RetentionPolicy.KeepTags, "Specify value for retention strategy")
cmd.Flags().BoolVar(&backupOpt.RetentionPolicy.Prune, "retention-prune", backupOpt.RetentionPolicy.Prune, "Specify weather to prune old snapshot data")
cmd.Flags().BoolVar(&backupOpt.RetentionPolicy.DryRun, "retention-dry-run", backupOpt.RetentionPolicy.DryRun, "Specify weather to test retention policy without deleting actual data")
cmd.Flags().StringVar(&outputDir, "output-dir", outputDir, "Directory where output.json file will be written (keep empty if you don't need to write output in file)")
cmd.Flags().BoolVar(&metrics.Enabled, "metrics-enabled", metrics.Enabled, "Specify weather to export Prometheus metrics")
cmd.Flags().StringVar(&metrics.PushgatewayURL, "metrics-pushgateway-url", metrics.PushgatewayURL, "Pushgateway URL where the metrics will be pushed")
cmd.Flags().StringVar(&metrics.MetricFileDir, "metrics-dir", metrics.MetricFileDir, "Directory where to write metric.prom file (keep empty if you don't want to write metric in a text file)")
cmd.Flags().StringSliceVar(&metrics.Labels, "metrics-labels", metrics.Labels, "Labels to apply in exported metrics")
return cmd
}
func waitForDBReady(host string, port int32) {
log.Infoln("Checking database connection")
cmd := fmt.Sprintf(`nc "%s" "%d" -w 30`, host, port)
for {
if err := exec.Command(cmd).Run(); err != nil {
break
}
log.Infoln("Waiting... database is not ready yet")
time.Sleep(5 * time.Second)
}
}