Skip to content

Commit 56b1bec

Browse files
committed
Add Python version and update project structure with pyproject.toml
1 parent 289be6d commit 56b1bec

File tree

9 files changed

+231
-255
lines changed

9 files changed

+231
-255
lines changed

.python-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.11

Dockerfile

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,34 @@
1-
# Stage 1: Build
2-
FROM python:3.10.7-slim-buster as builder
1+
# Use a Python image with uv pre-installed
2+
FROM ghcr.io/astral-sh/uv:python3.11-bookworm
33

4+
# Install the project into `/app`
45
WORKDIR /app
56

6-
COPY requirements.txt .
7+
# Enable bytecode compilation
8+
ENV UV_COMPILE_BYTECODE=1
79

8-
# Install build dependencies and libraries
9-
RUN pip install --no-cache-dir -r requirements.txt
10+
# Copy from the cache instead of linking since it's a mounted volume
11+
ENV UV_LINK_MODE=copy
1012

13+
# Install the project's dependencies using the lockfile and settings
14+
RUN --mount=type=cache,target=/root/.cache/uv \
15+
--mount=type=bind,source=uv.lock,target=uv.lock \
16+
--mount=type=bind,source=pyproject.toml,target=pyproject.toml \
17+
uv sync --frozen --no-install-project --no-dev
1118

12-
# Stage 2: Production
13-
FROM python:3.10.7-slim-buster as production
19+
# Then, add the rest of the project source code and install it
20+
# Installing separately from its dependencies allows optimal layer caching
21+
ADD . /app
22+
RUN --mount=type=cache,target=/root/.cache/uv \
23+
uv sync --frozen --no-dev
1424

15-
WORKDIR /app
16-
17-
COPY . .
25+
# Place executables in the environment at the front of the path
26+
ENV PATH="/app/.venv/bin:$PATH"
1827

19-
# Copy only the compiled result from previous stage
20-
COPY --from=builder /app .
28+
# Reset the entrypoint, don't invoke `uv`
29+
ENTRYPOINT []
2130

31+
# Run the FastAPI application by default
32+
# Uses `fastapi dev` to enable hot-reloading when the `watch` sync occurs
33+
# Uses `--host 0.0.0.0` to allow access from outside the container
2234
CMD ["python", "main.py"]

README.md

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,5 @@
11
# docker-volume-backup
22

3-
example
3+
## Overview
44

5-
```yaml
6-
version: "3.8"
7-
services:
8-
app:
9-
image: ubuntu
10-
volumes:
11-
- app:/app
12-
- app1:/app1
13-
working_dir: /app
14-
stdin_open: true
15-
tty: true
16-
command: tail -F anything
17-
18-
backup:
19-
build:
20-
context: .
21-
dockerfile: Dockerfile
22-
volumes:
23-
- app:/tmp/backups/app:ro
24-
- app1:/tmp/backups/app1:ro
25-
- ./outputs:/tmp/outputs
26-
# save logs to host
27-
28-
environment:
29-
- BACKUP_DIR=/tmp/backups
30-
- OUTPUT_DIR=/tmp/outputs
31-
- S3_ENDPOINT=
32-
- S3_BUCKET=backups
33-
- S3_ACCESS_KEY=
34-
- S3_SECRET_KEY=
35-
- S3_PREFIX=docker
36-
- SECOND_INTERVAL=60 // backup interval in seconds
37-
38-
volumes:
39-
app:
40-
app1:
41-
```
5+
`docker-volume-backup` is a project designed to automate the backup of Docker volumes to a specified output directory and optionally upload them to an S3

docker-compose.yml

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,18 @@
1-
version: '3.8'
1+
version: "3.8"
22
services:
3-
app:
4-
image: ubuntu
5-
volumes:
6-
- app:/app
7-
- app1:/app1
8-
working_dir: /app
9-
stdin_open: true
10-
tty: true
11-
command: tail -F anything
12-
133
backup:
14-
build:
4+
build:
155
context: .
166
dockerfile: Dockerfile
177
volumes:
18-
- app:/tmp/backups/app:ro
19-
- app1:/tmp/backups/app1:ro
20-
- ./outputs:/tmp/outputs
21-
# save logs to host
22-
8+
- ./tests/app:/tmp/backups/app:ro
9+
- ./tests/output:/tmp/output
2310
environment:
2411
- BACKUP_DIR=/tmp/backups
25-
- OUTPUT_DIR=/tmp/outputs
26-
- S3_ENDPOINT=https://0c5059a262cdf86340651b0d9a085c9e.r2.cloudflarestorage.com
12+
- OUTPUT_DIR=/tmp/output
13+
- S3_ENDPOINT=
2714
- S3_BUCKET=backups
28-
- S3_ACCESS_KEY=a37a6cd9f857e427818e7aae139689e4
29-
- S3_SECRET_KEY=7d8ab79a602a5a236d12e6e3e4bd6339665feea70f187775ff8d1914236e9eab
30-
- S3_PREFIX=docker
31-
- SECOND_INTERVAL=60
32-
33-
34-
volumes:
35-
app:
36-
app1:
15+
- S3_ACCESS_KEY=
16+
- S3_SECRET_KEY=
17+
- S3_PREFIX=docker-volumes-backup
18+
- SECOND_INTERVAL=60

main.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
from utils import *
2-
from botocore.exceptions import NoCredentialsError
32
import os
43
from dotenv import load_dotenv
54
import os
6-
import time
7-
import schedule
85
import logging
6+
import schedule
97

108
# Load the .env file
11-
load_dotenv(".env")
9+
load_dotenv(".env" ,override=True)
1210

13-
# main
14-
if __name__ == "__main__":
11+
def main():
1512
# # Load configuration from .env file
16-
mode = os.getenv("MODE")
13+
mode = os.getenv("MODE", 'backup')
1714
backup_dir = os.getenv("BACKUP_DIR")
1815
output_dir = os.getenv("OUTPUT_DIR")
16+
17+
print("backup_drr", backup_dir)
18+
19+
# logging.info("backup: {backup_dir}")
20+
1921
# Set up logging
2022
logging.basicConfig(level=logging.INFO,
2123
format='%(asctime)s - %(levelname)s - %(message)s',
@@ -33,16 +35,17 @@
3335

3436
if mode == "restore":
3537
logging.info("Restore mode ")
36-
S3_ENDPOINT_URL = os.getenv("S3_ENDPOINT")
37-
S3_ACCESS_KEY = os.getenv("S3_ACCESS_KEY")
38-
S3_SECRET_KEY = os.getenv("S3_SECRET_KEY")
38+
s3 = connect_s3_from_env()
3939
S3_BUCKET = os.getenv("S3_BUCKET")
40-
S3_PREFIX = os.getenv("S3_PREFIX")
4140
RESTORE_DIR = os.getenv("RESTORE_DIR")
42-
if (not S3_ENDPOINT_URL or not S3_ACCESS_KEY or not S3_SECRET_KEY or not S3_BUCKET):
43-
logging.error("S3 credentials not found")
44-
s3 = connect_s3(S3_ENDPOINT_URL, S3_ACCESS_KEY, S3_SECRET_KEY)
41+
if (not RESTORE_DIR):
42+
logging.error("Restore dir not found")
43+
return
4544
download_dir_from_s3(
4645
s3, S3_BUCKET, RESTORE_DIR, output_dir)
4746
restore_directory(output_dir, backup_dir)
48-
logging.info(f"Restore finished at {get_timestamp()}")
47+
logging.info(f"Restore finished at {get_time()}")
48+
49+
# main
50+
if __name__ == "__main__":
51+
main()

pyproject.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[project]
2+
name = "docker-volume-backup"
3+
version = "0.1.0"
4+
description = "Add your description here"
5+
readme = "README.md"
6+
requires-python = ">=3.11"
7+
dependencies = [
8+
"boto3>=1.35.90",
9+
"botocore>=1.35.90",
10+
"python-dotenv>=1.0.1",
11+
"schedule>=1.2.2",
12+
]

requirements.txt

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)