Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit42ba0f8

Browse files
committed
Add encryption support
1 parente66d96a commit42ba0f8

File tree

16 files changed

+2127
-43
lines changed

16 files changed

+2127
-43
lines changed

‎Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ PROGRAM = pg_probackup
22

33
# utils
44
OBJS = src/utils/configuration.o src/utils/json.o src/utils/logger.o\
5-
src/utils/parray.o src/utils/pgut.o src/utils/thread.o src/utils/remote.o src/utils/file.o
5+
src/utils/parray.o src/utils/pgut.o src/utils/thread.o src/utils/remote.o src/utils/file.o src/utils/rijndael.o
66

77
OBJS += src/archive.o src/backup.o src/catalog.o src/checkdb.o src/configure.o src/data.o\
88
src/delete.o src/dir.o src/fetch.o src/help.o src/init.o src/merge.o\

‎src/archive.c

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ push_wal_file(const char *from_path, const char *to_path, bool is_compress,
137137
booloverwrite)
138138
{
139139
FILE*in=NULL;
140-
intout=-1;
140+
FILE*out=NULL;
141141
charbuf[XLOG_BLCKSZ];
142142
constchar*to_path_p;
143143
charto_path_temp[MAXPGPATH];
@@ -157,7 +157,7 @@ push_wal_file(const char *from_path, const char *to_path, bool is_compress,
157157
to_path_p=to_path;
158158

159159
/* open file for read */
160-
in=fio_fopen(from_path,PG_BINARY_R,FIO_DB_HOST);
160+
in=fio_fopen(from_path,PG_BINARY_R,FIO_DB_HOST, false);
161161
if (in==NULL)
162162
elog(ERROR,"Cannot open source WAL file \"%s\": %s",from_path,
163163
strerror(errno));
@@ -178,20 +178,23 @@ push_wal_file(const char *from_path, const char *to_path, bool is_compress,
178178
{
179179
snprintf(to_path_temp,sizeof(to_path_temp),"%s.partial",gz_to_path);
180180

181-
gz_out=fio_gzopen(to_path_temp,PG_BINARY_W,instance_config.compress_level,FIO_BACKUP_HOST);
181+
gz_out=fio_gzopen(to_path_temp,PG_BINARY_W,instance_config.compress_level,FIO_BACKUP_HOST,
182+
instance_config.encryption);
182183
if (gz_out==NULL)
183184
elog(ERROR,"Cannot open destination temporary WAL file \"%s\": %s",
184185
to_path_temp,strerror(errno));
185186
}
186187
else
187188
#endif
188189
{
190+
intout_fd=-1;
189191
snprintf(to_path_temp,sizeof(to_path_temp),"%s.partial",to_path);
190192

191-
out=fio_open(to_path_temp,O_RDWR |O_CREAT |O_EXCL |PG_BINARY,FIO_BACKUP_HOST);
192-
if (out<0)
193+
out_fd=fio_open(to_path_temp,O_RDWR |O_CREAT |O_EXCL |PG_BINARY,FIO_BACKUP_HOST);
194+
if (out_fd<0)
193195
elog(ERROR,"Cannot open destination temporary WAL file \"%s\": %s",
194196
to_path_temp,strerror(errno));
197+
out=fio_fdopen(to_path_temp,out_fd,PG_BINARY_W,instance_config.encryption);
195198
}
196199

197200
/* copy content */
@@ -226,7 +229,8 @@ push_wal_file(const char *from_path, const char *to_path, bool is_compress,
226229
else
227230
#endif
228231
{
229-
if (fio_write(out,buf,read_len)!=read_len)
232+
ssize_twrite_len=fio_fwrite(out,buf,read_len);
233+
if (write_len!=read_len)
230234
{
231235
errno_temp=errno;
232236
fio_unlink(to_path_temp,FIO_BACKUP_HOST);
@@ -254,7 +258,7 @@ push_wal_file(const char *from_path, const char *to_path, bool is_compress,
254258
else
255259
#endif
256260
{
257-
if (fio_flush(out)!=0||fio_close(out)!=0)
261+
if (fio_fflush(out)!=0&&fio_fclose(out)!=0)
258262
{
259263
errno_temp=errno;
260264
fio_unlink(to_path_temp,FIO_BACKUP_HOST);
@@ -332,7 +336,7 @@ get_wal_file(const char *from_path, const char *to_path)
332336
/* open file for read */
333337
if (!is_decompress)
334338
{
335-
in=fio_fopen(from_path,PG_BINARY_R,FIO_BACKUP_HOST);
339+
in=fio_fopen(from_path,PG_BINARY_R,FIO_BACKUP_HOST,instance_config.encryption);
336340
if (in==NULL)
337341
elog(ERROR,"Cannot open source WAL file \"%s\": %s",
338342
from_path,strerror(errno));
@@ -341,7 +345,7 @@ get_wal_file(const char *from_path, const char *to_path)
341345
else
342346
{
343347
gz_in=fio_gzopen(gz_from_path,PG_BINARY_R,Z_DEFAULT_COMPRESSION,
344-
FIO_BACKUP_HOST);
348+
FIO_BACKUP_HOST,instance_config.encryption);
345349
if (gz_in==NULL)
346350
elog(ERROR,"Cannot open compressed WAL file \"%s\": %s",
347351
gz_from_path,strerror(errno));
@@ -496,7 +500,7 @@ fileEqualCRC(const char *path1, const char *path2, bool path2_is_compressed)
496500
gzFilegz_in=NULL;
497501

498502
INIT_FILE_CRC32(true,crc2);
499-
gz_in=fio_gzopen(path2,PG_BINARY_R,Z_DEFAULT_COMPRESSION,FIO_BACKUP_HOST);
503+
gz_in=fio_gzopen(path2,PG_BINARY_R,Z_DEFAULT_COMPRESSION,FIO_BACKUP_HOST,instance_config.encryption);
500504
if (gz_in==NULL)
501505
/* File cannot be read */
502506
elog(ERROR,

‎src/backup.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1786,7 +1786,7 @@ pg_stop_backup(pgBackup *backup, PGconn *pg_startbackup_conn)
17861786

17871787
/* Write backup_label */
17881788
join_path_components(backup_label,path,PG_BACKUP_LABEL_FILE);
1789-
fp=fio_fopen(backup_label,PG_BINARY_W,FIO_BACKUP_HOST);
1789+
fp=fio_fopen(backup_label,PG_BINARY_W,FIO_BACKUP_HOST, false);
17901790
if (fp==NULL)
17911791
elog(ERROR,"can't open backup label file \"%s\": %s",
17921792
backup_label,strerror(errno));
@@ -1836,7 +1836,7 @@ pg_stop_backup(pgBackup *backup, PGconn *pg_startbackup_conn)
18361836
chartablespace_map[MAXPGPATH];
18371837

18381838
join_path_components(tablespace_map,path,PG_TABLESPACE_MAP_FILE);
1839-
fp=fio_fopen(tablespace_map,PG_BINARY_W,FIO_BACKUP_HOST);
1839+
fp=fio_fopen(tablespace_map,PG_BINARY_W,FIO_BACKUP_HOST, false);
18401840
if (fp==NULL)
18411841
elog(ERROR,"can't open tablespace map file \"%s\": %s",
18421842
tablespace_map,strerror(errno));

‎src/catalog.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ write_backup(pgBackup *backup)
601601
pgBackupGetPath(backup,path,lengthof(path),BACKUP_CONTROL_FILE);
602602
snprintf(path_temp,sizeof(path_temp),"%s.tmp",path);
603603

604-
fp=fio_fopen(path_temp,PG_BINARY_W,FIO_BACKUP_HOST);
604+
fp=fio_fopen(path_temp,PG_BINARY_W,FIO_BACKUP_HOST, false);
605605
if (fp==NULL)
606606
elog(ERROR,"Cannot open configuration file \"%s\": %s",
607607
path_temp,strerror(errno));
@@ -640,7 +640,7 @@ write_backup_filelist(pgBackup *backup, parray *files, const char *root,
640640
pgBackupGetPath(backup,path,lengthof(path),DATABASE_FILE_LIST);
641641
snprintf(path_temp,sizeof(path_temp),"%s.tmp",path);
642642

643-
fp=fio_fopen(path_temp,PG_BINARY_W,FIO_BACKUP_HOST);
643+
fp=fio_fopen(path_temp,PG_BINARY_W,FIO_BACKUP_HOST, false);
644644
if (fp==NULL)
645645
elog(ERROR,"Cannot open file list \"%s\": %s",path_temp,
646646
strerror(errno));

‎src/configure.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ static void show_configure_json(ConfigOption *opt);
4040
#defineOPTION_RETENTION_GROUP"Retention parameters"
4141
#defineOPTION_COMPRESS_GROUP"Compression parameters"
4242
#defineOPTION_REMOTE_GROUP"Remote access parameters"
43+
#defineOPTION_ENCRYPTION_GROUP "Encryption parameters"
4344

4445
/*
4546
* Short name should be non-printable ASCII character.
@@ -216,6 +217,12 @@ ConfigOption instance_options[] =
216217
&instance_config.remote.ssh_config,SOURCE_CMD,0,
217218
OPTION_REMOTE_GROUP,0,option_get_value
218219
},
220+
/* Encryption options */
221+
{
222+
'b',226,"encryption",
223+
&instance_config.encryption,SOURCE_CMD,0,
224+
OPTION_ENCRYPTION_GROUP,0,option_get_value
225+
},
219226
{0 }
220227
};
221228

‎src/data.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ backup_data_file(backup_files_arg* arguments,
559559
INIT_FILE_CRC32(true,file->crc);
560560

561561
/* open backup mode file for read */
562-
in=fio_fopen(file->path,PG_BINARY_R,FIO_DB_HOST);
562+
in=fio_fopen(file->path,PG_BINARY_R,FIO_DB_HOST, false);
563563
if (in==NULL)
564564
{
565565
FIN_FILE_CRC32(true,file->crc);
@@ -598,7 +598,7 @@ backup_data_file(backup_files_arg* arguments,
598598
nblocks=file->size/BLCKSZ;
599599

600600
/* open backup file for write */
601-
out=fio_fopen(to_path,PG_BINARY_W,FIO_BACKUP_HOST);
601+
out=fio_fopen(to_path,PG_BINARY_W,FIO_BACKUP_HOST,instance_config.encryption);
602602
if (out==NULL)
603603
{
604604
interrno_tmp=errno;
@@ -740,7 +740,7 @@ restore_data_file(const char *to_path, pgFile *file, bool allow_truncate,
740740
* modified pages for differential restore. If the file does not exist,
741741
* re-open it with "w" to create an empty file.
742742
*/
743-
out=fio_fopen(to_path,PG_BINARY_R"+",FIO_DB_HOST);
743+
out=fio_fopen(to_path,PG_BINARY_R"+",FIO_DB_HOST, false);
744744
if (out==NULL)
745745
{
746746
interrno_tmp=errno;
@@ -964,7 +964,7 @@ copy_file(fio_location from_location, const char *to_root,
964964
file->write_size=0;
965965

966966
/* open backup mode file for read */
967-
in=fio_fopen(file->path,PG_BINARY_R,from_location);
967+
in=fio_fopen(file->path,PG_BINARY_R,from_location,instance_config.encryption&&from_location==FIO_BACKUP_HOST);
968968
if (in==NULL)
969969
{
970970
FIN_FILE_CRC32(true,crc);
@@ -989,7 +989,7 @@ copy_file(fio_location from_location, const char *to_root,
989989

990990
/* open backup file for write */
991991
join_path_components(to_path,to_root,file->rel_path);
992-
out=fio_fopen(to_path,PG_BINARY_W,to_location);
992+
out=fio_fopen(to_path,PG_BINARY_W,to_location,instance_config.encryption&&to_location==FIO_BACKUP_HOST);
993993
if (out==NULL)
994994
{
995995
interrno_tmp=errno;

‎src/dir.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ pgFileGetCRC(const char *file_path, bool use_crc32c, bool raise_on_deleted,
263263
INIT_FILE_CRC32(use_crc32c,crc);
264264

265265
/* open file in binary read mode */
266-
fp=fio_fopen(file_path,PG_BINARY_R,location);
266+
fp=fio_fopen(file_path,PG_BINARY_R,location,location==FIO_BACKUP_HOST&&instance_config.encryption);
267267
if (fp==NULL)
268268
{
269269
if (!raise_on_deleted&&errno==ENOENT)

‎src/parsexlog.c

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ typedef struct XLogReaderData
108108

109109
boolneed_switch;
110110

111-
intxlogfile;
111+
FILE*xlogfile;
112112
charxlogpath[MAXPGPATH];
113113

114114
#ifdefHAVE_LIBZ
@@ -720,20 +720,23 @@ SimpleXLogPageRead(XLogReaderState *xlogreader, XLogRecPtr targetPagePtr,
720720

721721
if (fileExists(reader_data->xlogpath,FIO_BACKUP_HOST))
722722
{
723+
intfd;
723724
elog(LOG,"Thread [%d]: Opening WAL segment \"%s\"",
724725
reader_data->thread_num,reader_data->xlogpath);
725726

726727
reader_data->xlogexists= true;
727-
reader_data->xlogfile=fio_open(reader_data->xlogpath,
728+
fd=fio_open(reader_data->xlogpath,
728729
O_RDONLY |PG_BINARY,FIO_BACKUP_HOST);
729730

730-
if (reader_data->xlogfile<0)
731+
if (fd<0)
731732
{
732733
elog(WARNING,"Thread [%d]: Could not open WAL segment \"%s\": %s",
733734
reader_data->thread_num,reader_data->xlogpath,
734735
strerror(errno));
735736
return-1;
736737
}
738+
reader_data->xlogfile=fio_fdopen(reader_data->xlogpath,fd,"rb",
739+
instance_config.encryption);
737740
}
738741
#ifdefHAVE_LIBZ
739742
/* Try to open compressed WAL segment */
@@ -748,7 +751,8 @@ SimpleXLogPageRead(XLogReaderState *xlogreader, XLogRecPtr targetPagePtr,
748751

749752
reader_data->xlogexists= true;
750753
reader_data->gz_xlogfile=fio_gzopen(reader_data->gz_xlogpath,
751-
"rb",-1,FIO_BACKUP_HOST);
754+
"rb",-1,FIO_BACKUP_HOST,
755+
instance_config.encryption);
752756
if (reader_data->gz_xlogfile==NULL)
753757
{
754758
elog(WARNING,"Thread [%d]: Could not open compressed WAL segment \"%s\": %s",
@@ -782,16 +786,16 @@ SimpleXLogPageRead(XLogReaderState *xlogreader, XLogRecPtr targetPagePtr,
782786
}
783787

784788
/* Read the requested page */
785-
if (reader_data->xlogfile!=-1)
789+
if (reader_data->xlogfile!=NULL)
786790
{
787-
if (fio_seek(reader_data->xlogfile, (off_t)targetPageOff)<0)
791+
if (fio_fseek(reader_data->xlogfile, (off_t)targetPageOff)<0)
788792
{
789793
elog(WARNING,"Thread [%d]: Could not seek in WAL segment \"%s\": %s",
790794
reader_data->thread_num,reader_data->xlogpath,strerror(errno));
791795
return-1;
792796
}
793797

794-
if (fio_read(reader_data->xlogfile,readBuf,XLOG_BLCKSZ)!=XLOG_BLCKSZ)
798+
if (fio_fread(reader_data->xlogfile,readBuf,XLOG_BLCKSZ)!=XLOG_BLCKSZ)
795799
{
796800
elog(WARNING,"Thread [%d]: Could not read from WAL segment \"%s\": %s",
797801
reader_data->thread_num,reader_data->xlogpath,strerror(errno));
@@ -842,7 +846,7 @@ InitXLogPageRead(XLogReaderData *reader_data, const char *archivedir,
842846

843847
MemSet(reader_data,0,sizeof(XLogReaderData));
844848
reader_data->tli=tli;
845-
reader_data->xlogfile=-1;
849+
reader_data->xlogfile=NULL;
846850

847851
if (allocate_reader)
848852
{
@@ -1365,10 +1369,10 @@ CleanupXLogPageRead(XLogReaderState *xlogreader)
13651369
XLogReaderData*reader_data;
13661370

13671371
reader_data= (XLogReaderData*)xlogreader->private_data;
1368-
if (reader_data->xlogfile>=0)
1372+
if (reader_data->xlogfile!=NULL)
13691373
{
1370-
fio_close(reader_data->xlogfile);
1371-
reader_data->xlogfile=-1;
1374+
fio_fclose(reader_data->xlogfile);
1375+
reader_data->xlogfile=NULL;
13721376
}
13731377
#ifdefHAVE_LIBZ
13741378
elseif (reader_data->gz_xlogfile!= NULL)
@@ -1393,7 +1397,7 @@ PrintXLogCorruptionMsg(XLogReaderData *reader_data, int elevel)
13931397
if (!reader_data->xlogexists)
13941398
elog(elevel,"Thread [%d]: WAL segment \"%s\" is absent",
13951399
reader_data->thread_num,reader_data->xlogpath);
1396-
elseif (reader_data->xlogfile!=-1)
1400+
elseif (reader_data->xlogfile!=NULL)
13971401
elog(elevel,"Thread [%d]: Possible WAL corruption. "
13981402
"Error has occured during reading WAL segment \"%s\"",
13991403
reader_data->thread_num,reader_data->xlogpath);

‎src/pg_probackup.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ booldry_run = false;
111111
/* compression options */
112112
boolcompress_shortcut= false;
113113

114+
/* encryption options */
115+
boolencryption_shortcut= false;
116+
114117
/* other options */
115118
char*instance_name;
116119

@@ -193,6 +196,8 @@ static ConfigOption cmd_options[] =
193196
{'b',152,"overwrite",&file_overwrite,SOURCE_CMD_STRICT },
194197
/* show options */
195198
{'f',153,"format",opt_show_format,SOURCE_CMD_STRICT },
199+
/* encryption options */
200+
{'b',154,"encryption",&encryption_shortcut,SOURCE_CMD_STRICT },
196201

197202
/* options for backward compatibility */
198203
{'s',136,"time",&target_time,SOURCE_CMD_STRICT },
@@ -596,6 +601,8 @@ main(int argc, char *argv[])
596601
num_threads=1;
597602

598603
compress_init();
604+
if (instance_config.encryption)
605+
fio_crypto_init();
599606

600607
/* do actual operation */
601608
switch (backup_subcmd)
@@ -717,6 +724,9 @@ compress_init(void)
717724
if (compress_shortcut)
718725
instance_config.compress_alg=ZLIB_COMPRESS;
719726

727+
if (encryption_shortcut)
728+
instance_config.encryption=encryption_shortcut;
729+
720730
if (backup_subcmd!=SET_CONFIG_CMD)
721731
{
722732
if (instance_config.compress_level!=COMPRESS_LEVEL_DEFAULT

‎src/pg_probackup.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ typedef struct pgFile
141141
boolis_database;
142142
intexternal_dir_num;/* Number of external directory. 0 if not external */
143143
boolexists_in_prev;/* Mark files, both data and regular, that exists in previous backup */
144+
boolencryption;/* encrypt backup */
144145
CompressAlgcompress_alg;/* compression algorithm applied to the file */
145146
volatilepg_atomic_flaglock;/* lock for synchronization of parallel threads */
146147
datapagemap_tpagemap;/* bitmap of pages updated since previous backup */
@@ -237,6 +238,8 @@ typedef struct InstanceConfig
237238

238239
CompressAlgcompress_alg;
239240
intcompress_level;
241+
242+
boolencryption;
240243
}InstanceConfig;
241244

242245
externConfigOptioninstance_options[];

‎src/restore.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,7 @@ create_recovery_conf(time_t backup_id,
789789
elog(LOG,"creating recovery.conf");
790790

791791
snprintf(path,lengthof(path),"%s/recovery.conf",instance_config.pgdata);
792-
fp=fio_fopen(path,"wt",FIO_DB_HOST);
792+
fp=fio_fopen(path,"wt",FIO_DB_HOST, false);
793793
if (fp==NULL)
794794
elog(ERROR,"cannot open recovery.conf \"%s\": %s",path,
795795
strerror(errno));

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp