#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    8
    Rep Power
    0

    Error In PostgreSQL Backup script (python)


    Hi, I have the Following Script to backup Database Postgres under Windows 7 64-bit:

    Code:
    from time import gmtime, strftime
    import subprocess
    import os
    import glob
    import time
    
    # change these as appropriate for your platform/environment :
    USER = "postgres"
    PASS = "xxxxxxx"
    HOST = "localhost"
    
    BACKUP_DIR = "d:\\Pg_Backup\py\\"
    dumper = """ "c:\\Program Files\\PostgreSQL\\9.2\\bin\\pg_dump" -U %s -Z 9 -f %s -F c %s  """                  
    
    def log(string):
        print time.strftime("%Y-%m-%d-%H-%M-%S", time.gmtime()) + ": " + str(string)
    
    # Change the value in brackets to keep more/fewer files. time.time() returns seconds since 1970...
    # currently set to 2 days ago from when this script starts to run.
    
    x_days_ago = time.time() - (60 * 60 * 24 * 2)
    
    os.putenv('PGPASSWORD', PASS)
    
    database_list = subprocess.Popen('echo "select datname from pg_database" | psql -t -U %s -h %s template1' % USER , shell=True, stdout=subprocess.PIPE).stdout.readlines()
    
    # Delete old backup files first.
    for database_name in database_list :
        database_name = database_name.strip()
        if database_name == '':
            continue
    
        glob_list = glob.glob(BACKUP_DIR + database_name + '*' + '.pgdump')
        for file in glob_list:
            file_info = os.stat(file)
            if file_info.st_ctime < x_days_ago:
                log("Deslincando: %s" % file)
                os.unlink(file)
            else:
                log("Mantendo : %s" % file)
    
    log("Arquivos anteriores a: %s foram removidos." % time.strftime('%c', time.gmtime(x_days_ago)))
    
    # Now perform the backup.
    for database_name in database_list :
        log("Dump iniciado para %s" % database_name)
        thetime = str(strftime("%Y-%m-%d-%H-%M"))
        file_name = database_name + '_' + thetime + ".sql.pgdump"
        # Run the pg_dump command to the right directory
        command = dumper % (USER, BACKUP_DIR + file_name, database_name)
        log(command)
        subprocess.call(command, shell=True)
        log("Dump terminado para %s" % database_name)
    
    log("Tarefa de Backup completa.")
    But when I run it, the following error occurs:

    Code:
    Traceback (most recent call last):
      File "teste.py", line 25, in <module>
        database_list = subprocess.Popen('echo "select datname from pg_database" | psql -t -U %s -h %s template1' % USER , shell=True, stdout=subprocess.PIPE).stdout.readlines()
    TypeError: not enough arguments for format string
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    You need a hostname. The string needs two string arguments for competion.
    Code:
    database_list = subprocess.Popen('echo "select datname from pg_database" | psql -t -U %s -h %s template1' % (USER, HOSTNAME) , shell=True, stdout=subprocess.PIPE).stdout.readlines()
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    8
    Rep Power
    0
    Hi,
    I changed the following line in the file:
    Code:
    database_list = subprocess.Popen('echo "select datname from pg_database" | psql -t -U %s -h %s template1' % (USER, HOST), shell=True, stdout=subprocess.PIPE).stdout.readlines()
    Now there was a different error:
    Code:
    Importing test modules ... ERROR: syntax error at or near ""select datname from pg_database"" 
    LINE 1: "select datname from pg_database" 
             ^
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    Looks to me that the it's a sql error. The select statement needs a terminator. Which is semi-colon.
    select datname from pg_database;

    If you need more help please hire me. I suspect you're in a paying job while I'm unemployed.
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    8
    Rep Power
    0
    Hello,
    Added the Semicolon in query as you can see below:

    Code:
    database_list = subprocess.Popen('echo "select datname from pg_database;" | psql -t -U %s -h %s template1' % (USER, HOST), shell=True, stdout=subprocess.PIPE).stdout.readlines()
    And error continues to occur, I think it must be something with the quotes, because the error is this:





    On the possibility of employment, perhaps more for the future, as for now I'm starting newly in this area and my financial situation is not very good.

    But thank you for the help you are giving me.

IMN logo majestic logo threadwatch logo seochat tools logo