作者:都市翡翠阿萨姆红茶 | 来源:互联网 | 2023-09-23 17:38
Imtryingtousethedatabaseconfigurationsetonsettingsfilestomakeadatabasedumpusingfab
I'm trying to use the database configuration set on settings files to make a database dump using fabric.
我正在尝试使用设置文件上的数据库配置集来使用fabric生成数据库转储。
There's more than one settings file, so I'd like to be able to do so based on the environment I choose.
有不止一个设置文件,所以我希望能够基于我所选择的环境来实现。
by now, my task is like this
现在,我的任务是这样的
def dump_database():
with cd('~/project_folder'), prefix(WORKON_VIRTUALENV):
django.settings_module(env.settings)
from django.conf import settings
dbname = settings.DATABASES['default']['NAME']
dbuser = settings.DATABASES['default']['USER']
dbpassword = settings.DATABASES['default']['PASSWORD']
fname = '/tmp/{0}-backup-{1}.sql.gz'.format(
dbname,
time.strftime('%Y%m%d%H%M%S')
)
run('mysqldump -u %s -p=%s %s | gzip -9 /tmp/backup-%s.sql.gz' % (
dbuser,
dbpassword,
dbname,
fname))
But I'm getting an ImportError:
但我有一种恐惧:
ImportError: Could not import settings 'project.settings.production'
I've tried to use shell_env() to set the DJANGO_SETTINGS_MODULE instead of django.settings_module(env.settings), with the same result.
我尝试使用shell_env()来设置DJANGO_SETTINGS_MODULE,而不是django.settings_module(env.settings),结果是相同的。
I use a task to change the environment based on a environment dict:
我使用一个任务来改变环境基于环境通告:
def environment(name):
env.update(environments[name])
env.envirOnment= name
This way, I want to be able to create a dump from multiple hosts like:
通过这种方式,我希望能够从多个主机创建一个转储,比如:
fab environment:live dump_database
fab environment:otherhost dump_database
Without having to reproduce database settings from all hosts on fabfile.
不需要从fabfile上的所有主机上复制数据库设置。
2 个解决方案
1
NOTE: I don't answer the question but offer a different solution
注意:我不回答这个问题,但提供了一个不同的解决方案
I had the same problem.. so I did custom .py script like that:
我遇到了同样的问题。我定制了。py脚本
I created a file named dump_db.py (placed next to fabfile.py for example, that is on the remote machine)
我创建了一个名为dump_db的文件。py(放置在fabfile旁边)。例如,py在远程机器上)
import os
import sys
from datetime import datetime
from django.conf import settings
def dump_mysql():
os.environ.setdefault("DJANGO_SETTINGS_MODULE", SETTINGS_MODULE)
DB_NAME = settings.DATABASES['default']['NAME']
DB_USER = settings.DATABASES['default']['USER']
DB_PASSWORD = settings.DATABASES['default']['PASSWORD']
dump_file_name = '{time}_{db_name}.sql'.format(
time=datetime.now().strftime('%Y_%m_%d'),
db_name=DB_NAME,
)
os.system('mysqldump -u {db_user} -p{db_password} {db_name} > {to_file}'.format(
db_user=DB_USER,
db_password=DB_PASSWORD,
db_name=DB_NAME,
to_file=dump_file_name,
))
return dump_file_name
if __name__ == '__main__':
try:
SETTINGS_MODULE = sys.argv[1:].pop()
except IndexError:
SETTINGS_MODULE = 'project_name.settings'
print dump_mysql()
As you see sys.argv[1:].pop() tries to take optional argument (the setting module in this case).
当您看到sys.argv[1:].pop()时,尝试使用可选参数(在本例中是设置模块)。
So in my fabfile: import os
在我的fabfile中:导入操作系统
from fabric.api import env, local, run, prefix, cd
.....
def dump():
current_dir = os.getcwd()
with prefix('source {}bin/activate'.format(env.venv)), cd('{}'.format(env.home)):
dumped_file = run('python dump_db.py {}'.format(env.environment)) # the optional argument given
file_path = os.path.join(env.home, dumped_file)
copy_to = os.path.join(current_dir, dumped_file)
scp(file_path, copy_to)
def scp(file_path, copy_to):
local('scp {}:{} {}'.format(env.host, file_path, copy_to))
where env.envirOnment= 'project_name.settings.env_module'
env的地方。环境= ' project_name.settings.env_module '
And this is how I dump my DB and copy it back to me.
这就是我如何转储我的DB并将它复制给我。
Hope it comes handy to someone! :)
希望它能派上用场!:)