===== Ways to backup a odoo database =====
Install anonfiles patched version:
pip3 install git+https://github.com/EstebanMonge/anonfiles.git
from datetime import datetime
from anonfiles import upload
import xmlrpc.client
import base64
import os
import smtplib
import ssl
#Please modify as you wish
backup_all_databases = False
port = 465 # For SSL
smtp_server = "smtp.mailgun.org"
sender_email = "" # Enter your address
receiver_email = "" # Enter receiver address
password = ""
odoo_url = ''
all_database = ['']
today = datetime.today()
today_string = today.strftime("%Y-%m-%d")
#Danger to modify
if os.path.exists('urls.txt'):
os.remove('urls.txt')
print("Remove previous URLs file")
try:
sock = xmlrpc.client.ServerProxy(odoo_url) #Real public IP
if backup_all_databases:
all_database = sock.list()
except:
print("Something is wrong when I try to create the database list")
for database in all_database:
print("Making backup of: " + database)
file_path = "/tmp/backups-" + today_string #Give path of folder where backup file will be store
try:
if not os.path.exists(file_path):
os.makedirs(file_path)
print("The backup directory is created!")
except:
print("Something is wrong when I try to create the backup directory")
file_path += "/" + database
file_path += "-" + today_string
file_path += ".zip"
file_name = database + "-" + today_string + ".zip"
# try:
backup_db_file = open(file_path, 'wb')
backup_db_file.write(base64.b64decode(sock.dump('password', database, 'zip'))) #admin is master password in my case
backup_db_file.close()
print("Backup of database " + database + " was stored on " + file_path)
# except:
# print("Something is wrong when I try to save the database to local path")
try:
upload([file_path])
except:
print("Something is wrong when I try to upload the database to anonfiles")
try:
message = """\
Subject: Sempai Space - Database backup - """ + today_string + """
Hi:
Below you will find the URL to access the today's backups:\n"""
with open('urls.txt', 'r') as file:
message += file.read()
context = ssl.create_default_context()
with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:
server.login(sender_email, password)
server.sendmail(sender_email, receiver_email, message)
except:
print("Something is wrong when I try to send the mail with the database backup information")