DEV Community

Fabian Anguiano
Fabian Anguiano

Posted on

Automate Your Odoo Backups with Python and Store Them in a S3 Bucket - Get Telegram Notifications Too!

It's 10:30pm and you're getting ready for a nice, cozy sleep. You're just about to drift off when you hear your phone explode with notifications. You groggily reach for your phone and see that Odoo is down. No problem, you think. You'll just roll over to another standby server. You know that Odoo is already installed, so you fire off some AWS magic and get it up and running in no time.

The only problem is that the backups you had "for sure ready to go" haven't been updated in months. You start to panic a little, wondering why on earth those backups weren't updated. Wasn't so-and-so supposed to do that? You can't believe that this is happening, especially at this late hour. You rub your eyes and try to think of a solution, but you're so tired that your brain just isn't cooperating.

If you want to avoid the chaos above let Python handle your back ups.

Logic

The script itself is simple. We will use subprocess to run the back up command.
We will then send the backup to an s3 bucket, and alert by telegram. After that use cron to run it every x time.

PIP Requirments

Here is the packages you will need.

import subprocess
import boto3
from telegram import Bot
from telegram.error import TelegramError
import os
Enter fullscreen mode Exit fullscreen mode

Code

Note: Ideally some of the variable would be set in .env will wire that in the next itegration.

#!/usr/bin/env python

import subprocess
import boto3
from telegram import Bot
from telegram.error import TelegramError
import os

# Set the URL of the Odoo instance we are backing up. 

odoo_url = "http://localhost:8069"

# Set the filepath for the backup file

backup_filepath = "/path/to/backup/file.dump"

# if you want the script to save the back up the same directory
# backup_filepath = os.path.join(os.path.dirname(__file__), "file.dump")


# Set the name of the S3 bucket where the backup will be uploaded

s3_bucket_name = "YOUR_BUCKET_NAME"

# Set the API token for the Telegram bot

telegram_api_token = "YOUR_API_TOKEN"

# Set the chat ID for the Telegram chat where notifications will be sent

telegram_chat_id = "YOUR_CHAT_ID"

# Use curl to create a backup of the Odoo instance
subprocess.run(["curl", "-X", "POST", '-F', 'master_pwd=yourpassword', '-F', 'name=dbname', '-F', 'backup_format=zip', '-o', backup_filepath, 'http://localhost:8069/web/database/backup'])


# Check the exit code of the curl command to make sure the backup was successful
if subprocess.run(["curl", "-X", "POST", '-F', 'master_pwd=yourpassword', '-F', 'name=dbname' '-F', 'backup_format=zip', '-o', backup_filepath, 'http://localhost:8069/web/database/backup']).returncode == 0:
    # Upload the backup file to S3
    s3 = boto3.client("s3")
    with open(backup_filepath, "rb") as data:
        s3.upload_fileobj(data, s3_bucket_name, backup_filepath)
        print("Odoo backup uploaded to S3 successfully!")

        # Send a success message using the Telegram bot
        try:
            bot = Bot(telegram_api_token)
            bot.send_message(telegram_chat_id, "Odoo backup created and uploaded to S3 successfully!")
        except TelegramError as e:
            print(e)
else:
    # Send a failure message using the Telegram bot
    try:
        bot = Bot(telegram_api_token)
        bot.send_message(telegram_chat_id, "Odoo backup failed")
    except TelegramError as e:
        print(e)
Enter fullscreen mode Exit fullscreen mode

Here is the github repo. https://github.com/thetrebelcc/Odoo_back_script/tree/main

Follow me on twitter more Odoo/Python

https://twitter.com/tunahorse21?t=ZzU5NNgL0YvTSRpy-6NThA&s=09

Top comments (0)