Skip to Content
เมนู
คุณต้องลงทะเบียนเพื่อโต้ตอบกับคอมมูนิตี้
คำถามนี้ถูกตั้งค่าสถานะ
3 ตอบกลับ
5633 มุมมอง

"Manage database" link showing at the time of login. So any user can click on that and if he know the password of DB can drop it. In my point of view giving "Manage database" link in login screen is not that much secure. I can hide that link by inheriting web module. But the problem is to take backup of DB i have to connect to "server" and need to write the pgsql commands to take backup of db. What's my actual requirement is, can any one give the "path" of manage database. <a href="#" class="oe_login_manage_db">Manage Databases</a> is the manage database anchor tab. Where it is going when i clik on this link. So I saved that path some where in my mail. If i want to take back up of DB, i directly copy that path in browser and take back up. Or else is there any way to take backup without using the "manage database link", Psql command , and pgadmin 3?

อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

Hi,

Try below command may help you

pg_dump -U <DB Username> <DB Name>  >  backup.sql

อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

Put it in a file, then run a cron on it:

#! /usr/bin/env python
# -*- coding: utf-8 -*-

import os
import time
from subprocess import Popen, PIPE
import glob

BACKUP_DIR = '/path/to/some/backup/directory/'
LOG_FILE = '{}backup.log'.format(BACKUP_DIR)
USER = 'username'
DATABASES = ['foo', 'bar']


def dump(db_name):
    dump_dir = BACKUP_DIR + db_name + '/'
    # check if dump_dir exists, create it if not
    dump_dir_glob = glob.glob(dump_dir)
    if len(dump_dir_glob) == 0:
        os.mkdir(dump_dir)
    # dump db
    now = int(time.time() * 1000000)
    dump_file_path = '{}{}_{}.dump'.format(dump_dir, db_name, now)
    with open(dump_file_path, 'wb') as dump_file, \
            open(LOG_FILE, 'a') as log_file:
        process = Popen(['pg_dump', '-Fc', db_name, '-U', USER], stdout=dump_file, stderr=log_file)

def clean(days=2, hours=0, minutes=0, seconds=0):
    interval = seconds + minutes*60 + hours*60*60 + days*24*60*60
    x_days_ago = time.time() - interval
    unlinked = 0
    for db_name in DATABASES:
        glob_list = glob.glob('{}{}/*'.format(BACKUP_DIR, db_name))
        for filename in glob_list:
            file_info = os.stat(filename)
            if file_info.st_ctime < x_days_ago:
                os.unlink(filename)
                unlinked += 1
    return unlinked

def log(string):
    now = time.strftime('%d-%m-%Y_%H:%M:%S')
    string = '{} >> {}\n'.format(now, string)
    with open(LOG_FILE, 'a') as f:
        f.write(string)

def start():
    unlinked = clean(days=40)
    for db in DATABASES:
        dump(db)
    log('done | {} old archive(s) deleted | {} new archive(s) created'.format(unlinked, len(DATABASES)))

start()

อวตาร
ละทิ้ง

In the start function, change days=40 means that your db archives will stay 40 days before beeing cleaned.

คำตอบที่ดีที่สุด

After backup ,What is the restore command?

อวตาร
ละทิ้ง

DB=dbname && createdb $DB && pg_restore -n public --no-acl -O -d $DB

Here What is dbname?, $DB ? O -d $DB ?

DB=dbname && createdb $DB && pg_restore -n public --no-acl -O -d $DB