103 lines
3.4 KiB
Python
103 lines
3.4 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"
|
|
RESTORE_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="restore 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 restore (leave empty to restore all databases)",
|
|
)
|
|
return parser.parse_args()
|
|
|
|
|
|
def restore_database(odoo: odoorpc.ODOO, db_name: str, admin_password: str):
|
|
"""Restore a single Odoo database."""
|
|
try:
|
|
backup_path = os.path.join(RESTORE_DIR, f"{db_name}.zip")
|
|
if not os.path.exists(backup_path):
|
|
print(f"Backup file for {db_name} not found: {backup_path}")
|
|
return
|
|
|
|
with open(backup_path, "rb") as f:
|
|
print(f"Restoring database: {db_name} from {backup_path}...")
|
|
timeout_backup = odoo.config['timeout']
|
|
odoo.config['timeout'] = 7200 # Timeout set to 2 hours
|
|
odoo.db.restore(admin_password, db_name, f)
|
|
odoo.config['timeout'] = timeout_backup
|
|
print(f"Database {db_name} restored successfully.")
|
|
except Exception as e:
|
|
print(f"Failed to restore {db_name}: {e}")
|
|
|
|
|
|
def restore_all_databases(odoo: odoorpc.ODOO, admin_password: str):
|
|
"""Restore all databases from backup files in the restore directory."""
|
|
try:
|
|
backup_files = [f for f in os.listdir(RESTORE_DIR) if f.endswith(".zip")]
|
|
print("Backup files found:", backup_files)
|
|
for backup_file in backup_files:
|
|
db_name = os.path.splitext(backup_file)[0]
|
|
restore_database(odoo, db_name, admin_password)
|
|
except Exception as e:
|
|
print(f"Error restoring databases: {e}")
|
|
|
|
|
|
def main():
|
|
"""Main execution flow."""
|
|
args = parse_arguments()
|
|
odoo = connect_to_odoo(args)
|
|
if args.database:
|
|
for db_name in args.database:
|
|
restore_database(odoo, db_name, args.admin_password)
|
|
else:
|
|
restore_all_databases(odoo, args.admin_password)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
main()
|
|
except KeyboardInterrupt:
|
|
color_log.Show(FAIL, "\nOperation cancelled by user")
|
|
sys.exit(1)
|