update backup database
This commit is contained in:
parent
82fad9712a
commit
3714b37121
@ -1,10 +1,12 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
import shutil
|
||||||
import odoorpc
|
import odoorpc
|
||||||
import color_log
|
import color_log
|
||||||
import argparse
|
import argparse
|
||||||
import sys
|
import sys
|
||||||
import base64
|
import base64
|
||||||
import os
|
import os
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
# Default configuration
|
# Default configuration
|
||||||
DEFAULT_HOST = "localhost"
|
DEFAULT_HOST = "localhost"
|
||||||
@ -12,7 +14,6 @@ DEFAULT_PORT = 8069
|
|||||||
DEFAULT_USERNAME = "admin"
|
DEFAULT_USERNAME = "admin"
|
||||||
DEFAULT_PASSWORD = "admin"
|
DEFAULT_PASSWORD = "admin"
|
||||||
BACKUP_DIR = "odoo_backups"
|
BACKUP_DIR = "odoo_backups"
|
||||||
CHUNK_SIZE = 500 # Records per batch for search operations
|
|
||||||
OK, FAIL, INFO, WARNING = 0, 1, 2, 3
|
OK, FAIL, INFO, WARNING = 0, 1, 2, 3
|
||||||
|
|
||||||
|
|
||||||
@ -20,11 +21,10 @@ def connect_to_odoo(args: argparse.Namespace) -> odoorpc.ODOO:
|
|||||||
"""Establish and verify Odoo connection."""
|
"""Establish and verify Odoo connection."""
|
||||||
try:
|
try:
|
||||||
odoo = odoorpc.ODOO(args.host, port=args.port)
|
odoo = odoorpc.ODOO(args.host, port=args.port)
|
||||||
if args.verbose:
|
|
||||||
color_log.Show(INFO, f"Available databases: {odoo.db.list()}")
|
color_log.Show(INFO, f"Available databases: {odoo.db.list()}")
|
||||||
|
|
||||||
odoo.login(args.db_name, args.username, args.password)
|
# odoo.login(args.db_name, args.username, args.password)
|
||||||
color_log.Show(OK, f"Connected to {args.host}:{args.port}, DB: {args.db_name}")
|
# color_log.Show(OK, f"Connected to {args.host}:{args.port}, DB: {args.db_name}")
|
||||||
return odoo
|
return odoo
|
||||||
except odoorpc.error.RPCError as e:
|
except odoorpc.error.RPCError as e:
|
||||||
color_log.Show(FAIL, f"Login failed: {e}")
|
color_log.Show(FAIL, f"Login failed: {e}")
|
||||||
@ -41,24 +41,33 @@ def parse_arguments() -> argparse.Namespace:
|
|||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--port", type=int, default=DEFAULT_PORT, help="Odoo server port"
|
"--port", type=int, default=DEFAULT_PORT, help="Odoo server port"
|
||||||
)
|
)
|
||||||
parser.add_argument("--username", default=DEFAULT_USERNAME, help="Odoo username")
|
|
||||||
parser.add_argument("--password", default=DEFAULT_PASSWORD, help="Odoo password")
|
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--admin-password", required=True, help="Odoo master admin password"
|
"--admin-password", required=True, help="Odoo master admin password"
|
||||||
)
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--database",
|
||||||
|
nargs="*",
|
||||||
|
help="Specific databases to backup (leave empty to backup all databases)",
|
||||||
|
)
|
||||||
return parser.parse_args()
|
return parser.parse_args()
|
||||||
|
|
||||||
|
|
||||||
def backup_database(odoo: odoorpc.ODOO, db_name: str, admin_password: str):
|
def backup_database(odoo: odoorpc.ODOO, db_name: str, admin_password: str):
|
||||||
"""Backup a single Odoo database."""
|
"""Backup a single Odoo database."""
|
||||||
|
date_str = datetime.now().strftime("%m-%d-%Y")
|
||||||
try:
|
try:
|
||||||
print(f"Backing up database: {db_name}...")
|
print(f"Backing up database: {db_name}...")
|
||||||
backup_data = odoo.db.dump(admin_password, admin_password, db_name)
|
timeout_backup = odoo.config["timeout"]
|
||||||
|
odoo.config["timeout"] = 600 # Timeout set to 10 minutes
|
||||||
|
backup_data = odoo.db.dump(admin_password, db_name)
|
||||||
|
odoo.config["timeout"] = timeout_backup
|
||||||
os.makedirs(BACKUP_DIR, exist_ok=True)
|
os.makedirs(BACKUP_DIR, exist_ok=True)
|
||||||
backup_path = os.path.join(BACKUP_DIR, f"{db_name}.zip")
|
backup_path = os.path.join(BACKUP_DIR, f"{db_name}-{date_str}.zip")
|
||||||
|
|
||||||
|
# Ensure BytesIO object is written correctly
|
||||||
with open(backup_path, "wb") as f:
|
with open(backup_path, "wb") as f:
|
||||||
f.write(base64.b64decode(backup_data))
|
f.write(backup_data.read())
|
||||||
|
|
||||||
print(f"Backup saved: {backup_path}")
|
print(f"Backup saved: {backup_path}")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Failed to backup {db_name}: {e}")
|
print(f"Failed to backup {db_name}: {e}")
|
||||||
@ -79,6 +88,10 @@ def main():
|
|||||||
"""Main execution flow."""
|
"""Main execution flow."""
|
||||||
args = parse_arguments()
|
args = parse_arguments()
|
||||||
odoo = connect_to_odoo(args)
|
odoo = connect_to_odoo(args)
|
||||||
|
if args.database:
|
||||||
|
for db_name in args.database:
|
||||||
|
backup_database(odoo, db_name, args.admin_password)
|
||||||
|
else:
|
||||||
backup_all_databases(odoo, args.admin_password)
|
backup_all_databases(odoo, args.admin_password)
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user