1010
1111#include "pg_probackup.h"
1212
13- #include <stdio.h>
14- #include <stdlib.h>
15- #include <string.h>
16- #include <sys/stat.h>
17- #include <sys/time.h>
18- #include <unistd.h>
19- #include <dirent.h>
20- #include <time.h>
21-
13+ #if PG_VERSION_NUM < 110000
2214#include "catalog/catalog.h"
15+ #endif
2316#include "catalog/pg_tablespace.h"
24- #include "datapagemap.h"
25- #include "libpq/pqsignal.h"
2617#include "pgtar.h"
2718#include "receivelog.h"
28- #include "storage/bufpage.h"
2919#include "streamutil.h"
20+
21+ #include <sys/stat.h>
22+ #include <unistd.h>
23+
3024#include "utils/thread.h"
3125
26+ #define PG_STOP_BACKUP_TIMEOUT 300
27+
28+ /*
29+ * Macro needed to parse ptrack.
30+ * NOTE Keep those values syncronised with definitions in ptrack.h
31+ */
32+ #define PTRACK_BITS_PER_HEAPBLOCK 1
33+ #define HEAPBLOCKS_PER_BYTE (BITS_PER_BYTE / PTRACK_BITS_PER_HEAPBLOCK)
34+
3235static int standby_message_timeout = 10 * 1000 ; /* 10 sec = default */
3336static XLogRecPtr stop_backup_lsn = InvalidXLogRecPtr ;
3437static XLogRecPtr stop_stream_lsn = InvalidXLogRecPtr ;
@@ -531,7 +534,7 @@ do_backup_instance(void)
531534 prev_backup_start_lsn = prev_backup -> start_lsn ;
532535 current .parent_backup = prev_backup -> start_time ;
533536
534- pgBackupWriteBackupControlFile (& current );
537+ write_backup (& current );
535538 }
536539
537540 /*
@@ -906,11 +909,13 @@ do_backup(time_t start_time)
906909 /* Start backup. Update backup status. */
907910 current .status = BACKUP_STATUS_RUNNING ;
908911 current .start_time = start_time ;
912+ StrNCpy (current .program_version , PROGRAM_VERSION ,
913+ sizeof (current .program_version ));
909914
910915 /* Create backup directory and BACKUP_CONTROL_FILE */
911916 if (pgBackupCreateDir (& current ))
912917 elog (ERROR , "cannot create backup directory" );
913- pgBackupWriteBackupControlFile (& current );
918+ write_backup (& current );
914919
915920 elog (LOG , "Backup destination is initialized" );
916921
@@ -932,7 +937,7 @@ do_backup(time_t start_time)
932937 /* Backup is done. Update backup status */
933938 current .end_time = time (NULL );
934939 current .status = BACKUP_STATUS_DONE ;
935- pgBackupWriteBackupControlFile (& current );
940+ write_backup (& current );
936941
937942 //elog(LOG, "Backup completed. Total bytes : " INT64_FORMAT "",
938943 // current.data_bytes);
@@ -2010,7 +2015,7 @@ backup_cleanup(bool fatal, void *userdata)
20102015 base36enc (current .start_time ));
20112016 current .end_time = time (NULL );
20122017 current .status = BACKUP_STATUS_ERROR ;
2013- pgBackupWriteBackupControlFile (& current );
2018+ write_backup (& current );
20142019 }
20152020
20162021 /*
@@ -2096,12 +2101,13 @@ backup_files(void *arg)
20962101
20972102 if (S_ISREG (buf .st_mode ))
20982103 {
2104+ pgFile * * prev_file ;
2105+
20992106 /* Check that file exist in previous backup */
21002107 if (current .backup_mode != BACKUP_MODE_FULL )
21012108 {
21022109 char * relative ;
21032110 pgFile key ;
2104- pgFile * * prev_file ;
21052111
21062112 relative = GetRelativePath (file -> path , arguments -> from_root );
21072113 key .path = relative ;
@@ -2131,20 +2137,27 @@ backup_files(void *arg)
21312137 continue ;
21322138 }
21332139 }
2134- /* TODO:
2135- * Check if file exists in previous backup
2136- * If exists:
2137- * if mtime > start_backup_time of parent backup,
2138- * copy file to backup
2139- * if mtime < start_backup_time
2140- * calculate crc, compare crc to old file
2141- * if crc is the same -> skip file
2142- */
2143- else if (!copy_file (arguments -> from_root , arguments -> to_root , file ))
2140+ else
21442141 {
2145- file -> write_size = BYTES_INVALID ;
2146- elog (VERBOSE , "File \"%s\" was not copied to backup" , file -> path );
2147- continue ;
2142+ bool skip = false;
2143+
2144+ /* If non-data file has not changed since last backup... */
2145+ if (file -> exists_in_prev &&
2146+ buf .st_mtime < current .parent_backup )
2147+ {
2148+ calc_file_checksum (file );
2149+ /* ...and checksum is the same... */
2150+ if (EQ_CRC32C (file -> crc , (* prev_file )-> crc ))
2151+ skip = true; /* ...skip copying file. */
2152+ }
2153+ if (skip ||
2154+ !copy_file (arguments -> from_root , arguments -> to_root , file ))
2155+ {
2156+ file -> write_size = BYTES_INVALID ;
2157+ elog (VERBOSE , "File \"%s\" was not copied to backup" ,
2158+ file -> path );
2159+ continue ;
2160+ }
21482161 }
21492162
21502163 elog (VERBOSE , "File \"%s\". Copied " INT64_FORMAT " bytes" ,
0 commit comments