forked from inteos/pgsql-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pgsql-fd.c
2522 lines (2137 loc) · 70.6 KB
/
pgsql-fd.c
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2013 Inteos Sp. z o.o.
* All right reserved. See LICENSE.pgsql for details.
*
* This is a Bacula plugin for making backup and restore of PostgreSQL database.
*
* config file: pgsql.conf
#
# Config file for pgsql plugin
#
PGDATA = <pg.data.cluster.path>
PGHOST = <path.to.sockets.directory>
PGPORT = <socket.'port'>
PGVERSION = "8.x" | "9.x"
CATDB = <catalog.db.name>
CATDBHOST = <catalog.db.host>
CATDBPORT = <catalog.db.port>
CATUSER = <catalog.db.user>
CATPASSWD = <catalog.db.password>
ARCHDEST = <destination.of.archived.wal's.path>
ARCHCLIENT = <name.of.archived.client>
*/
/*
TODO:
* add a timeout watchdog thread to check if everything is ok.
* add a remote ARCHDEST using SSH/SCP
* add an offline backup mode of operation
* scenario:
* if database instance is down then perform a standard file backup (including pg_xlog)
* if database is running and plugin command having 'dboff' parameter then shutdown a database
* instance and perform a standard file backup (including pg_xlog), at the end startup an
* instance
* add an integration with pgsqllib utility framework
* prepare a docummentation about plugin
*/
#include "bacula.h"
#include "fd_plugins.h"
#include "fdapi.h"
/* it is PostgreSQL backup plugin, so we need a libpq library */
#include <libpq-fe.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <libgen.h>
#include <utime.h>
#include "keylist.h"
#include "parseconfig.h"
/*
* libbac uses its own sscanf implementation which is not compatible with
* libc implementation, unfortunately. usage of bsscanf require format string rewriting.
*/
#ifdef sscanf
#undef sscanf
#endif
#ifdef __cplusplus
extern "C" {
#endif
#define PLUGIN_LICENSE "AGPLv3"
#define PLUGIN_AUTHOR "Inteos Sp. z o.o."
#define PLUGIN_DATE "April 2013"
#define PLUGIN_VERSION "2.2"
#define PLUGIN_DESCRIPTION "PostgreSQL online backup and recovery plugin (c) Inteos Sp. z o.o."
#define PLUGIN_INFO "pgsql plugin: "
/* Forward referenced functions */
static bRC newPlugin(bpContext *ctx);
static bRC freePlugin(bpContext *ctx);
static bRC getPluginValue(bpContext *ctx, pVariable var, void *value);
static bRC setPluginValue(bpContext *ctx, pVariable var, void *value);
static bRC handlePluginEvent(bpContext *ctx, bEvent *event, void *value);
static bRC startBackupFile(bpContext *ctx, struct save_pkt *sp);
static bRC endBackupFile(bpContext *ctx);
static bRC pluginIO(bpContext *ctx, struct io_pkt *io);
static bRC startRestoreFile(bpContext *ctx, const char *cmd);
static bRC endRestoreFile(bpContext *ctx);
static bRC createFile(bpContext *ctx, struct restore_pkt *rp);
static bRC setFileAttributes(bpContext *ctx, struct restore_pkt *rp);
static bRC checkFile(bpContext *ctx, char *fname);
keylist * get_file_list ( bpContext *ctx, keylist * list, const char * base, const char * path );
/* Pointers to Bacula functions */
static bFuncs *bfuncs = NULL;
static bInfo *binfo = NULL;
static pInfo pluginInfo = {
sizeof(pluginInfo),
#ifdef FDAPI
FDAPI,
#else
FD_PLUGIN_INTERFACE_VERSION,
#endif
FD_PLUGIN_MAGIC,
PLUGIN_LICENSE,
PLUGIN_AUTHOR,
PLUGIN_DATE,
PLUGIN_VERSION,
PLUGIN_DESCRIPTION,
};
static pFuncs pluginFuncs = {
sizeof(pluginFuncs),
FD_PLUGIN_INTERFACE_VERSION,
/* Entry points into plugin */
newPlugin,
freePlugin,
getPluginValue,
setPluginValue,
handlePluginEvent,
startBackupFile,
endBackupFile,
startRestoreFile,
endRestoreFile,
pluginIO,
createFile,
setFileAttributes,
checkFile
};
/*
* pgsql plugin requires working PostgreSQL catalog database
* it could be Bacula catalog database if it is postgresql
* it can't be database which we backup, because it has to be available
* during instance shutdown, when WAls are generated.
*/
enum PGSQLMode {
PGSQL_NONE = 0,
PGSQL_ARCH_BACKUP,
PGSQL_DB_BACKUP,
PGSQL_ARCH_RESTORE,
PGSQL_DB_RESTORE,
};
enum PGFileType {
PG_NONE = 0,
PG_FILE,
PG_LINK,
PG_DIR,
};
typedef enum {
PARSE_BACKUP,
PARSE_RESTORE,
} ParseMode;
typedef struct _pg_plug_inst pg_plug_inst;
struct _pg_plug_inst {
int JobId;
PGconn * catdb;
char * restore_command_value;
char * configfile;
keylist * paramlist;
int mode;
keylist * filelist;
keyitem * curfile;
int curfd;
int diropen;
char * linkval;
int linkread;
};
/*
* TODO:
* TODO: integrate pgsql-fd with pgsqllib
* TODO:
*/
/* size of different string or sql buffer */
#define SQLLEN 256
#define CONNSTRLEN 128
/* Assertions defines */
#define ASSERT_bfuncs \
if ( ! bfuncs ){ \
return bRC_Error; \
}
#define ASSERT_ctx \
if ( ! ctx ) { \
return bRC_Error; \
}
#define ASSERT_ctxp_RET_BRCERROR ASSERT_ctx_p
#define ASSERT_ctx_p \
if ( ! ctx || ! ctx->pContext ) { \
return bRC_Error; \
}
#define ASSERT_p(value) \
if ( ! value ){ \
return bRC_Error; \
}
#define ASSERT_n(value) \
if ( value ){ \
return bRC_Error; \
}
#define ASSERT_pex(value) \
if ( ! value ){ \
exit ( bRC_Error ); \
}
/* memory allocation/deallocation */
#define MALLOC(size) \
(char *) malloc ( size );
#define FREE(ptr) \
free ( ptr ); \
ptr = NULL;
/*
* TODO: extend with additional calls for Bacula-FD: baculaAddOptions
*/
#define JMSG0(ctx,type,msg) \
bfuncs->JobMessage ( ctx, __FILE__, __LINE__, type, 0, PLUGIN_INFO msg );
#define JMSG(ctx,type,msg,var) \
bfuncs->JobMessage ( ctx, __FILE__, __LINE__, type, 0, PLUGIN_INFO msg, var );
#define JMSG2(ctx,type,msg,var1,var2) \
bfuncs->JobMessage ( ctx, __FILE__, __LINE__, type, 0, PLUGIN_INFO msg, var1, var2 );
#define DMSG0(ctx,level,msg) \
bfuncs->DebugMessage ( ctx, __FILE__, __LINE__, level, PLUGIN_INFO msg );
#define DMSG1(ctx,level,msg,var) \
bfuncs->DebugMessage ( ctx, __FILE__, __LINE__, level, PLUGIN_INFO msg, var );
#define DMSG2(ctx,level,msg,var1,var2) \
bfuncs->DebugMessage ( ctx, __FILE__, __LINE__, level, PLUGIN_INFO msg, var1, var2 );
#define DMSG3(ctx,level,msg,var1,var2,var3) \
bfuncs->DebugMessage ( ctx, __FILE__, __LINE__, level, PLUGIN_INFO msg, var1, var2, var3 );
/* fixed debug level definitions */
#define D1 1
#define D2 10
#define D3 100
/* error in sql exec */
#define PGERROR(msg,sql,result,status) \
DMSG2 ( ctx, D1, "%s (%s)\n", msg, sql); \
JMSG2 ( ctx, M_ERROR, "%s (%s)\n", msg, sql); \
JMSG ( ctx, M_ERROR, "pg_res: %s\n", PQresStatus(status) ); \
JMSG ( ctx, M_ERROR, "pg_err: %s\n", PQresultErrorMessage(result) );
/*
* Plugin called here when it is first loaded
*/
bRC loadPlugin ( bInfo *lbinfo, bFuncs *lbfuncs, pInfo **pinfo, pFuncs **pfuncs )
{
bfuncs = lbfuncs; /* set Bacula funct pointers */
binfo = lbinfo;
printf ( PLUGIN_INFO "version %s %s (c) 2011 by Inteos\n", PLUGIN_VERSION, PLUGIN_DATE );
printf ( PLUGIN_INFO "connected to Bacula version %d\n", binfo->version );
*pinfo = &pluginInfo; /* return pointer to our info */
*pfuncs = &pluginFuncs; /* return pointer to our functions */
return bRC_OK;
}
/*
* Plugin called here when it is unloaded, normally when Bacula is going to exit.
*/
bRC unloadPlugin()
{
return bRC_OK;
}
/*
* Called here to make a new instance of the plugin -- i.e. when
* a new Job is started. There can be multiple instances of
* each plugin that are running at the same time. Your
* plugin instance must be thread safe and keep its own
* local data.
*/
static bRC newPlugin ( bpContext *ctx )
{
pg_plug_inst * pinst;
ASSERT_ctx;
pinst = (pg_plug_inst *) malloc ( sizeof( pg_plug_inst ) );
if ( pinst == NULL )
{
JMSG0 ( ctx, M_FATAL, "Error allocating plugin structures" );
return bRC_Error;
}
ctx->pContext = pinst;
/* initialize pinst contents */
memset ( pinst, 0, sizeof ( pg_plug_inst ) );
bfuncs->getBaculaValue ( ctx, bVarJobId, (void *)&pinst->JobId );
DMSG1 ( ctx, D1, "newPlugin JobId=%d\n", pinst->JobId );
return bRC_OK;
}
/*
* Release everything concerning a particular instance of a
* plugin. Normally called when the Job terminates.
*/
static bRC freePlugin ( bpContext *ctx )
{
int JobId = 0;
pg_plug_inst *pinst;
ASSERT_ctx_p;
pinst = (pg_plug_inst *) ctx->pContext;
ASSERT_bfuncs;
bfuncs->getBaculaValue ( ctx, bVarJobId, (void *)&JobId );
if ( pinst->JobId != JobId )
{
JMSG0 ( ctx, M_ERROR, "freePlugin JobId mismatch" );
}
/* free pg_plug_inst private data */
PQfinish ( pinst->catdb );
if ( pinst->configfile )
FREE ( pinst->configfile );
keylist_free ( pinst->paramlist );
keylist_free ( pinst->filelist );
FREE ( pinst );
DMSG1 ( ctx, D2, "freePlugin JobId=%d\n", JobId );
return bRC_OK;
}
/*
* Called by core code to get a variable from the plugin.
* Not currently used.
*/
static bRC getPluginValue ( bpContext *ctx, pVariable var, void *value )
{
DMSG0 ( ctx, D3, "getPluginValue called.\n" );
return bRC_OK;
}
/*
* Called by core code to set a plugin variable.
* Not currently used.
*/
static bRC setPluginValue ( bpContext *ctx, pVariable var, void *value )
{
DMSG0 ( ctx, D3, "setPluginValue called." );
return bRC_OK;
}
/*
* Parsing a plugin command.
*
* in:
* ctx - plugin context
* parse_mode - indicate if we parse for backup or restore
* command - plugin command string to parse
* out:
* ctx->pContext->configfile - name and path to config file
* ctx->pContext->mode - what we are doing (wal backup, db backup, db restore)
* ctx->pContext->paramlist - a list of parameters from config file
* bRC_OK - on success
* bRC_Error - on error
*/
bRC parse_plugin_command ( bpContext *ctx, const ParseMode parse_mode, const char * command )
{
/* pgsql:/usr/local/bacula/etc/pgsql.phobos.conf:[wal,db] */
char * s;
char * n;
pg_plug_inst * pinst;
int len;
ASSERT_ctx_p;
pinst = (pg_plug_inst *) ctx->pContext;
if ( ! ( pinst->restore_command_value != NULL &&
strncmp ( pinst->restore_command_value, command, strlen ( command ) ) == 0 ) ){
/* check for command correctness */
s = strchr ( (char *)command, ':' );
ASSERT_p ( s );
s++;
/* check for command correctness */
n = strchr ( s, ':' );
ASSERT_p ( n );
ASSERT_bfuncs;
/*
* s points to begin of the config file name, n points into last ':' sign
* len indicate a length of config file name
*/
len = n - s;
/* if configfile was allocated then free it */
if ( pinst->configfile ){
FREE ( pinst->configfile );
}
if ( pinst->restore_command_value ){
FREE ( pinst->restore_command_value );
}
if ( pinst->paramlist ){
keylist_free ( pinst->paramlist );
}
/* new configfile allocation */
pinst->configfile = MALLOC ( len + 1 );
ASSERT_p ( pinst->configfile );
/* copy config filename from plugin command */
strncpy ( pinst->configfile, s, len );
/* terminate config filename with null */
pinst->configfile [len] = 0;
n++;
/* search for command/mode setting */
if ( ! strncasecmp ( n, "wal", 3 ) ){
if ( parse_mode == PARSE_BACKUP )
pinst->mode = PGSQL_ARCH_BACKUP;
else
if ( parse_mode == PARSE_RESTORE )
pinst->mode = PGSQL_ARCH_RESTORE;
} else
if ( ! strncasecmp ( n, "db", 2 ) ){
if ( parse_mode == PARSE_BACKUP )
pinst->mode = PGSQL_DB_BACKUP;
else
if ( parse_mode == PARSE_RESTORE )
pinst->mode = PGSQL_DB_RESTORE;
} else {
/* unknown plugin command */
FREE ( pinst->configfile );
return bRC_Error;
}
pinst->paramlist = parse_config_file ( pinst->configfile );
pinst->restore_command_value = bstrdup ( command );
}
return bRC_OK;
}
/*
* connect to pgsql catalog database
*
* in:
* ctx - plugin context
* out:
* ctx->pContext->catdb - working catalog connection
* 0 - on success
* 1 - on error
*/
bRC catdbconnect ( bpContext *ctx ){
char * catdbconnstring;
ConnStatusType status;
pg_plug_inst * pinst;
ASSERT_ctx_p;
pinst = (pg_plug_inst *)ctx->pContext;
catdbconnstring = MALLOC ( CONNSTRLEN );
ASSERT_p ( catdbconnstring );
snprintf ( catdbconnstring, CONNSTRLEN, "host=%s port=%s dbname=%s user=%s password=%s",
search_key ( pinst->paramlist, "CATDBHOST" ),
search_key ( pinst->paramlist, "CATDBPORT" ),
search_key ( pinst->paramlist, "CATDB" ),
search_key ( pinst->paramlist, "CATUSER" ),
search_key ( pinst->paramlist, "CATPASSWD" ));
pinst->catdb = PQconnectdb ( catdbconnstring );
FREE ( catdbconnstring );
status = PQstatus ( pinst->catdb );
if ( status == CONNECTION_BAD ){
return bRC_Error;
}
return bRC_OK;
}
/*
* perform an internal connection to the database in separate process and execute sql
* statement
*
* in:
* sql - SQL statement
* out:
* bRC_OK - OK
* bRC_ERROR - Error
* bRC_More -
*/
bRC pg_internal_conn ( bpContext *ctx, const char * sql ){
PGconn * db;
ConnStatusType status;
ExecStatusType resstatus;
PGresult * result;
int pid = 0;
struct stat st;
uid_t pguid;
int err;
char connstring[CONNSTRLEN];
pg_plug_inst * pinst;
int exitstatus = 0;
/* check input data */
ASSERT_ctx_p;
pinst = (pg_plug_inst *)ctx->pContext;
/* dynamic production database owner verification, we use it do connect to production
* database. Required PGDATA from config file */
ASSERT_p ( pinst->paramlist );
err = stat ( search_key ( pinst->paramlist, "PGDATA" ) , &st );
if ( err ){
/* error, invalid PGDATA in config file */
JMSG0 ( ctx, M_ERROR, "invalid 'PGDATA' variable in config file." );
return bRC_Error;
}
/* we will fork to the owner of PGDATA database cluster */
pguid = st.st_uid;
/* switch pg xlog in different process, so make a fork */
/*
* TODO: test if switching is possible only with seteuid to postgres and return to root after that
* it was possible with pg_ctl shutdown ... but what about possible security flaw?
*/
pid = fork ();
if ( pid == 0 ){
DMSG0 ( ctx, D3, "forked process\n" );
/* sleep used for debuging forked process in gdb */
// sleep ( 60 );
/* we are in forked process, do a real job */
/* switch to pgcluster owner (postgres) */
err = seteuid ( pguid );
if ( err ){
/* error switching uid, report a problem */
/* TODO: add an errorlog display */
JMSG ( ctx, M_ERROR, "seteuid to uid=%i failed!\n", pguid );
exit (bRC_Error);
}
/* host is a socket directory, port is a socket 'port', we perform an 'internal'
* connection through a postgresql socket, which is required by plugin
* it require in pg_hba.conf a following line:
* local all postgres trust
* without it internall connection will not work */
snprintf ( connstring, CONNSTRLEN, "host=%s port=%s",
search_key ( pinst->paramlist, "PGHOST" ),
search_key ( pinst->paramlist, "PGPORT" ) );
db = PQconnectdb ( connstring );
status = PQstatus ( db );
if ( status == CONNECTION_BAD ){
DMSG0 ( ctx, D1, "pg_internal_conn.conndb failed!\n" );
JMSG0 ( ctx, M_WARNING, "pg_internal_conn.conndb failed!\n" );
/* not all goes ok so we have to raise it, but it is not a critical error,
* it should be handled by calling function */
exit (bRC_More);
}
/* we have a successful production database connection, so execute sql */
result = PQexec ( db, sql );
resstatus = PQresultStatus ( result );
if ( !(resstatus == PGRES_TUPLES_OK || resstatus == PGRES_COMMAND_OK) ){
/* TODO: add an errorlog display */
PGERROR ("pg_internal_conn.pqexec failed!", sql, result, resstatus);
exit (bRC_Error);
}
/* finish database connection */
PQfinish ( db );
/* finish forked process */
exit (bRC_OK);
} else {
/* we are waiting for background process to finish */
waitpid ( pid, &exitstatus, 0 );
}
/* XXX: we should check for forked process exit status */
DMSG1 ( ctx, D2, "pg_internal_conn.exit: %i\n", WEXITSTATUS( exitstatus ) );
return (bRC) WEXITSTATUS( exitstatus );
}
/*
* it makes a connection to production database in separate process and perform a
* transaction log switching
*/
bRC switch_pg_xlog ( bpContext *ctx ){
bRC err;
err = pg_internal_conn ( ctx, "select pg_switch_xlog()" );
if ( err == bRC_OK ) {
/* wait a few seconds to switch log to finish
* FIXME: we have to consider how to perform interprocess synchronization
* one of the option is to use message passing (pgsql_msg_send,pgsql_msg_recv) */
DMSG0 ( ctx, D3, "waiting for switch log");
sleep ( 3 );
DMSG0 ( ctx, D3, "waiting finish");
} else
if ( err == bRC_More ){
/* we have got a connection error for log switching, possibly database temporary unavailable
* maintanance shutdown? */
JMSG0 ( ctx, M_INFO, "switch log failed. Current wal wasn't archived\n" );
err = bRC_OK;
}
return err;
}
/*
* start backup procedure
*
* in:
* ctx - plugin context
* out:
* bRC_OK - success
* bRC_Error - error
*/
bRC start_pg_backup ( bpContext *ctx ){
pg_plug_inst * pinst;
char * sql;
bRC err = bRC_OK;
/* check input data */
ASSERT_ctx_p;
pinst = (pg_plug_inst *)ctx->pContext;
sql = MALLOC ( SQLLEN );
ASSERT_p ( sql );
#if 0
/* catalog database connection */
err = catdbconnect ( ctx );
if ( err ){
return bRC_Error;
}
/* insert status in catalog */
snprintf ( sql, SQLLEN, "insert into pgsql_pgsql_backupdbs (client, status, blevel) values ('%s', '%i', '%i')",
search_key ( pdata->paramlist, "ARCHCLIENT" ),
status, 0 );
result = PQexec ( pdata->catdb, sql );
FREE ( sql );
if ( PQresultStatus ( result ) != PGRES_COMMAND_OK ){
abortprg ( pdata, EXITCATDBEINS, "CATDB: SQL insert error!" );
}
pgid = get_walid_from_catalog ( pdata );
#endif
snprintf ( sql, SQLLEN, "select pg_start_backup ('%s:%i')",
search_key ( pinst->paramlist, "ARCHCLIENT" ),
pinst->JobId );
err = pg_internal_conn ( ctx, sql );
/* error database connection in this case it is a problem,
* rise en error for calling function */
if ( err == bRC_More )
err = bRC_Error;
FREE ( sql );
return err;
}
/*
* end backup procedure
*
* in:
* ctx - plugin context
* out:
* bRC_OK - success
* bRC_Error - error
*/
bRC stop_pg_backup ( bpContext *ctx ){
bRC err = bRC_OK;
err = pg_internal_conn ( ctx, "select pg_stop_backup ()" );
/* error database connection in this case it is a problem,
* rise en error for calling function */
if ( err == bRC_More )
err = bRC_Error;
return err;
}
/*
* grab an arhivelogs list for backup
*
* in:
* ctx - plugin context
* out:
* bRC_OK - success
* bRC_Error - error
*/
bRC get_wal_list ( bpContext *ctx ){
PGresult * result;
char * sql;
pg_plug_inst * pinst;
char * filename;
char * pgid;
int nr;
ExecStatusType resstatus;
ASSERT_ctx_p;
pinst = (pg_plug_inst *)ctx->pContext;
ASSERT_p ( pinst->paramlist );
sql = MALLOC ( SQLLEN );
ASSERT_p ( sql );
/* PGSQL_STATUS_WAL_ARCH_FINISH, PGSQL_STATUS_WAL_ARCH_MULTI */
snprintf ( sql, SQLLEN, "select * from pgsql_archivelogs where client='%s' and status in (3,5) order by mod_date",
search_key ( pinst->paramlist, "ARCHCLIENT" ) );
result = PQexec ( pinst->catdb, sql );
resstatus = PQresultStatus ( result );
if ( resstatus != PGRES_TUPLES_OK ){
PGERROR ( "get_wal_list.pqexec failed!", sql, result, resstatus );
return bRC_Error;
}
// FREE ( sql );
nr = PQntuples ( result );
if ( nr ){
for (int a = 0; a < nr; a++ ){
pgid = (char *) PQgetvalue ( result, a, PQfnumber ( result, "id") );
filename = (char *) PQgetvalue ( result, a, PQfnumber ( result, "filename") );
pinst->filelist = add_keylist ( pinst->filelist, pgid, filename );
}
/* first node of the list will be our current file for backup */
pinst->curfile = (keyitem *)pinst->filelist->first();
//print_keylist ( pinst->filelist );
} else {
/* no files to backup, startBackupFile will fill required structs */
pinst->filelist = NULL;
pinst->curfile = NULL;
}
return bRC_OK;
}
/*
* function performs pg_tblspc directory analyzis in case of defined tablespaces - symbolic links
* into a directories where a tablespaces are located. we have to insert both: pg_tblspc directory
* with links and tablespaces location directories.
*
* pg_tblspc directory and its links will have a pgsqldb namespace but every tablespace location
* will have a pgsqltbs namespace instead
*
* for PostgreSQL version 9.x we have to handle a different tablespace directory hierarchy
* postgres version is not discovered currently and we hanle it through PGVERSION parameter
*
* in:
* ctx - plugin context
* list - current filename list
* base - indicate if filename path is relative or absolute
* '$ROOT$' - path is absolute, no need to concatenate both
* other => absolute filename path = $base/$path
* path - filename path (file or directory)
* out:
* list - updated filename list
*/
keylist * get_tablespace_dir ( bpContext * ctx, keylist * list, const char * base, const char * path )
{
DIR * dirp_tab;
DIR * dirp_tablink;
char * bpath = NULL;
char * link = NULL;
keylist * nlist = list;
struct dirent * filedir_tab;
struct dirent * filedir_tablink;
int dl;
char * pgver;
pg_plug_inst * pinst;
pinst = (pg_plug_inst *)ctx->pContext;
DMSG1 ( ctx, D2, "perform tablespace dir at: %s/pg_tblspc\n", base );
/* we add to the file list a contents of tablespace directory pg_tplspc at
* pgsqldb namespace */
nlist = get_file_list ( ctx, nlist, base, "pg_tblspc" );
/* most important variable allocation on heap */
bpath = MALLOC ( PATH_MAX );
if ( ! bpath ){
JMSG0 ( ctx, M_ERROR, "error allocating memory." );
return list;
}
/* building absolute path into a tablespaces directory for symbolic links analyzis */
snprintf ( bpath, PATH_MAX, "%s/pg_tblspc/", base );
dirp_tab = opendir ( bpath );
if ( !dirp_tab ){
JMSG ( ctx, M_ERROR, "error opening dir: %s", bpath );
FREE ( bpath );
return nlist;
}
/* all tablespaces locations are symbolic links */
while ( (filedir_tab = readdir ( dirp_tab )) ){
if ( strcmp ( filedir_tab->d_name, "." ) != 0 &&
strcmp ( filedir_tab->d_name, ".." ) != 0 ){
DMSG1 ( ctx, D2, "tablespace found: %s\n", filedir_tab->d_name );
link = MALLOC ( PATH_MAX );
if ( ! link ){
JMSG0 ( ctx, M_ERROR, "error allocating memory." );
FREE ( bpath );
return nlist;
}
/* building absolute path into a links */
snprintf ( bpath, PATH_MAX, "%s/pg_tblspc/%s", base, filedir_tab->d_name );
/* get a link contents */
dl = readlink ( bpath, link, PATH_MAX - 1 );
if ( dl < 0 ){
JMSG ( ctx, M_ERROR, "error reading link: %s", strerror ( errno ) );
FREE ( bpath );
FREE ( link );
closedir ( dirp_tab );
return nlist;
}
link [ dl ] = 0;
DMSG2 ( ctx, D3, "symlink dl=%i %s\n", dl, link );
/* check if a link is absolute or not */
if ( link [ 0 ] == '/' ){
/* absolute - it is good */
strncpy ( bpath, link, PATH_MAX );
} else {
/* according to PostgreSQL a tablespace location has to be an absolute,
* if not we have an error, raise it. */
JMSG0 ( ctx, M_ERROR, "Relative link Error!" );
FREE ( bpath );
FREE ( link );
closedir ( dirp_tab );
return nlist;
}
/* all tablespaces are backuped with absolute path */
realpath ( bpath, link );
pgver = search_key ( pinst->paramlist, "PGVERSION" );
if ( !pgver || strcmp ( pgver, "8.x" ) == 0 ){
/* PostgreSQL 8.x */
DMSG0(ctx, D3, "PGVERSION=8.x\n");
nlist = get_file_list ( ctx, nlist, "$ROOT$", link );
} else
if ( strcmp (pgver, "9.x") == 0){
/* PostgreSQL 9.x */
dirp_tablink = opendir ( link );
if ( !dirp_tablink ){
JMSG ( ctx, M_ERROR, "error opening dir: %s", bpath );
FREE ( bpath );
FREE ( link );
return nlist;
}
while ( (filedir_tablink = readdir ( dirp_tablink )) ){
if ( strcmp ( filedir_tablink->d_name, "." ) != 0 &&
strcmp ( filedir_tablink->d_name, ".." ) != 0 ){
if ( strncmp ( filedir_tablink->d_name, "PG_9.", 5 ) == 0){
DMSG0(ctx, D3, "PGVERSION=9.x\n");
snprintf ( bpath, PATH_MAX, "%s/%s", link, filedir_tablink->d_name );
DMSG1(ctx,D3,"->%s\n",bpath);
nlist = get_file_list ( ctx, nlist, "$ROOT$", bpath );
}
}
}
/* end postgres 9.x */
} else {
JMSG ( ctx, M_ERROR, "unsupported PostgreSQL version: %s",pgver);
FREE ( bpath );
FREE ( link );
return nlist;
}
FREE ( link );
}
}
closedir ( dirp_tab );
DMSG0 ( ctx, D2, "end perform tablespace\n" );
return nlist;
}
/*
* adds a directory entry into a filename list
*
* in:
* ctx - plugin context
* list - current filename list
* base - indicate if filename path is relative or absolute
* '$ROOT$' - path is absolute, no need to concatenate both
* other => absolute filename path = $base/$path
* path - filename path (file or directory)
* direntry -
* out:
* list - updated filename list
*/
keylist * get_dir_content ( bpContext *ctx, keylist * list, const char * base, const char * path, char * direntry ){
char * npath = NULL;
keylist * nlist = list;
int plen = strlen ( path );
npath = MALLOC ( PATH_MAX );
if ( ! npath ){
JMSG0 ( ctx, M_ERROR, "error allocating memory." );
return nlist;
}
/* XXX: required fix, we concatenate directory content with relative path into npath */
snprintf ( npath, PATH_MAX, plen ? "%s/%s" : "%s%s",
path, direntry );
if ( strcmp ( direntry, "pg_xlog" ) == 0 ){
/* we have to add pg_xlog and pg_xlog/archive_status directories into a list but
* without its contents */
int len = strlen ( npath );
npath [ len ] = '/';
npath [ len + 1 ] = '\0';
nlist = add_keylist_attr ( nlist, base, npath, PG_DIR );
snprintf ( npath, PATH_MAX, "%s%s/archive_status", path, direntry );
nlist = add_keylist_attr ( nlist, base, npath, PG_DIR );
} else {
/* recursive add a file/directory */
nlist = get_file_list ( ctx, nlist, base, npath );
}
FREE ( npath );
return nlist;
}
/*
* adds a dir filetype into a files list
*
* in:
* ctx - plugin context
* list - current filename list
* base - indicate if filename path is relative or absolute
* '$ROOT$' - path is absolute, no need to concatenate both
* other => absolute filename path = $base/$path
* path - filename path (file or directory)
* bpath - absolute path
* out:
* list - updated filename list
*/
keylist * add_dir_list ( bpContext *ctx, keylist * list, const char * base, const char * path, char * bpath ){
char * npath = NULL;
keylist * nlist = list;
int plen = strlen ( path );
npath = MALLOC ( PATH_MAX );
if ( ! npath ){
JMSG0 ( ctx, M_ERROR, "Error allocating memory." );
return NULL;
}
DMSG3 ( ctx, D3, "add dir to the list base=%s path=%s bpath=%s\n", base, path, bpath );
if ( strncmp ( base, "$ROOT$", PATH_MAX ) == 0 ){
/* means that we backup directory with absolute path */