0

I'm trying to export Postgresql database but it returns backup() takes no arguments (1 given). I tried various methods, but unable to export database.

from pathlib import Path, PureWindowsPath
from subprocess import PIPE,Popen
​
​
def backup():
    version = 11
    # postgresDir = "D:/Program Files (x86)/PostgreSQL/9.1/bin"
    postgresDir = str("D:/Program Files (x86)/PostgreSQL/9.1/bin/").split('\\')[-1:][0]
​
    directory = postgresDir
    filename = 'myBackUp2'  # output filename here
    saveDir = Path("D:/{}.tar".format(filename))  # output directory here
    file = PureWindowsPath(saveDir)
​
    host = 'localhost'
    user = 'postgres'
    port = '5432'
    dbname = 'BPS_Server'  # database name here
    proc = Popen(['pg_dump', '-h', host, '-U', user, '-W', '-p', port,
                   '-F', 't', '-f', str(file), '-d', dbname],
                    cwd=directory, shell=True, stdin=PIPE)
    proc.wait()
​
backup()
pali
  • 7
  • 2
  • 8
  • Any reason why you are using Django? This looks like a regular python script to me, can you try moving the script out of your Django project and executing it? Also maybe rename the backup function, just a wild guess – pragman Feb 23 '19 at 12:52
  • I tried to run the file out of my django project but it's not working. It returns FileNotFoundError: [WinError 2] The system cannot find the file specified – pali Feb 23 '19 at 14:33

1 Answers1

0

for backup from postgresql you have two ways:

1) use a bash script and run it every day or month for get backup : you can get more information here

2) use fixture in django:

def backup():
    filename = 'myBackUp2'  # output filename here
    saveDir = open("D:/{}.json".format(filename), 'w')

    # change application_name with your django app which you want to get backup from it
    call_command('dumpdata', 'application_name', stdout=saveDir, indent=3)
    saveDir.close()

call_command() uses for execute django command and dumpdata uses for get fixture from a table. more information

  • I create db.json file. Sorry , i'm confused what shall i put in dumpdata and application_name. Shall I put path to db.json file in open("D:/{}.json".format(filename), 'w') ? – pali Feb 23 '19 at 16:57
  • `dumpdata` generate a json file which contains your data. you know, in django we can define some applications in `INSTALLED_APPS` so for get dumpdata you should define an application which you define in `INSTALLED_APPS` and django generate its dumpdata – Khashayar Ghamati Feb 24 '19 at 05:29
  • see this [sample](https://stackoverflow.com/questions/1113096/django-dump-data-for-a-single-model) – Khashayar Ghamati Feb 24 '19 at 05:31
  • I want to get backup of whole database. I did python manage.py dumpdata. I can see the dumpdata created in python shell but I don't have any idea where it is created. – pali Feb 24 '19 at 05:48
  • I tried it by using subprocess also.It creates gzip file but empty file. – pali Feb 24 '19 at 05:49
  • do that with `django-background-task` or `redis-queue` for sample you can execute this command `python manage.py dumpdata your-application` but before execute this command be sure your database has data. – Khashayar Ghamati Feb 24 '19 at 05:53
  • you can use `call_command()` in your code and generate a dumpdata – Khashayar Ghamati Feb 24 '19 at 05:54
  • let me write a example and share with you in gitlab – Khashayar Ghamati Feb 24 '19 at 06:00
  • I'm stuck in another problem also. My project is runs in browser but when i tried to import anythin in python shell, it returns 'import' is not recognized as an internal or external command, operable program or batch file – pali Feb 24 '19 at 06:09
  • check [this repo](https://gitlab.com/khashayarghamati/dumpdata_sample) and be in touch with me via my email address ;) – Khashayar Ghamati Feb 24 '19 at 06:28
  • I have sent you request in hangout. – pali Feb 24 '19 at 06:50
  • I haven't new request please send me a mail – Khashayar Ghamati Feb 24 '19 at 08:48