Skip to content

Commit

Permalink
Merge pull request #153 from rafsaf/bump-to-python-3.12
Browse files Browse the repository at this point in the history
update python to 3.12.0 version, small misc
  • Loading branch information
rafsaf authored Oct 8, 2023
2 parents 6707bb5 + 40f34de commit 6f9af73
Show file tree
Hide file tree
Showing 16 changed files with 286 additions and 266 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/type_check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: "3.11.5"
python-version: "3.12.0"

- name: Install Poetry
uses: snok/install-poetry@v1
Expand Down
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
rev: v4.5.0
hooks:
- id: check-yaml

- repo: https://github.com/psf/black
rev: "23.3.0"
rev: "23.9.1"
hooks:
- id: black

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.0.288
rev: v0.0.292
hooks:
- id: ruff
args: [--fix]
Expand Down
18 changes: 8 additions & 10 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Both upload providers and backup targets were created with possibility to easly

## Project requirements

- Python 3.11.
- Python 3.12.
- Poetry [https://python-poetry.org/](https://python-poetry.org/).
- Docker and docker compose plugin [https://docs.docker.com/get-docker/](https://docs.docker.com/get-docker/).
- Debian/Ubuntu are known to work.
Expand All @@ -25,33 +25,32 @@ Both upload providers and backup targets were created with possibility to easly

1. Install python dependencies

`poetry install`
`poetry install`

2. Install pre-commit hooks.

`pre-commit install`
`pre-commit install`

3. Create `.env` file

`cp .env.example .env`
`cp .env.example .env`

4. To run database backups, you will need `mariadb-client` and `postgresql-client` installed, there are dedicated scripts in folder `/scripts` that can do that (or install using `apt` for example).

5. Setup databases

`docker compose up -d postgres_15 postgres_14 postgres_13 postgres_12 postgres_11 mysql_57 mysql_80 mariadb_1011 mariadb_1006 mariadb_1005 mariadb_1004`
`docker compose up -d postgres_15 postgres_14 postgres_13 postgres_12 postgres_11 mysql_57 mysql_80 mariadb_1011 mariadb_1006 mariadb_1005 mariadb_1004`

6. You can run backuper (`--single` here to make all backups immediatly and then exit):

`python -m backuper.main --single`
`python -m backuper.main --single`

## Docs
## Docs

To play with documentation, after dependencies are in place installed with poetry:

`mkdocs serve` will start development server.


## Testing

The project has a handful of test cases which must pass for a contribution to be
Expand All @@ -60,7 +59,7 @@ existing ones in order to target your changes.

You can run all the test cases locally by invoking `pytest`.

Since the code is closely related to docker container environment, setup to run pytest
Since the code is closely related to docker container environment, setup to run pytest
directly in the container exists:

- amd64: `docker compose run --rm --build backuper_tests_amd64`
Expand All @@ -78,7 +77,6 @@ For `Debug` provider acceptance tests, existing `.env.example` can be ok for `.e

Then `docker compose run --rm --build backuper_acceptance_test_amd64` (or even for arm64 `docker compose run --rm --build backuper_acceptance_test_arm64`) will use target `build` and your local `.env` file.


## Coding conventions

We expect that all code contributions have been formatted using `black`. You can
Expand Down
18 changes: 11 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM python:3.11.5-slim-bookworm AS base
FROM python:3.12.0-slim-bookworm AS base
ARG PYTHONUNBUFFERED=1
ARG PIP_DISABLE_PIP_VERSION_CHECK=1
ARG PIP_NO_CACHE_DIR=1
Expand All @@ -8,12 +8,16 @@ ENV FOLDER_PATH="/var/lib/backuper"
ENV LOG_FOLDER_PATH="/var/log/backuper"
WORKDIR ${FOLDER_PATH}

RUN apt-get -y update && apt-get -y install wget unzip postgresql-client mariadb-client
RUN apt-get -y update && apt-get -y install wget unzip gpg
COPY scripts scripts
RUN ./scripts/install_mariadb_client.sh
RUN ./scripts/install_postgresql_client.sh
RUN apt-get -y remove gpg
RUN addgroup --gid 1001 --system ${SERVICE_NAME} && \
adduser --gid 1001 --shell /bin/false --disabled-password --uid 1001 ${SERVICE_NAME}

FROM base as poetry
RUN pip install poetry==1.5.1
RUN pip install poetry==1.6.1
COPY poetry.lock pyproject.toml ./
RUN poetry export -o /requirements.txt --without-hashes
RUN poetry export -o /requirements-tests.txt --without-hashes --with tests
Expand All @@ -24,10 +28,10 @@ RUN pip install -r requirements.txt
RUN rm -f requirements.txt
# reduce size of botocore lib, see https://github.com/boto/botocore/issues/1543
RUN mkdir /tmp/data \
&& cp -r /usr/local/lib/python3.11/site-packages/botocore/data/s3 /tmp/data/ \
&& cp -f /usr/local/lib/python3.11/site-packages/botocore/data/*.json /tmp/data \
&& rm -rf /usr/local/lib/python3.11/site-packages/botocore/data \
&& cp -r /tmp/data /usr/local/lib/python3.11/site-packages/botocore/ \
&& cp -r /usr/local/lib/python3.12/site-packages/botocore/data/s3 /tmp/data/ \
&& cp -f /usr/local/lib/python3.12/site-packages/botocore/data/*.json /tmp/data \
&& rm -rf /usr/local/lib/python3.12/site-packages/botocore/data \
&& cp -r /tmp/data /usr/local/lib/python3.12/site-packages/botocore/ \
&& rm -rf /tmp/data

COPY backuper backuper
Expand Down
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<a href="https://github.com/rafsaf/backuper/blob/main/LICENSE" target="_blank">
<img src="https://img.shields.io/github/license/rafsaf/backuper" alt="License">
</a>
<a href="https://docs.python.org/3/whatsnew/3.11.html" target="_blank">
<img src="https://img.shields.io/badge/python-3.11-blue" alt="Python">
<a href="https://docs.python.org/3/whatsnew/3.12.html" target="_blank">
<img src="https://img.shields.io/badge/python-3.12-blue" alt="Python">
</a>
<a href="https://github.com/psf/black" target="_blank">
<img src="https://img.shields.io/badge/code%20style-black-lightgrey" alt="Black">
Expand All @@ -27,6 +27,7 @@ A tool for performing scheduled database backups and transferring encrypted data
Backups are in `zip` format using [7-zip](https://www.7-zip.org/), with strong AES-256 encryption under the hood.

## Documentation

- [https://backuper.rafsaf.pl](https://backuper.rafsaf.pl)

## Supported backup targets
Expand Down Expand Up @@ -80,7 +81,6 @@ services:
- POSTGRESQL_PG15=host=db password=pwd cron_rule=0 0 5 * * port=5432
- ZIP_ARCHIVE_PASSWORD=change_me
- BACKUP_PROVIDER=name=debug

```
(NOTE this will use provider [debug](https://backuper.rafsaf.pl/providers/debug/) that store backups locally in the container).
Expand All @@ -91,8 +91,7 @@ The author actively uses backuper (with GCS) for one production project [plemion
See how it looks for ~2GB size database:

![backuper_gcp_example_twp-min.jpg](https://raw.githubusercontent.com/rafsaf/backuper/main/docs/images/backuper_gcp_example_twp-min.jpg)
<br>
<br>
<br>
6 changes: 3 additions & 3 deletions backuper/backup_targets/base_target.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging
from abc import ABC, abstractmethod
from datetime import datetime
from datetime import datetime, timezone
from pathlib import Path
from typing import final

Expand All @@ -21,7 +21,7 @@ def __init__(
self.env_name: str = env_name
self.max_backups: int = max_backups
self.min_retention_days: int = min_retention_days
self.last_backup_time: datetime = datetime.utcnow()
self.last_backup_time: datetime = datetime.now(timezone.utc)
self.next_backup_time: datetime = self._get_next_backup_time()
log.info(
"first calculated backup of target `%s` will be: %s",
Expand All @@ -43,7 +43,7 @@ def make_backup(self) -> Path:

@final
def _get_next_backup_time(self) -> datetime:
now = datetime.utcnow()
now = datetime.now(timezone.utc)
cron = croniter(
self.cron_rule,
start_time=now,
Expand Down
15 changes: 7 additions & 8 deletions backuper/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import shlex
import shutil
import subprocess
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone
from pathlib import Path
from typing import Any, TypeVar

Expand Down Expand Up @@ -62,12 +62,11 @@ def remove_path(path: Path) -> None:
def get_new_backup_path(env_name: str, name: str, sql: bool = False) -> Path:
base_dir_path = config.CONST_BACKUP_FOLDER_PATH / env_name
base_dir_path.mkdir(mode=0o700, exist_ok=True, parents=True)
random_string = secrets.token_urlsafe(3)
new_file = "{}_{}_{}_{}".format(
env_name,
datetime.utcnow().strftime("%Y%m%d_%H%M"),
name,
random_string,
new_file = (
f"{env_name}_"
f"{datetime.now(timezone.utc).strftime('%Y%m%d_%H%M')}_"
f"{name}_"
f"{secrets.token_urlsafe(3)}"
)
if sql:
new_file += ".sql"
Expand Down Expand Up @@ -193,7 +192,7 @@ def create_provider_model() -> ProviderModel:
def file_before_retention_period_ends(
backup_name: str, min_retention_days: int
) -> bool:
now = datetime.utcnow()
now = datetime.now()
matches = DATETIME_BACKUP_FILE_PATTERN.finditer(backup_name)

datetime_str = ""
Expand Down
4 changes: 2 additions & 2 deletions backuper/notifications/notifications_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import traceback
from concurrent.futures import ThreadPoolExecutor
from contextlib import ContextDecorator
from datetime import datetime
from datetime import datetime, timezone
from enum import StrEnum
from types import TracebackType

Expand Down Expand Up @@ -41,7 +41,7 @@ def create_fail_message(
exc_val: BaseException,
exc_traceback: TracebackType,
) -> str:
now = datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S,%f UTC")
now = datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M:%S,%f %Z")
msg = f"[FAIL] {now}\nStep: {self.step_name}\n"
msg += f"Backuper Host: {config.options.INSTANCE_NAME}\n"
if self.env_name:
Expand Down
Loading

0 comments on commit 6f9af73

Please sign in to comment.