Skip to content

Commit 8ccfae8

Browse files
Amr-Shamsjesperpedersen
authored andcommitted
[pgmoneta#916]support listobjc2 for s3
Signed-off-by: Amr-Shams <amr.shams2015.as@gmail.com>
1 parent 697045c commit 8ccfae8

14 files changed

Lines changed: 1058 additions & 9 deletions

File tree

doc/CLI.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ Commands:
4343
- 'reload' to reload the configuration
4444
- 'set' to modify a configuration value;
4545
conf set <parameter_name> <parameter_value>;
46+
s3 <action> Manage S3, with one of subcommands:
47+
- 'ls' to get the list of files in s3
4648
decompress Decompress a file using configured method
4749
decrypt Decrypt a file using master-key
4850
delete Delete a backup from a server
@@ -104,7 +106,6 @@ Example
104106
```sh
105107
pgmoneta-cli list-backup primary
106108
```
107-
108109
## restore
109110

110111
Restore a backup from a server
@@ -440,7 +441,37 @@ Configuration change requires manual restart
440441
Requested value: 192.168.1.100 (cannot be applied to live instance)
441442
Status: Requires full service restart
442443
```
444+
## s3
445+
446+
Manage the s3 storage
447+
448+
Command
443449

450+
```sh
451+
pgmoneta-cli s3 [ls] <server>
452+
```
453+
454+
Subcommand
455+
456+
- `ls` : List all the files in s3
457+
458+
Example
459+
460+
```sh
461+
pgmoneta-cli s3 ls primary
462+
```
463+
### s3 ls
464+
465+
Get the list of server files/objects in the remote storage s3
466+
467+
- you can set the server or use the global one in the config
468+
469+
Examples
470+
471+
```sh
472+
pgmoneta-cli s3 ls primary
473+
pgmoneta-cli s3 ls
474+
```
444475
## clear
445476

446477
Clear data/statistics

doc/S3.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,45 @@ s3_endpoint = <endpoint_url>
9292
s3_port = <port>
9393
s3_region = <region>
9494
```
95+
96+
### Garage Tutorial
97+
98+
If you already have Garage downloaded and your S3 access key and secret key created, the flow is:
99+
100+
1. Start Garage so the S3 endpoint is reachable.
101+
2. Configure pgmoneta to use your Garage endpoint.
102+
3. Start pgmoneta.
103+
4. Run `pgmoneta-cli s3 ls primary`.
104+
105+
Minimal configuration example:
106+
107+
```ini
108+
[pgmoneta]
109+
storage_engine = s3
110+
s3_access_key_id = <garage_access_key_id>
111+
s3_secret_access_key = <garage_secret_access_key>
112+
s3_bucket = <garage_bucket_name>
113+
s3_base_dir = pgmoneta
114+
s3_endpoint = <garage_endpoint_host>
115+
s3_port = <garage_endpoint_port>
116+
s3_region = garage
117+
s3_use_tls = off
118+
```
119+
120+
Start pgmoneta:
121+
122+
```sh
123+
pgmoneta -c pgmoneta.conf -u pgmoneta_users.conf
124+
```
125+
126+
List remote objects:
127+
128+
```sh
129+
pgmoneta-cli s3 ls primary
130+
```
131+
132+
`pgmoneta-cli s3 ls` lists objects recursively under the configured server path and returns all objects in one result set.
133+
95134
## Per-Server Configuration
96135

97136
All S3 configuration settings can be specified within a server's configuration section. This allows you to use different S3 buckets, credentials, or even regions for different PostgreSQL servers.

doc/manual/en/05-cli.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ Commands:
4646
- 'reload' to reload the configuration
4747
- 'set' to modify a configuration value;
4848
conf set <parameter_name> <parameter_value>;
49+
s3 <action> Manage S3, with one of subcommands:
50+
- 'ls' to get the list of files in s3
51+
4952
decompress Decompress a file using configured method
5053
decrypt Decrypt a file using master-key
5154
delete Delete a backup from a server
@@ -474,6 +477,39 @@ pgmoneta-cli conf get host
474477
- On restart, pgmoneta always reads from the configuration files on disk
475478
- Without file updates, restart will revert to the original file-based values
476479
480+
## s3
481+
482+
Manage the s3 storage
483+
484+
Command
485+
486+
```sh
487+
pgmoneta-cli s3 [ls] <server>
488+
```
489+
490+
Subcommand
491+
492+
- `ls` : List all the files in s3
493+
494+
Example
495+
496+
```sh
497+
pgmoneta-cli s3 ls primary
498+
```
499+
500+
### s3 ls
501+
502+
Get the list of server files/objects in the remote storage s3
503+
504+
- you can set the server or use the [pgmoneta] section in the config
505+
506+
Examples
507+
508+
```sh
509+
pgmoneta-cli s3 ls primary
510+
pgmoneta-cli s3 ls
511+
```
512+
477513
478514
## clear
479515

doc/manual/en/16-s3.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,40 @@ s3_base_dir = directory-where-backups-will-be-stored-in
5454
```
5555

5656
under the `[pgmoneta]` section.
57+
58+
## Garage tutorial
59+
60+
If Garage is already downloaded and configured with an S3 access key, secret key, and bucket, the flow is:
61+
62+
1. Start Garage.
63+
2. Configure pgmoneta for the Garage S3 endpoint.
64+
3. Start pgmoneta.
65+
4. Run `pgmoneta-cli s3 ls primary`.
66+
67+
Example `pgmoneta.conf` entries under `[pgmoneta]`:
68+
69+
``` ini
70+
storage_engine = s3
71+
s3_access_key_id = your-garage-access-key-id
72+
s3_secret_access_key = your-garage-secret-access-key
73+
s3_bucket = your-garage-bucket
74+
s3_base_dir = pgmoneta
75+
s3_endpoint = your-garage-endpoint-host
76+
s3_port = 3900
77+
s3_region = garage
78+
s3_use_tls = off
79+
```
80+
81+
Start pgmoneta:
82+
83+
``` sh
84+
pgmoneta -c pgmoneta.conf -u pgmoneta_users.conf
85+
```
86+
87+
List remote objects:
88+
89+
``` sh
90+
pgmoneta-cli s3 ls primary
91+
```
92+
93+
`pgmoneta-cli s3 ls` lists objects recursively in the configured remote path and prints all discovered objects in one output.

src/cli.c

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
#define COMMAND_STATUS "status"
8888
#define COMMAND_STATUS_DETAILS "status-details"
8989
#define COMMAND_VERIFY "verify"
90+
#define COMMAND_S3 "s3"
9091

9192
#define OUTPUT_FORMAT_JSON "json"
9293
#define OUTPUT_FORMAT_TEXT "text"
@@ -118,6 +119,7 @@ static void display_helper(char* command);
118119

119120
static int backup(SSL* ssl, int socket, char* server, uint8_t compression, uint8_t encryption, char* incremental, int32_t output_format);
120121
static int list_backup(SSL* ssl, int socket, char* server, char* sort_order, uint8_t compression, uint8_t encryption, int32_t output_format);
122+
static int list_s3_objects(SSL* ssl, int socket, char* server, uint8_t compression, uint8_t encryption, int32_t output_format);
121123
static int restore(SSL* ssl, int socket, char* server, char* backup_id, char* position, char* directory, uint8_t compression, uint8_t encryption, int32_t output_format);
122124
static int verify(SSL* ssl, int socket, char* server, char* backup_id, char* directory, char* files, uint8_t compression, uint8_t encryption, int32_t output_format);
123125
static int archive(SSL* ssl, int socket, char* server, char* backup_id, char* position, char* directory, uint8_t compression, uint8_t encryption, int32_t output_format);
@@ -253,6 +255,12 @@ struct pgmoneta_command command_table[] = {
253255
.deprecated = false,
254256
.log_message = "<list-backup> [%s]",
255257
},
258+
{.command = "s3",
259+
.subcommand = "ls",
260+
.accepted_argument_count = {1, 2},
261+
.action = MANAGEMENT_S3_LS,
262+
.deprecated = false,
263+
.log_message = "<s3 ls>"},
256264
{
257265
.command = "restore",
258266
.subcommand = "",
@@ -894,6 +902,10 @@ main(int argc, char** argv)
894902
{
895903
exit_code = list_backup(s_ssl, socket, parsed.args[0], sort_option, compression, encryption, output_format);
896904
}
905+
else if (parsed.cmd->action == MANAGEMENT_S3_LS)
906+
{
907+
exit_code = list_s3_objects(s_ssl, socket, parsed.args[0], compression, encryption, output_format);
908+
}
897909
else if (parsed.cmd->action == MANAGEMENT_RESTORE)
898910
{
899911
if (parsed.args[3])
@@ -1092,6 +1104,12 @@ help_list_backup(void)
10921104
printf("List backups for a server\n");
10931105
printf(" pgmoneta-cli list-backup <server> [--sort asc|desc]\n");
10941106
}
1107+
static void
1108+
help_s3(void)
1109+
{
1110+
printf("Manage the s3\n");
1111+
printf(" pgmoneta-cli s3 [ls] <server>\n");
1112+
}
10951113

10961114
static void
10971115
help_restore(void)
@@ -1233,6 +1251,10 @@ display_helper(char* command)
12331251
{
12341252
help_list_backup();
12351253
}
1254+
else if (!strcmp(command, COMMAND_S3))
1255+
{
1256+
help_s3();
1257+
}
12361258
else if (!strcmp(command, COMMAND_RESTORE))
12371259
{
12381260
help_restore();
@@ -1350,7 +1372,21 @@ list_backup(SSL* ssl, int socket, char* server, char* sort_order, uint8_t compre
13501372

13511373
return 1;
13521374
}
1353-
1375+
static int
1376+
list_s3_objects(SSL* ssl, int socket, char* server, uint8_t compression, uint8_t encryption, int32_t output_format)
1377+
{
1378+
if (pgmoneta_management_request_list_s3_objects(ssl, socket, server, compression, encryption, output_format))
1379+
{
1380+
goto error;
1381+
}
1382+
if (process_result(ssl, socket, output_format))
1383+
{
1384+
goto error;
1385+
}
1386+
return 0;
1387+
error:
1388+
return 1;
1389+
}
13541390
static int
13551391
restore(SSL* ssl, int socket, char* server, char* backup_id, char* position, char* directory, uint8_t compression, uint8_t encryption, int32_t output_format)
13561392
{
@@ -2498,6 +2534,11 @@ translate_command(int32_t cmd_code)
24982534
case MANAGEMENT_LIST_BACKUP:
24992535
command_output = pgmoneta_append(command_output, COMMAND_LIST_BACKUP);
25002536
break;
2537+
case MANAGEMENT_S3_LS:
2538+
command_output = pgmoneta_append(command_output, COMMAND_S3);
2539+
command_output = pgmoneta_append_char(command_output, ' ');
2540+
command_output = pgmoneta_append(command_output, "ls");
2541+
break;
25012542
case MANAGEMENT_RESTORE:
25022543
command_output = pgmoneta_append(command_output, COMMAND_RESTORE);
25032544
break;
@@ -3169,6 +3210,16 @@ translate_json_object(struct json* j)
31693210
}
31703211
pgmoneta_json_iterator_destroy(backup_it);
31713212
break;
3213+
case MANAGEMENT_S3_LS:
3214+
translate_response_argument(response);
3215+
servers = (struct json*)pgmoneta_json_get(response, MANAGEMENT_ARGUMENT_SERVERS);
3216+
pgmoneta_json_iterator_create(servers, &server_it);
3217+
while (pgmoneta_json_iterator_next(server_it))
3218+
{
3219+
translate_servers_argument((struct json*)pgmoneta_value_data(server_it->value));
3220+
}
3221+
pgmoneta_json_iterator_destroy(server_it);
3222+
break;
31723223
case MANAGEMENT_STATUS_DETAILS:
31733224
translate_response_argument(response);
31743225
servers = (struct json*)pgmoneta_json_get(response, MANAGEMENT_ARGUMENT_SERVERS);

src/include/management.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ extern "C" {
9393
#define MANAGEMENT_REMOVE_USER 27
9494
#define MANAGEMENT_LIST_USERS 28
9595

96+
#define MANAGEMENT_S3_LS 100
97+
9698
/**
9799
* Management categories
98100
*/
@@ -165,6 +167,8 @@ extern "C" {
165167
#define MANAGEMENT_ARGUMENT_RETENTION_MONTHS "RetentionMonths"
166168
#define MANAGEMENT_ARGUMENT_RETENTION_WEEKS "RetentionWeeks"
167169
#define MANAGEMENT_ARGUMENT_RETENTION_YEARS "RetentionYears"
170+
#define MANAGEMENT_ARGUMENT_S3_OBJECTS "S3Objects"
171+
#define MANAGEMENT_ARGUMENT_S3_KEY "S3Key"
168172
#define MANAGEMENT_ARGUMENT_SERVER "Server"
169173
#define MANAGEMENT_ARGUMENT_SERVERS "Servers"
170174
#define MANAGEMENT_ARGUMENT_SERVER_SIZE "ServerSize"
@@ -372,6 +376,13 @@ extern "C" {
372376
#define MANAGEMENT_ERROR_MODE_ERROR 2804
373377
#define MANAGEMENT_ERROR_MODE_UNKNOWN_ACTION 2805
374378

379+
#define MANAGEMENT_ERROR_LIST_S3_NOSERVER 2900
380+
#define MANAGEMENT_ERROR_LIST_S3_NOFORK 2901
381+
#define MANAGEMENT_ERROR_LIST_S3_WORKFLOW 2902
382+
#define MANAGEMENT_ERROR_LIST_S3_JSON_VALUE 2903
383+
#define MANAGEMENT_ERROR_LIST_S3_NETWORK 2904
384+
#define MANAGEMENT_ERROR_LIST_S3_ERROR 2905
385+
375386
/**
376387
* Output formats
377388
*/
@@ -450,6 +461,19 @@ pgmoneta_management_request_backup(SSL* ssl, int socket, char* server, uint8_t c
450461
int
451462
pgmoneta_management_request_list_backup(SSL* ssl, int socket, char* server, char* sort_order, uint8_t compression, uint8_t encryption, int32_t output_format);
452463

464+
/**
465+
* Create a list s3 objects request
466+
* @param ssl The SSL connection
467+
* @param socket The socket descriptor
468+
* @param server The server
469+
* @param compression The compress method for wire protocol
470+
* @param encryption The encrypt method for wire protocol
471+
* @param output_format The output format
472+
* @return 0 upon success, otherwise 1
473+
*/
474+
int
475+
pgmoneta_management_request_list_s3_objects(SSL* ssl, int socket, char* server, uint8_t compression, uint8_t encryption, int32_t output_format);
476+
453477
/**
454478
* Create a restore request
455479
* @param ssl The SSL connection

0 commit comments

Comments
 (0)