Pol Frances
Pol Frances

Reputation: 402

Django manage.py command having SyntaxError on ElasticBeanstalk

I'm using AWS for first time, and after uploading my Django project I wanted to know how to backup the information of the DB into a file to be able to modify the data in case I have to modify the models of my project (still on development) and keep having some population data.

I thought about the django dumpdata command, so to be able to execute it on EB through the CLI I did the following (and here is where maybe I'm doing something wrong):

- eb ssh
- sudo -s
- cd /opt/python/current/app/
- python manage.py dumpdata --natural-foreign --natural-primary -e contenttypes -e auth.Permission --indent 4 > project_dump.json

For what I understand, the first command is just to access to the SSH on Elastic Beanstalk.

The second one is to have root permissions inside the linux server to evade problems creating and opening files, etc.

The third one is just to access where the current working aplication is.

And the last is the command I have to use to dump all the data "human friendly" without restrictions to be able to use it in any other new database.

I have to say that I tried this last command on my local machine and worked as expected without any error or warning.

So, the issue I'm facing here is that when I execute this last command, I get the following error:

  File "manage.py", line 14
    ) from exc
         ^
SyntaxError: invalid syntax

Also I tried to skip the sudo -s to just use the permissions of the user I'm using to log on the ssh, but got: -bash: project_dump.json: Permission denied. So that is why I thought using the sudo command would help here.

In addition, I followed this known tutorial to deploy Django+PostgreSQL on EB, so the user I'm using to access to ssh is one in a group with AdministratorAccess permissions.

Before Trying all of this, I also looked for a way of having this information directly from AWS-RDS, but I only found a way of having a backup restored, but without being able to modify the content manually, so is not what I really need.

Upvotes: 1

Views: 707

Answers (1)

dirkgroten
dirkgroten

Reputation: 20692

As on your local environment, you need to run your manage.py commands inside your correct python virtualenv and make sure the environment variables like RDS_USERNAME and RDS_PASSWORD are set. To do that, you need to:

  1. Activate your virtualenv
  2. Source your environment variables

As described at the end of the tutorial you mentioned, this is how to do it:

source /opt/python/run/venv/bin/activate
source /opt/python/current/env
python manage.py <your_command>

And you have to do that every time you ssh into the machine.

Note: the reason you're getting the permission denied error is that when you pipe the output of dumpdata to project_dump.json, you're trying to write in the app directory itself. Not a good idea. Try piping to > ~/project_dump.json instead (your home directory), then sudo won't be needed.

Upvotes: 3

Related Questions