@@ -2,16 +2,46 @@ package instance
22
33import (
44 "context"
5+ "fmt"
6+ "os"
7+ "strconv"
8+ "time"
59
10+ barmanBackup "github.com/cloudnative-pg/barman-cloud/pkg/backup"
11+ barmanCapabilities "github.com/cloudnative-pg/barman-cloud/pkg/capabilities"
12+ barmanCredentials "github.com/cloudnative-pg/barman-cloud/pkg/credentials"
13+ "github.com/cloudnative-pg/cloudnative-pg/pkg/postgres"
614 "github.com/cloudnative-pg/cnpg-i/pkg/backup"
15+ "github.com/cloudnative-pg/machinery/pkg/fileutils"
16+ "github.com/cloudnative-pg/machinery/pkg/log"
17+ pgTime "github.com/cloudnative-pg/machinery/pkg/postgres/time"
18+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
19+ "sigs.k8s.io/controller-runtime/pkg/client"
20+
21+ barmancloudv1 "github.com/cloudnative-pg/plugin-barman-cloud/api/v1"
22+ "github.com/cloudnative-pg/plugin-barman-cloud/internal/cnpgi/metadata"
723)
824
925// BackupServiceImplementation is the implementation
1026// of the Backup CNPG capability
1127type BackupServiceImplementation struct {
28+ BarmanObjectKey client.ObjectKey
29+ ClusterObjectKey client.ObjectKey
30+ Client client.Client
31+ InstanceName string
1232 backup.UnimplementedBackupServer
1333}
1434
35+ // This is an implementation of the barman executor
36+ // that always instruct the barman library to use the
37+ // "--name" option for backups. We don't support old
38+ // Barman versions that do not implement that option.
39+ type barmanCloudExecutor struct {}
40+
41+ func (barmanCloudExecutor ) ShouldForceLegacyBackup () bool {
42+ return false
43+ }
44+
1545// GetCapabilities implements the BackupService interface
1646func (b BackupServiceImplementation ) GetCapabilities (
1747 _ context.Context , _ * backup.BackupCapabilitiesRequest ,
@@ -30,7 +60,86 @@ func (b BackupServiceImplementation) GetCapabilities(
3060}
3161
3262// Backup implements the Backup interface
33- func (b BackupServiceImplementation ) Backup (_ context.Context , _ * backup.BackupRequest ) (* backup.BackupResult , error ) {
34- // TODO implement me
35- panic ("implement me" )
63+ func (b BackupServiceImplementation ) Backup (
64+ ctx context.Context ,
65+ _ * backup.BackupRequest ,
66+ ) (* backup.BackupResult , error ) {
67+ contextLogger := log .FromContext (ctx )
68+
69+ var objectStore barmancloudv1.ObjectStore
70+ if err := b .Client .Get (ctx , b .BarmanObjectKey , & objectStore ); err != nil {
71+ return nil , err
72+ }
73+
74+ if err := fileutils .EnsureDirectoryExists (postgres .BackupTemporaryDirectory ); err != nil {
75+ contextLogger .Error (err , "Cannot create backup temporary directory" , "err" , err )
76+ return nil , err
77+ }
78+
79+ capabilities , err := barmanCapabilities .CurrentCapabilities ()
80+ if err != nil {
81+ return nil , err
82+ }
83+ backupCmd := barmanBackup .NewBackupCommand (
84+ & objectStore .Spec .Configuration ,
85+ capabilities ,
86+ )
87+
88+ // We need to connect to PostgreSQL and to do that we need
89+ // PGHOST (and the like) to be available
90+ osEnvironment := os .Environ ()
91+ caBundleEnvironment := getRestoreCABundleEnv (& objectStore .Spec .Configuration )
92+ env , err := barmanCredentials .EnvSetBackupCloudCredentials (
93+ ctx ,
94+ b .Client ,
95+ objectStore .Namespace ,
96+ & objectStore .Spec .Configuration ,
97+ mergeEnv (osEnvironment , caBundleEnvironment ))
98+ if err != nil {
99+ return nil , err
100+ }
101+
102+ backupName := fmt .Sprintf ("backup-%v" , pgTime .ToCompactISO8601 (time .Now ()))
103+
104+ if err = backupCmd .Take (
105+ ctx ,
106+ backupName ,
107+ b .InstanceName ,
108+ env ,
109+ barmanCloudExecutor {},
110+ postgres .BackupTemporaryDirectory ,
111+ ); err != nil {
112+ return nil , err
113+ }
114+
115+ executedBackupInfo , err := backupCmd .GetExecutedBackupInfo (
116+ ctx ,
117+ backupName ,
118+ b .InstanceName ,
119+ barmanCloudExecutor {},
120+ env )
121+ if err != nil {
122+ return nil , err
123+ }
124+
125+ return & backup.BackupResult {
126+ BackupId : executedBackupInfo .ID ,
127+ BackupName : executedBackupInfo .BackupName ,
128+ StartedAt : metav1.Time {Time : executedBackupInfo .BeginTime }.Unix (),
129+ StoppedAt : metav1.Time {Time : executedBackupInfo .EndTime }.Unix (),
130+ BeginWal : executedBackupInfo .BeginWal ,
131+ EndWal : executedBackupInfo .EndWal ,
132+ BeginLsn : executedBackupInfo .BeginLSN ,
133+ EndLsn : executedBackupInfo .EndLSN ,
134+ BackupLabelFile : nil ,
135+ TablespaceMapFile : nil ,
136+ InstanceId : b .InstanceName ,
137+ Online : true ,
138+ Metadata : map [string ]string {
139+ "timeline" : strconv .Itoa (executedBackupInfo .TimeLine ),
140+ "version" : metadata .Data .Version ,
141+ "name" : metadata .Data .Name ,
142+ "displayName" : metadata .Data .DisplayName ,
143+ },
144+ }, nil
36145}
0 commit comments