Skip to content

Commit

Permalink
Implement database backup using webex (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
hackenbergstefan authored Aug 18, 2024
1 parent 9cbfe4c commit b7cf832
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 13 deletions.
69 changes: 56 additions & 13 deletions coffeebuddy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import os
import random
import socket
import subprocess
from pathlib import Path
from tempfile import TemporaryDirectory

import flask
import flask_login
Expand All @@ -13,7 +16,7 @@
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.exc import OperationalError

__version__ = "1.4.0"
__version__ = "1.5.0"

db = SQLAlchemy()
login_manager = flask_login.LoginManager()
Expand Down Expand Up @@ -136,7 +139,10 @@ def init_app_context(app):

coffeebuddy.pir.init()

if "REMINDER_MESSAGE" in flask.current_app.config:
if (
"WEBEX_DATABASE_BACKUP" in flask.current_app.config
or "REMINDER_MESSAGE" in flask.current_app.config
):
start_scheduler(app)


Expand Down Expand Up @@ -205,16 +211,53 @@ def prefill():


def start_scheduler(app):
from coffeebuddy.reminder import remind

scheduler = BackgroundScheduler()
scheduler.start()
scheduler.add_job(
func=remind,
args=(app,),
trigger="interval",
minutes=60,
id="webex dept reminder",
name="webex dept reminder",
replace_existing=True,
)

if flask.current_app.config.get("REMINDER_MESSAGE"):
from coffeebuddy.reminder import remind

scheduler.add_job(
func=remind,
args=(app,),
trigger="interval",
minutes=60,
id="webex dept reminder",
name="webex dept reminder",
replace_existing=True,
)

if flask.current_app.config.get("WEBEX_DATABASE_BACKUP"):
import webexteamssdk

@scheduler.scheduled_job("cron", day_of_week="sun")
def backup_database():
with TemporaryDirectory() as tmpdir, app.app_context():
backupfile = Path(tmpdir) / "coffeebuddydb-backup-{}".format(
datetime.datetime.now().strftime("%Y-%m-%dT%H-%M-%S.sql")
)
backupfile.write_text(
subprocess.check_output(
[
"sudo",
"docker-compose",
"exec",
"coffeebuddydb",
"pg_dump",
"-U",
"coffeebuddydb",
"-d",
"coffeebuddy",
],
cwd="database",
universal_newlines=True,
)
)

api = webexteamssdk.WebexTeamsAPI(
access_token=flask.current_app.config["WEBEX_ACCESS_TOKEN"]
)
api.messages.create(
roomId=flask.current_app.config["WEBEX_DATABASE_BACKUP"],
files=[str(backupfile)],
)
3 changes: 3 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@
NAME ROOM is looking forward to seeing you(r money)!
"""

# If not False, the webex roomid where to post database backup files
WEBEX_DATABASE_BACKUP = False

# List of peoples' emails who are notified when someone pays
PAYMENT_NOTIFICATION_EMAILS = []

Expand Down

0 comments on commit b7cf832

Please sign in to comment.