§
    ôcJ)  ã                   ó�   — d dl Z d dlZd dlZd dlmZmZmZ d dlmZ  ej	        e
¦  «        Zd„ Zd„ ZdZd„ Zd„ Zd	„ Z	 dd
„Zd„ ZdS )é    N)Úexecute_commandÚ"execute_command_and_capture_outputÚexecute_command_with_processes)Údumpc                 óR   — t          j        |                      d¦  «        d¦  «        S )z]
    Make the dump path from the given location configuration and the name of this hook.
    Úborgmatic_source_directoryÚpostgresql_databases)r   Úmake_database_dump_pathÚget)Úlocation_configs    ú</usr/lib/python3/dist-packages/borgmatic/hooks/postgresql.pyÚmake_dump_pathr      s.   € õ Ô'Ø×ÒÐ8Ñ9Ô9Ð;Qñô ð ó    c                 óê   — t          ¦   «         }d| v r| d         |d<   |                      dd¦  «        |d<   d| v r| d         |d<   d| v r| d         |d	<   d
| v r| d
         |d<   d| v r| d         |d<   |S )zP
    Make the extra_environment dict from the given database configuration.
    ÚpasswordÚ
PGPASSWORDÚssl_modeÚdisableÚ	PGSSLMODEÚssl_certÚ	PGSSLCERTÚssl_keyÚPGSSLKEYÚssl_root_certÚPGSSLROOTCERTÚssl_crlÚPGSSLCRL)Údictr   )ÚdatabaseÚextras     r   Úmake_extra_environmentr!      s¬   € õ ‰FŒF€EØ�XÐÐØ& zÔ2ˆˆlÑØ!Ÿš j°)Ñ<Ô<€Eˆ+ÑØ�XÐÐØ% jÔ1ˆˆkÑØ�HÐÐØ$ YÔ/ˆˆjÑØ˜(Ð"Ð"Ø!)¨/Ô!:ˆˆoÑØ�HÐÐØ$ YÔ/ˆˆjÑØ€Lr   )Ú	template0Ú	template1c                 ó2  — | d         }|dk    r|fS |                       d¦  «        sdS dd| v r
d| d         fndz   d	| v rd
t          | d	         ¦  «        fndz   d| v r
d| d         fndz   d| v r(t          | d                              d¦  «        ¦  «        ndz   }t                               d                     ||¦  «        ¦  «         t          ||¬¦  «        }t          d„ t          j	        | 
                    ¦   «         dd¬¦  «        D ¦   «         ¦  «        S )aB  
    Given a requested database config, return the corresponding sequence of database names to dump.
    In the case of "all" when a database format is given, query for the names of databases on the
    configured host and return them. For "all" without a database format, just return a sequence
    containing "all".
    ÚnameÚallÚformat)r&   )Úpsqlz--listú--no-passwordz--csvz--tuples-onlyÚhostnameú--host© Úportú--portÚusernameú
--usernameÚlist_optionsú z5{}: Querying for "all" PostgreSQL databases to dump{}©Úextra_environmentc              3   óD   K  — | ]}|d          t           v¯|d          V — ŒdS )r   N)ÚEXCLUDED_DATABASE_NAMES)Ú.0Úrows     r   ú	<genexpr>z)database_names_to_dump.<locals>.<genexpr>J   sB   è è € ð ð àØˆqŒ6Õ0Ð0Ð0ð 	ˆAŒà0Ð0Ð0Ð0ðð r   ú,ú")Ú	delimiterÚ	quotechar)r   ÚstrÚtupleÚsplitÚloggerÚdebugr'   r   ÚcsvÚreaderÚ
splitlines)r   r4   Ú
log_prefixÚdry_run_labelÚrequested_nameÚlist_commandÚlist_outputs          r   Údatabase_names_to_dumprK   .   sƒ  € ð ˜fÔ%€Nà˜ÒÐØÐ Ð Ø�<Š<˜Ñ!Ô!ð Øˆxð 	FØ/9¸XÐ/EÐ/EˆH�h˜zÔ*Ð+Ð+È2ñ	Oà06¸(Ð0BÐ0BˆH•c˜( 6Ô*Ñ+Ô+Ð,Ð,Èñ	Lð 4>ÀÐ3IÐ3IˆL˜( :Ô.Ð/Ð/Èrñ	Sð :HÈ8Ð9SÐ9S�5�˜.Ô)×/Ò/°Ñ4Ô4Ñ5Ô5Ð5ÐY[ñ		]ð õ ‡L‚LØ?×FÒFÀzÐS`ÑaÔañô ð õ 5ØÐ(9ðñ ô €Kõ ð ð å”:˜k×4Ò4Ñ6Ô6À#ÐQTÐUÑUÔUðñ ô ñ ô ð r   c           
      óÌ  — |rdnd}g }t                                d                     ||¦  «        ¦  «         | D �])}t          |¦  «        }t	          |¦  «        }t          ||||¦  «        }	|	st          d¦  «        ‚|	D �]á}
|                     d|
dk    rdnd¦  «        }|
dk    rd	nd
}|                     d¦  «        p|}t          j	        ||
|                     d¦  «        ¦  «        }t          j                             |¦  «        r#t                                |› d|
› d|› �¦  «         Œ­|dddfd|v r
d|d         fndz   d|v rdt          |d         ¦  «        fndz   d|v r
d|d         fndz   |rd|fndz   |dk    rd|fndz   d|v r(t          |d                              d¦  «        ¦  «        ndz   |
dk    rdn|
fz   |dk    rd|fndz   }t                                |› d|
› d|› |› �¦  «         |r�Œy|dk    r(t          j        |¦  «         t'          |d|¬ ¦  «         �Œ§t          j        |¦  «         |                     t'          |d|d!¬"¦  «        ¦  «         �Œã�Œ+|S )#aH  
    Dump the given PostgreSQL databases to a named pipe. The databases are supplied as a sequence of
    dicts, one dict describing each database as per the configuration schema. Use the given log
    prefix in any log entries. Use the given location configuration dict to construct the
    destination path.

    Return a sequence of subprocess.Popen instances for the dump processes ready to spew to a named
    pipe. But if this is a dry run, then don't actually dump anything and return an empty sequence.

    Raise ValueError if the databases to dump cannot be determined.
    z) (dry run; not actually dumping anything)Ú z"{}: Dumping PostgreSQL databases{}z-Cannot find any PostgreSQL databases to dump.r'   r&   NÚcustomÚ
pg_dumpallÚpg_dumpÚpg_dump_commandr*   z2: Skipping duplicate dump of PostgreSQL database "z" to r)   ú--cleanú--if-existsr+   r,   r-   r.   r/   r0   z--formatÚ	directoryz--fileÚoptionsr2   ú>z: Dumping PostgreSQL database "T)Úshellr4   F)rW   r4   Úrun_to_completion)rA   Úinfor'   r!   r   rK   Ú
ValueErrorr   r   Úmake_database_dump_filenameÚosÚpathÚexistsÚwarningr>   r?   r@   rB   Ú create_parent_directory_for_dumpr   Úcreate_named_pipe_for_dumpÚappend)Ú	databasesrF   r   Údry_runrG   Ú	processesr   r4   Ú	dump_pathÚdump_database_namesÚdatabase_nameÚdump_formatÚdefault_dump_commandÚdump_commandÚdump_filenameÚcommands                   r   Údump_databasesrn   Q   s‚  € ð DKÐRÐ?Ð?ÐPR€MØ€Iå
‡K‚KÐ4×;Ò;¸JÈÑVÔVÑWÔWÐWàð :ñ :ˆÝ2°8Ñ<Ô<ÐÝ" ?Ñ3Ô3ˆ	Ý4ØÐ'¨°]ñ
ô 
Ðð #ð 	NÝÐLÑMÔMÐMà0ð 0	ñ 0	ˆMØ"Ÿ,š, x¸È%Ò9OÐ9O°°ÐU]Ñ^Ô^ˆKØ3@ÀEÒ3IÐ3I < <ÈyÐ Ø#Ÿ<š<Ð(9Ñ:Ô:ÐRÐ>RˆLÝ Ô<Ø˜=¨(¯,ª,°zÑ*BÔ*Bñô ˆMõ Œw�~Š~˜mÑ,Ô,ð Ý—’Ø!ÐxÐxÐUbÐxÐxÐivÐxÐxñô ð ð ð ˜°	¸=ÐJØ7AÀXÐ7MÐ7M�H˜h zÔ2Ð3Ð3ÐSUñWà8>À(Ð8JÐ8J�H�c (¨6Ô"2Ñ3Ô3Ð4Ð4ÐPRñTð <FÈÐ;QÐ;Q�L (¨:Ô"6Ð7Ð7ÐWYñ[ð 1<ÐC�J Ð,Ð,Àñ	Eð
 1<¸{Ò0JÐ0J�H˜mÐ,Ð,ÐPRñTð =FÈÐ<QÐ<Q•5˜ )Ô,×2Ò2°3Ñ7Ô7Ñ8Ô8Ð8ÐWYñ[ð '¨%Ò/Ð/�2�2°mÐ5EñGð ,7¸+Ò+EÐ+E�C˜Ð'Ð'È2ñOð õ �LŠLØÐpÐp¸mÐpÐpÐR_ÐpÐanÐpÐpñô ð ð ð Ùà˜kÒ)Ð)ÝÔ5°mÑDÔDÐDÝØ 4Ð;Lðñ ô ð ñ õ Ô/°Ñ>Ô>Ð>Ø× Ò Ý#ØØ"Ø*;Ø*/ð	ñ ô ñô ð ñ ñS0	ðd Ðr   c                 óN   — t          j        t          |¦  «        d||¦  «         dS )a  
    Remove all database dump files for this hook regardless of the given databases. Use the log
    prefix in any log entries. Use the given location configuration dict to construct the
    destination path. If this is a dry run, then don't actually remove anything.
    Ú
PostgreSQLN)r   Úremove_database_dumpsr   )rc   rF   r   rd   s       r   rq   rq   ¡   s*   € õ 	Ô�~¨oÑ>Ô>ÀÈjÐZaÑbÔbÐbÐbÐbr   c                 óJ   — t          j        t          |¦  «        |d¬¦  «        S )zß
    Given a sequence of configurations dicts, a prefix to log with, a location configuration dict,
    and a database name to match, return the corresponding glob patterns to match the database dump
    in an archive.
    Ú*)r*   )r   r[   r   )rc   rF   r   r%   s       r   Úmake_database_dump_patternrt   ª   s%   € õ Ô+­N¸?Ñ,KÔ,KÈTÐ\_Ð`Ñ`Ô`Ð`r   c                 ó¤  — |rdnd}t          | ¦  «        dk    rt          d¦  «        ‚| d         }t          |d         dk    ¦  «        }t          j        t          |¦  «        |d         |                     d¦  «        ¦  «        }|                     d	¦  «        pd
}	|	ddfd|v r
d|d         fndz   d|v rdt          |d         ¦  «        fndz   d|v r
d|d         fndz   |s
d|d         fndz   d|v r(t          |d          	                    d¦  «        ¦  «        ndz   dz   }
|                     d¦  «        pd}|r|	n|df|sdddd|d         fndz   d|v r
d|d         fndz   d|v rdt          |d         ¦  «        fndz   d|v r
d|d         fndz   d|v r(t          |d          	                    d¦  «        ¦  «        ndz   |rdn|fz   }t          |¦  «        }t                               d                     ||d         |¦  «        ¦  «         |rdS t          ||r|gng t          j        |r|j        nd|¬¦  «         t%          |
|¬ ¦  «         dS )!a  
    Restore the given PostgreSQL database from an extract stream. The database is supplied as a
    one-element sequence containing a dict describing the database, as per the configuration schema.
    Use the given log prefix in any log entries. If this is a dry run, then don't actually restore
    anything. Trigger the given active extract process (an instance of subprocess.Popen) to produce
    output to consume.

    If the extract process is None, then restore the dump from the filesystem rather than from an
    extract stream.
    z+ (dry run; not actually restoring anything)rM   é   z+The database configuration value is invalidr   r%   r&   r*   Úpsql_commandr(   r)   z--quietr+   r,   r-   r.   r/   r0   z--dbnameÚanalyze_optionsr2   )z	--commandÚANALYZEÚpg_restore_commandÚ
pg_restorerS   z--exit-on-errorrR   Úrestore_optionsz&{}: Restoring PostgreSQL database {}{}N)Úoutput_log_levelÚ
input_filer4   r3   )ÚlenrZ   Úboolr   r[   r   r   r>   r?   r@   r!   rA   rB   r'   r   ÚloggingÚDEBUGÚstdoutr   )Údatabase_configrF   r   rd   Úextract_processrG   r   Úall_databasesrl   rw   Úanalyze_commandrz   Úrestore_commandr4   s                 r   Úrestore_database_dumpr‰   µ   sQ  € ð FMÐTÐAÐAÐRT€Må
ˆ?ÑÔ˜qÒ Ð ÝÐFÑGÔGÐGà˜qÔ!€HÝ˜ &Ô)¨UÒ2Ñ3Ô3€MÝÔ4Ý�Ñ'Ô'¨°&Ô)9¸8¿<º<È
Ñ;SÔ;Sñô €Mð —<’< Ñ/Ô/Ð9°6€Là	�¨	Ð2Ø/9¸XÐ/EÐ/EˆH�h˜zÔ*Ð+Ð+È2ñ	Oà06¸(Ð0BÐ0BˆH•c˜( 6Ô*Ñ+Ô+Ð,Ð,Èñ	Lð 4>ÀÐ3IÐ3IˆL˜( :Ô.Ð/Ð/Èrñ	Sð 2?ÐFˆJ˜ Ô(Ð)Ð)ÀBñ		Hð
 =NÐQYÐ<YÐ<Y�5�Ð+Ô,×2Ò2°3Ñ7Ô7Ñ8Ô8Ð8Ð_añ	cð #ñ	#ð ð "ŸšÐ&:Ñ;Ô;ÐK¸|Ðà&Ð	>ˆˆÐ,>ÀÐPð !ðˆ]Ð-¨y¸*ÀhÈvÔFVÐWÐWàñ		
ð 0:¸XÐ/EÐ/EˆH�h˜zÔ*Ð+Ð+È2ñ	Oð 17¸(Ð0BÐ0BˆH•c˜( 6Ô*Ñ+Ô+Ð,Ð,Èñ	Lð 4>ÀÐ3IÐ3IˆL˜( :Ô.Ð/Ð/Èrñ	Sð =NÐQYÐ<YÐ<Y�5�Ð+Ô,×2Ò2°3Ñ7Ô7Ñ8Ô8Ð8Ð_añ		cð !Ð6ˆ2ˆ2 }Ð&6ñ
	8ð õ /¨xÑ8Ô8Ðå
‡L‚LØ0×7Ò7¸
ÀHÈVÔDTÐVcÑdÔdñô ð ð ð Øˆõ #ØØ,Ð4ˆÐÐ°"Ý œØ-<ÐF�?Ô)Ð)À$Ø+ðñ ô ð õ �OÐ7HÐIÑIÔIÐIÐIÐIr   )N)rC   r�   r\   Úborgmatic.executer   r   r   Úborgmatic.hooksr   Ú	getLoggerÚ__name__rA   r   r!   r6   rK   rn   rq   rt   r‰   r,   r   r   ú<module>rŽ      s  ðØ 
€
€
€
Ø €€€Ø 	€	€	€	ðð ð ð ð ð ð ð ð ð ð
 !Ð  Ð  Ð  Ð  Ð  à	ˆÔ	˜8Ñ	$Ô	$€ðð ð ðð ð ð& 5Ð ð ð  ð  ðFMð Mð Mð`cð cð cð 26ðað að að að>Jð >Jð >Jð >Jð >Jr   