@@ -248,6 +248,14 @@ def deep_update(a, b):
248248 recovery_target_inclusive: false
249249 {{/CLONE_TARGET_INCLUSIVE}}
250250 {{/CLONE_WITH_WALE}}
251+ {{#CLONE_WITH_PGBACKREST}}
252+ method: clone_with_pgbackrest
253+ clone_with_pgbackrest:
254+ command: python3 /scripts/clone_with_pgbackrest.py
255+ --recovery-target-time="{{CLONE_TARGET_TIME}}"
256+ recovery_conf:
257+ restore_command: pgbackrest --stanza=db archive-get %f "%p"
258+ {{/CLONE_WITH_PGBACKREST}}
251259 {{#CLONE_WITH_BASEBACKUP}}
252260 method: clone_with_basebackup
253261 clone_with_basebackup:
@@ -560,7 +568,8 @@ def get_placeholders(provider):
560568
561569 placeholders .setdefault ('PGHOME' , os .path .expanduser ('~' ))
562570 placeholders .setdefault ('APIPORT' , '8008' )
563- placeholders .setdefault ('BACKUP_SCHEDULE' , '0 1 * * *' )
571+ placeholders .setdefault ('BACKUP_SCHEDULE' , '0 1 * * SAT' )
572+ placeholders .setdefault ('BACKUP_SCHEDULE_INCREMENTAL' , '0 1 * * *' )
564573 placeholders .setdefault ('BACKUP_NUM_TO_RETAIN' , '5' )
565574 placeholders .setdefault ('CRONTAB' , '[]' )
566575 placeholders .setdefault ('PGROOT' , os .path .join (placeholders ['PGHOME' ], 'pgroot' ))
@@ -620,6 +629,7 @@ def get_placeholders(provider):
620629 placeholders .setdefault ('USE_PAUSE_AT_RECOVERY_TARGET' , False )
621630 placeholders .setdefault ('CLONE_METHOD' , '' )
622631 placeholders .setdefault ('CLONE_WITH_WALE' , '' )
632+ placeholders .setdefault ('CLONE_WITH_PGBACKREST' , '' )
623633 placeholders .setdefault ('CLONE_WITH_BASEBACKUP' , '' )
624634 placeholders .setdefault ('CLONE_TARGET_TIME' , '' )
625635 placeholders .setdefault ('CLONE_TARGET_INCLUSIVE' , True )
@@ -1008,16 +1018,28 @@ def write_clone_pgpass(placeholders, overwrite):
10081018
10091019def check_crontab (user ):
10101020 with open (os .devnull , 'w' ) as devnull :
1011- cron_exit = subprocess .call (['crontab' , '-lu' , user ], stdout = devnull , stderr = devnull )
1012- if cron_exit == 0 :
1013- return logging .warning ('Cron for %s is already configured. (Use option --force to overwrite cron)' , user )
1021+ try :
1022+ cron_exit = subprocess .call (['crontab' , '-lu' , user ], stdout = devnull , stderr = devnull )
1023+ if cron_exit == 0 :
1024+ return logging .warning ('Cron for %s is already configured. (Use option --force to overwrite cron)' , user )
1025+ except :
1026+ logging .error ('We were not able to add cron for user %s. Is cron enabled during build?' , user )
10141027 return True
10151028
10161029
10171030def setup_crontab (user , lines ):
10181031 lines += ['' ] # EOF requires empty line for cron
1019- c = subprocess .Popen (['crontab' , '-u' , user , '-' ], stdin = subprocess .PIPE )
1020- c .communicate (input = '\n ' .join (lines ).encode ())
1032+ c = subprocess .Popen (['crontab' , '-u' , user , '-' ], stdin = subprocess .PIPE , stdout = subprocess .PIPE , stderr = subprocess .PIPE )
1033+ stdout , stderr = c .communicate (input = '\n ' .join (lines ).encode ())
1034+ if stderr :
1035+ return logging .error ('Error while adding a crontab: %s' , stderr )
1036+
1037+ def setup_crontab_postgres (lines ):
1038+ lines += ['' ] # EOF requires empty line for cron
1039+ c = subprocess .Popen (['crontab' ,'-' ], stdin = subprocess .PIPE , stdout = subprocess .PIPE , stderr = subprocess .PIPE )
1040+ stdout , stderr = c .communicate (input = '\n ' .join (lines ).encode ())
1041+ if stderr :
1042+ return logging .error ('Error while adding a crontab for user postgres: %s' , stderr )
10211043
10221044
10231045def setup_runit_cron (placeholders ):
@@ -1063,6 +1085,12 @@ def write_crontab(placeholders, overwrite):
10631085 hash_dir = os .path .join (placeholders ['RW_DIR' ], 'tmp' )
10641086 lines += ['*/5 * * * * {0} /scripts/test_reload_ssl.sh {1}' .format (env , hash_dir )]
10651087
1088+
1089+ if bool (placeholders .get ('USE_PGBACKREST' )) and not USE_KUBERNETES :
1090+ lines += [('{BACKUP_SCHEDULE} /usr/bin/pgbackrest --stanza=db --type=full backup' ).format (** placeholders )]
1091+ lines += [('{BACKUP_SCHEDULE_INCREMENTAL} /usr/bin/pgbackrest --stanza=db --type=incr backup' ).format (** placeholders )]
1092+
1093+
10661094 if bool (placeholders .get ('USE_WALE' )):
10671095 lines += [('{BACKUP_SCHEDULE} envdir "{WALE_ENV_DIR}" /scripts/postgres_backup.sh' +
10681096 ' "{PGDATA}"' ).format (** placeholders )]
@@ -1077,7 +1105,10 @@ def write_crontab(placeholders, overwrite):
10771105 setup_runit_cron (placeholders )
10781106
10791107 if len (lines ) > 1 and (overwrite or check_crontab ('postgres' )):
1080- setup_crontab ('postgres' , lines )
1108+ try :
1109+ setup_crontab_postgres (lines )
1110+ except :
1111+ logging .error ('Unable to add crontab, is cron as service enabled during build? ' )
10811112
10821113 if root_lines and (overwrite or check_crontab ('root' )):
10831114 setup_crontab ('root' , root_lines )
0 commit comments