mò
ZEc           @   s»   d  k  Z  d  k Z d  k Z e i d ƒ Z d e f d „  ƒ  YZ d  k Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ	 d e f d	 „  ƒ  YZ
 d
 e
 f d „  ƒ  YZ d e
 f d „  ƒ  YZ d  S(   Ns   thefactz.sysadmin.backupt   BackupItemBasec           B   s2   t  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   sœ   
    Base class for backup item classes which perform backup activities for a single archive of items of a particular type (e.g. filesystem, dbs, etc).
    c         C   s   | |  _ | |  _ d S(   so   
        @param archiveName: name of archive to write to. NB: extension may get
        added to this.
        N(   t   backupBaseDirPatht   selft   _backupBaseDirPatht   archiveNamet   _archiveName(   R   R   R   (    (    t:   /users/home/rpollock/rgrp/sysadmin/common/backup/backup.pyt   __init__   s     	c         C   s   d S(   s   
        Performs backup for this backup item.
        In base class performs no function and **must** be overridden in derived classes
        N(    (   R   (    (    R   t   doBackup   s    c         C   s5   t  i | ƒ } | d j o t i d | ƒ n | S(   si   
        Run a shell command (UNIX or windows)
        
        @return the resulting error code
        i    s   Shell command failed: N(   t   ost   systemt   commandt   err_codet   loggert   error(   R   R   R   (    (    R   t
   runCommand   s
     c         C   s!   t  i i |  i |  i d | ƒ S(   ss   
        @param extension: additional extension to add to file name (should be
        without leading '.'
        t   .N(   R	   t   patht   joinR   R   R   t	   extension(   R   R   (    (    R   t   getBackupFilePath'   s     (   t   __name__t
   __module__t   __doc__R   R   R   R   (    (    (    R   R       s
    			t   BackupItemFilec           B   s   t  Z d „  Z d „  Z RS(   Nc         C   s&   t  t |  ƒ i | | ƒ | |  _ d S(   sN   
        @param files: list of paths to backup (files or directories)
        N(   t   superR   R   R   R   R   t   filest	   _fileList(   R   R   R   R   (    (    R   R   8   s     c         C   si   t  i i |  i |  i d ƒ } t i | d ƒ } t
 | _ x |  i D] } | i | ƒ qD W| i ƒ  d S(   s³  
        @see BackupItemBase.doBackup()
        Add files/directories in list supplied on creation to archive
        See __init__ for more information on parameters used
        
        By default recurse into subdirectories.
        Uses integrated gzip by default.
        Auto naming of destination file if destination is a directory with first name of first source.
        Existing destination file will be overwritten.
        s   .tgzs   w:gzN(   R	   R   R   R   R   R   t   backupFilePatht   tarfilet   opent   tart   Falset   posixR   t   fft   addt   close(   R   R   R   R"   (    (    R   R   ?   s    
 	
 (   R   R   R   R   (    (    (    R   R   6   s   	t   BackupItemSvnc           B   sD   t  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   s5   
    Backup svn repositories using svnadmin dump
    c         C   s5   t  t |  ƒ i | | ƒ t i i | | ƒ |  _	 d S(   sf   
        @param archiveName: optional subdirectory name into which to put the
        backups
        N(
   R   R%   R   R   R   R   R	   R   R   t   baseBackupPath(   R   R   R   (    (    R   R   [   s     c         C   s   | |  _ d  S(   N(   t   repoListR   t	   _repoList(   R   R'   (    (    R   t   setRepoListc   s    c         C   sL   g  |  _ t i | ƒ } x- | D]% } |  i i t i i	 | | ƒ ƒ q Wd S(   sk   
        @param parentPath: parentPath to repos (i.e. all repos are subdirectories of parent path)
        N(
   R   R(   R	   t   listdirt
   parentPatht   repost   repot   appendR   R   (   R   R+   R-   R,   (    (    R   t   setRepoListFromParentPathf   s     	 c         C   sO   t  i i |  i ƒ p t  i |  i ƒ n x |  i D] } |  i | ƒ q4 Wd  S(   N(	   R	   R   t   existsR   R&   t   makedirsR(   t   repoPatht   _dumpBackup(   R   R2   (    (    R   R   o   s
    
 c         C   sz   t  i i t  i i | ƒ ƒ } t  i i |  i | d ƒ } t
 i d | d | d ƒ d | d | } |  i | ƒ d  S(   Ns   .gzs   Backing up repository s    to s    .... s   svnadmin dump --quiet s	    | gzip >(   R	   R   t   basenamet   normpathR2   t   repoNameR   R   R&   t   destPathR   t   infot   cmdR   (   R   R2   R7   R9   R6   (    (    R   R3   u   s
    c         C   sž   t  i i t  i i | ƒ ƒ } t  i i |  i | ƒ } t  i i
 | ƒ p t  i | ƒ n t i d | d | d ƒ d | d | d } |  i | ƒ d  S(   Ns   Backing up repository s    to s    .... s   svnadmin hotcopy t    s    --clean-logs(   R	   R   R4   R5   R2   R6   R   R   R&   R7   R0   R1   R   R8   R9   R   (   R   R2   R7   R9   R6   (    (    R   t   _hotCopyBackup|   s    (	   R   R   R   R   R)   R/   R   R3   R;   (    (    (    R   R%   V   s    						t   BackupItemDbc           B   s   t  Z d  Z d „  Z RS(   sÀ  
    @attribute dbName: name of database or special value __all__ which
    indicates that all databases should be backed up. Defaults to __all__
    @attribute username: username to use in connecting to database.
    @attribute extraParams: array of extra params to use in running backup
        Should be provided in correct string form i.e. --lock-tables or -d NOT 
        as lock-tables or d
    @attribute password: password for database
    c         C   sA   t  t |  ƒ i | | ƒ d |  _ d |  _ d |  _ g  |  _	 d S(   s}   
        @archiveName: name of archive to write to. NB: name should not have
        extension as this will be added
        t    N(
   R   R<   R   R   R   R   t   dbNamet   usernamet   passwordt   extraParams(   R   R   R   (    (    R   R   ”   s     			(   R   R   R   R   (    (    (    R   R<   ‰   s   	 t   BackupItemDbPgsqlc           B   s    t  Z d  Z d „  Z d „  Z RS(   sŽ   
    Use ident authentication so:
        1. username may be blank in which case connect as current user
        2. password not relevant
    c         C   s   t  t |  ƒ i | | ƒ d  S(   N(   R   RB   R   R   R   R   (   R   R   R   (    (    R   R   §   s    c         C   s¶   |  i d j o
 d } n d } | d i |  i ƒ 7} |  i d j o | d |  i 7} n |  i d j o d |  i d | } n | d 7} | d |  i d	 ƒ 7} |  i | ƒ d  S(
   Nt   __all__s   pg_dumpall s   pg_dump R:   R=   s   sudo -u s
    --insertss
    | gzip > t   gz(   R   R>   t	   backupCmdR   RA   R?   R   R   (   R   RE   (    (    R   R   ª   s    

(   R   R   R   R   R   (    (    (    R   RB       s    	t   BackupItemDbMysqlc           B   s   t  Z d  Z d „  Z RS(   s   
    See BackupItemDb.
    c         C   sÁ   d } |  i d j o | d |  i 7} n |  i d j o | d |  i 7} n | d d i |  i ƒ 7} |  i d j o | d 7} n | d |  i 7} | d |  i d	 ƒ 7} |  i | ƒ d  S(
   Nt	   mysqldumpR=   s    --user s    --password=R:   RC   s    --all-databasess
    | gzip > RD   (	   RE   R   R?   R@   R   RA   R>   R   R   (   R   RE   (    (    R   R   Â   s    (   R   R   R   R   (    (    (    R   RF   ½   s    (   R	   t   shutilt   loggingt	   getLoggerR   t   objectR    R   R   R%   R<   RB   RF   (   RB   RI   R<   R   R   R%   RF   R   RH   R	   R    (    (    R   t   ?   s   			-	 3
