update backup database
This commit is contained in:
parent
82fad9712a
commit
3714b37121
@ -1,10 +1,12 @@
|
||||
#!/usr/bin/env python
|
||||
import shutil
|
||||
import odoorpc
|
||||
import color_log
|
||||
import argparse
|
||||
import sys
|
||||
import base64
|
||||
import os
|
||||
from datetime import datetime
|
||||
|
||||
# Default configuration
|
||||
DEFAULT_HOST = "localhost"
|
||||
@ -12,7 +14,6 @@ DEFAULT_PORT = 8069
|
||||
DEFAULT_USERNAME = "admin"
|
||||
DEFAULT_PASSWORD = "admin"
|
||||
BACKUP_DIR = "odoo_backups"
|
||||
CHUNK_SIZE = 500 # Records per batch for search operations
|
||||
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."""
|
||||
try:
|
||||
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)
|
||||
color_log.Show(OK, f"Connected to {args.host}:{args.port}, DB: {args.db_name}")
|
||||
# odoo.login(args.db_name, args.username, args.password)
|
||||
# color_log.Show(OK, f"Connected to {args.host}:{args.port}, DB: {args.db_name}")
|
||||
return odoo
|
||||
except odoorpc.error.RPCError as e:
|
||||
color_log.Show(FAIL, f"Login failed: {e}")
|
||||
@ -41,24 +41,33 @@ def parse_arguments() -> argparse.Namespace:
|
||||
parser.add_argument(
|
||||
"--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(
|
||||
"--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()
|
||||
|
||||
|
||||
def backup_database(odoo: odoorpc.ODOO, db_name: str, admin_password: str):
|
||||
"""Backup a single Odoo database."""
|
||||
date_str = datetime.now().strftime("%m-%d-%Y")
|
||||
try:
|
||||
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)
|
||||
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:
|
||||
f.write(base64.b64decode(backup_data))
|
||||
f.write(backup_data.read())
|
||||
|
||||
print(f"Backup saved: {backup_path}")
|
||||
except Exception as e:
|
||||
print(f"Failed to backup {db_name}: {e}")
|
||||
@ -79,7 +88,11 @@ def main():
|
||||
"""Main execution flow."""
|
||||
args = parse_arguments()
|
||||
odoo = connect_to_odoo(args)
|
||||
backup_all_databases(odoo, args.admin_password)
|
||||
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)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
Loading…
Reference in New Issue
Block a user