Skip to content

Commit

Permalink
Merge pull request #683 from EvanBldy/master
Browse files Browse the repository at this point in the history
various improvements
  • Loading branch information
EvanBldy authored Aug 11, 2023
2 parents a041c7a + 0d10a4a commit aa7be3d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 14 deletions.
9 changes: 3 additions & 6 deletions zou/app/services/backup_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,10 @@
)


def generate_db_backup(host, port, user, password, database):
def generate_db_backup(host, port, user, password, database, filename):
"""
Generate a Postgres dump file from the database.
"""
now = datetime.datetime.now().strftime("%Y-%m-%d")
filename = "%s-zou-db-backup.sql.gz" % now
cmd = ["pg_dump", "-h", host, "-p", port, "-U", user, database]
with gzip.open(filename, "wb") as f:
popen = subprocess.Popen(
Expand All @@ -48,19 +46,18 @@ def generate_db_backup(host, port, user, password, database):
popen.stdout.close()
popen.wait()

print(f"Postgres dump created ({filename}).")
return filename


def store_db_backup(filename):
def store_db_backup(filename, path):
"""
Store given file located in the same directory, inside the files bucket
using the `dbbackup` prefix.
"""
from zou.app import app

with app.app_context():
file_store.add_file("dbbackup", filename, filename)
file_store.add_file("dbbackup", filename, path)


def upload_preview_files_to_storage(days=None):
Expand Down
19 changes: 17 additions & 2 deletions zou/app/utils/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import json
import datetime
import tempfile


from ldap3 import Server, Connection, ALL, NTLM, SIMPLE
Expand Down Expand Up @@ -525,15 +526,29 @@ def download_file_from_storage():
sync_service.download_preview_files_from_storage()


def dump_database():
def dump_database(store=False):
now = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S")
filename = f"zou-db-backup-{now}.sql.gz"
if store:
filename = os.path.join(tempfile.gettempdir(), filename)

filename = backup_service.generate_db_backup(
app.config["DATABASE"]["host"],
app.config["DATABASE"]["port"],
app.config["DATABASE"]["username"],
app.config["DATABASE"]["password"],
app.config["DATABASE"]["database"],
filename,
)
backup_service.store_db_backup(filename)

if store:
backup_service.store_db_backup(os.path.basename(filename), filename)
os.remove(filename)
print(
f"Postgres dump added to store (dbbackup/{os.path.basename(filename)})."
)
else:
print(f"Postgres dump created ({os.path.realpath(filename)}).")


def upload_files_to_cloud_storage(days):
Expand Down
8 changes: 4 additions & 4 deletions zou/app/utils/date_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def get_year_interval(year):
Get a tuple containing start date and end date for given year.
"""
year = int(year)
if year > datetime.now().year or year < 2010:
if year > datetime.utcnow().year or year < 2010:
raise WrongDateFormatException

start = datetime(year, 1, 1)
Expand All @@ -80,7 +80,7 @@ def get_month_interval(year, month):
"""
year = int(year)
month = int(month)
if year > datetime.now().year or year < 2010 or month < 1 or month > 12:
if year > datetime.utcnow().year or year < 2010 or month < 1 or month > 12:
raise WrongDateFormatException

start = datetime(year, month, 1)
Expand All @@ -94,7 +94,7 @@ def get_week_interval(year, week):
"""
year = int(year)
week = int(week)
if year > datetime.now().year or year < 2010 or week < 1 or week > 52:
if year > datetime.utcnow().year or year < 2010 or week < 1 or week > 52:
raise WrongDateFormatException
start = isoweek.Week(year, week).monday()
end = start + relativedelta.relativedelta(days=7)
Expand All @@ -109,7 +109,7 @@ def get_day_interval(year, month, day):
month = int(month)
day = int(day)
if (
year > datetime.now().year
year > datetime.utcnow().year
or year < 2010
or month < 1
or month > 12
Expand Down
5 changes: 3 additions & 2 deletions zou/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,12 +395,13 @@ def download_storage_files():


@cli.command()
def dump_database():
@click.option("--store", is_flag=True)
def dump_database(store=False):
"""
Dump database described in Zou environment variables and save it to
configured object storage.
"""
commands.dump_database()
commands.dump_database(store)


@cli.command()
Expand Down

0 comments on commit aa7be3d

Please sign in to comment.