1
+ /*-------------------------------------------------------------------------
2
+ *
3
+ * archive.c: - pg_probackup specific archive commands for archive backups.
4
+ *
5
+ *
6
+ * Portions Copyright (c) 2017, Postgres Professional
7
+ *
8
+ *-------------------------------------------------------------------------
9
+ */
10
+ #include "pg_probackup.h"
11
+
12
+ #include <unistd.h>
13
+ #include <sys/stat.h>
14
+
15
+ /*
16
+ * pg_probackup specific archive command for archive backups
17
+ * set archive_command = 'pg_probackup archive-push -B /home/anastasia/backup
18
+ * --wal-file-path %p --wal-file-name %f', to move backups into arclog_path.
19
+ * Where archlog_path is $BACKUP_PATH/wal/system_id.
20
+ * Currently it just copies wal files to the new location.
21
+ * TODO Planned options: compress, list the arclog content,
22
+ * compute and validate checksums.
23
+ */
24
+ int
25
+ do_archive_push (char * wal_file_path ,char * wal_file_name )
26
+ {
27
+ char backup_wal_file_path [MAXPGPATH ];
28
+ char absolute_wal_file_path [MAXPGPATH ];
29
+ char current_dir [MAXPGPATH ];
30
+ int64 system_id ;
31
+ pgBackupConfig * config ;
32
+
33
+ if (!getcwd (current_dir ,sizeof (current_dir )))
34
+ elog (ERROR ,"getcwd() error" );
35
+
36
+ /* verify that archive-push --instance parameter is valid */
37
+ config = readBackupCatalogConfigFile ();
38
+ system_id = get_system_identifier (current_dir );
39
+
40
+ if (config -> pgdata == NULL )
41
+ elog (ERROR ,"cannot read pg_probackup.conf for this instance" );
42
+
43
+ if (system_id != config -> system_identifier )
44
+ elog (ERROR ,"Refuse to push WAL segment %s into archive. Instance parameters mismatch."
45
+ "Instance '%s' should have SYSTEM_ID = %ld instead of %ld" ,
46
+ wal_file_name ,instance_name ,config -> system_identifier ,system_id );
47
+
48
+ if (strcmp (current_dir ,config -> pgdata )!= 0 )
49
+ elog (ERROR ,"Refuse to push WAL segment %s into archive. Instance parameters mismatch."
50
+ "Instance '%s' should have PGDATA = %s instead of %s" ,
51
+ wal_file_name ,instance_name ,config -> pgdata ,current_dir );
52
+
53
+ /* Create 'archlog_path' directory. Do nothing if it already exists. */
54
+ dir_create_dir (arclog_path ,DIR_PERMISSION );
55
+
56
+ join_path_components (absolute_wal_file_path ,current_dir ,wal_file_path );
57
+ join_path_components (backup_wal_file_path ,arclog_path ,wal_file_name );
58
+
59
+ elog (INFO ,"pg_probackup archive-push from %s to %s" ,absolute_wal_file_path ,backup_wal_file_path );
60
+ copy_wal_file (absolute_wal_file_path ,backup_wal_file_path );
61
+ elog (INFO ,"pg_probackup archive-push completed successfully" );
62
+
63
+ return 0 ;
64
+ }
65
+
66
+ /*
67
+ * pg_probackup specific restore command.
68
+ * Move files from arclog_path to pgdata/wal_file_path.
69
+ */
70
+ int
71
+ do_archive_get (char * wal_file_path ,char * wal_file_name )
72
+ {
73
+ char backup_wal_file_path [MAXPGPATH ];
74
+ char absolute_wal_file_path [MAXPGPATH ];
75
+ char current_dir [MAXPGPATH ];
76
+
77
+ if (!getcwd (current_dir ,sizeof (current_dir )))
78
+ elog (ERROR ,"getcwd() error" );
79
+
80
+ join_path_components (absolute_wal_file_path ,current_dir ,wal_file_path );
81
+ join_path_components (backup_wal_file_path ,arclog_path ,wal_file_name );
82
+
83
+ elog (INFO ,"pg_probackup archive-get from %s to %s" ,backup_wal_file_path ,absolute_wal_file_path );
84
+ copy_wal_file (backup_wal_file_path ,absolute_wal_file_path );
85
+ elog (INFO ,"pg_probackup archive-get completed successfully" );
86
+
87
+ return 0 ;
88
+ }