Forums

how to auto backup database?

  1. how to auto backup my database (say once every week), by running "python manage.py dumpdata appname>datafilename.json"?
  2. after auto backup, how to auto send the backup json file to a specific email address?

You may set up a scheduled task which would execute the relevant code only on a certain day of the week. If Django's dumpdata (ad 1) works fine, it's a good way to go. Then (ad 2) you'd need to have a code that sends the email for you.

the dumpdata method saves the json file directly to the server, how to get the path of this file from server and upload it into the email and sent to another email address? For step 2, I have no ideas how to do. please help

You can provide an absolute path for the command that dumps the data, e.g. /home/twintech/datafilename.json and then use this path when needed to attach the file to the email. As for the step 2 -- you need to look on more general forums (for example Stack Overflow) since our forums are dedicated to PythonAnywhere support and not general coding issues. Since you're using Django, you may have a look here as well.

to setup a schelued task, how to write the python script to "cd to the right directory" and run the command? In my case, the DB_backup.py program need to firstly run "cd OnlinePricing" to go to the right webapp directory in console, then run "python manage.py dumpdata OnlinePricing>DB_backup/backup.json ". I am not familiar with pythonanywhere console environment, how to write the py script (located in /home/twintech/OnlinePricing)?

Then, I just input "python3.6 /home/twintech/OnlinePricing/DB_backup.py" in the scheduled tasks?

Just run the management command from the task:

cd /the/directory; python3.6 manage.py dumpdata  appname > /full/path/to/the/file

thanks a lot. the above solution works. However, I want to make it more customized so it still needs to be running thorugh a .py script than a single commnad.

first, I want to add a date in the backup file name, e.g. backup_2022.5.4.json. So this is easier to be done in the .py script.

second, I need to wirte an auto email sending function that every time when the new backup file is created, attach it to an email to send it to another specific email address (I alreays have the auto-sending email program in place, just need to sort out how to locate the file path and attach it to the email)

So it returns back to my original question, how to write the python script to "cd to the right directory" and run the command, in full accordance with the pythonanywhere console environment?

Here's how to run external programs from a Python script: https://docs.python.org/3/library/subprocess.html and how to send email from Django: https://docs.djangoproject.com/en/4.0/topics/email/

cd /the/directory; python3.6 manage.py dumpdata appname > /full/path/to/the/file --- i just tested, if I add such command in the scheduled task, the second time when it excutes the command, it will overwrite the existing backup file with nothing (i.e. the previous backup file becomes empty), which is strange!

Use date command to add the timestamp to the filename.