104 lines
3.2 KiB
Python
104 lines
3.2 KiB
Python
#!/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"
|
|
DEFAULT_PORT = 8069
|
|
DEFAULT_USERNAME = "admin"
|
|
DEFAULT_PASSWORD = "admin"
|
|
BACKUP_DIR = "odoo_backups"
|
|
OK, FAIL, INFO, WARNING = 0, 1, 2, 3
|
|
|
|
|
|
def connect_to_odoo(args: argparse.Namespace) -> odoorpc.ODOO:
|
|
"""Establish and verify Odoo connection."""
|
|
try:
|
|
odoo = odoorpc.ODOO(args.host, port=args.port)
|
|
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}")
|
|
return odoo
|
|
except odoorpc.error.RPCError as e:
|
|
color_log.Show(FAIL, f"Login failed: {e}")
|
|
sys.exit(1)
|
|
except Exception as e:
|
|
color_log.Show(FAIL, f"Connection error: {e}")
|
|
sys.exit(1)
|
|
|
|
|
|
def parse_arguments() -> argparse.Namespace:
|
|
"""Parse and validate command-line arguments."""
|
|
parser = argparse.ArgumentParser(description="Backup all Odoo databases.")
|
|
parser.add_argument("--host", default=DEFAULT_HOST, help="Odoo server host")
|
|
parser.add_argument(
|
|
"--port", type=int, default=DEFAULT_PORT, help="Odoo server port"
|
|
)
|
|
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}...")
|
|
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}-{date_str}.zip")
|
|
|
|
# Ensure BytesIO object is written correctly
|
|
with open(backup_path, "wb") as f:
|
|
f.write(backup_data.read())
|
|
|
|
print(f"Backup saved: {backup_path}")
|
|
except Exception as e:
|
|
print(f"Failed to backup {db_name}: {e}")
|
|
|
|
|
|
def backup_all_databases(odoo: odoorpc.ODOO, admin_password: str):
|
|
"""Backup all available databases."""
|
|
try:
|
|
db_list = odoo.db.list()
|
|
print("Databases found:", db_list)
|
|
for db_name in db_list:
|
|
backup_database(odoo, db_name, admin_password)
|
|
except Exception as e:
|
|
print(f"Error retrieving database list: {e}")
|
|
|
|
|
|
def main():
|
|
"""Main execution flow."""
|
|
args = parse_arguments()
|
|
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)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
main()
|
|
except KeyboardInterrupt:
|
|
color_log.Show(FAIL, "\nOperation cancelled by user")
|
|
sys.exit(1)
|