This script is designed to download the contents of an S3-compatible bucket (including AWS S3, MinIO, DigitalOcean Spaces, Wasabi, or others) and store them in a local directory. It supports both AWS S3 and generic S3-compatible services with custom endpoint URLs, making it a flexible solution for backing up your cloud storage.
The script ensures backups are organized by date and uses a unique subdirectory for each run (e.g., 2023/11/01/01). It is packaged within a Docker image for easy portability and execution.
The script is configured using environment variables. Here's what you need to set:
| Variable Name | Description | Required | Example Values |
|---|---|---|---|
BACKUP_DIR | The directory inside the container where backups will be stored. Defaults to /backups. | No | /backups |
S3_BUCKET_NAME | The name of the S3-compatible bucket to download. | Yes | my-example-bucket |
S3_ACCESS_KEY_ID | The Access Key for authenticating with the S3-compatible service. | Yes | your-access-key-id |
S3_SECRET_ACCESS_KEY | The Secret Key for authenticating with the S3-compatible service. | Yes | your-secret-access-key |
S3_ENDPOINT_URL | The custom endpoint URL for the S3-compatible service. Use AWS-compatible defaults if absent. | No | https://nyc3.digitaloceanspaces.com (for DigitalOcean Spaces) |
To run the Docker container, you need to pass the required environment variables and specify a volume to store the backup files.
Use pre-built Docker Image:
docker run --rm \
-e BACKUP_DIR=/backups \
-e S3_BUCKET_NAME=my-bucket \
-e S3_ACCESS_KEY_ID=your-access-key-id \
-e S3_SECRET_ACCESS_KEY=your-secret-access-key \
-e S3_ENDPOINT_URL=https://nyc3.digitaloceanspaces.com \
-v /path/to/your/local/backup:/backups \
ghcr.io/jory3/s3backup
Build the Docker Image on your own: If you haven't already built the image, you can build it using:
docker build -t s3-backup-app .
Run the Docker Container: To run the container, use the following command:
docker run --rm \
-e BACKUP_DIR=/backups \
-e S3_BUCKET_NAME=my-bucket \
-e S3_ACCESS_KEY_ID=your-access-key-id \
-e S3_SECRET_ACCESS_KEY=your-secret-access-key \
-e S3_ENDPOINT_URL=https://nyc3.digitaloceanspaces.com \
-v /path/to/your/local/backup:/backups \
s3-backup-app
--rm: Automatically removes the container after it stops.-e BACKUP_DIR=/backups: Specifies the backup directory inside the container.-e S3_BUCKET_NAME=my-bucket: Sets the name of the bucket to back up.-e S3_ACCESS_KEY_ID & -e S3_SECRET_ACCESS_KEY: Provide your S3-compatible access credentials.-e S3_ENDPOINT_URL: (Optional) Endpoint URL for non-AWS S3 services (e.g., MinIO, DigitalOcean Spaces). Leave this empty for AWS S3.-v /path/to/your/local/backup:/backups: Maps the container's backup directory /backups to a local directory on your system (/path/to/your/local/backup allows you to see your backups after the container stops).s3-backup-app: Name of the Docker image to run./path/to/your/local/backup..env FileInstead of passing environment variables directly in the docker run command, you can use a .env file to simplify the configuration process. Here's how you can do it:
Create a .env File:
.env in the same directory where you will run the Docker container:
BACKUP_DIR=/backups
S3_BUCKET_NAME=my-example-bucket
S3_ACCESS_KEY_ID=your-access-key-id
S3_SECRET_ACCESS_KEY=your-secret-access-key
S3_ENDPOINT_URL=https://nyc3.digitaloceanspaces.com
Modify the Values:
your-access-key-id, your-secret-access-key, etc.) with your actual credentials and settings.Run the Docker Container with the .env File:
.env file to the container using the --env-file option:
docker run --rm \
--env-file .env \
-v /path/to/your/local/backup:/backups \
s3-backup-app
.env:docker run command every time.docker run --rm \
-e BACKUP_DIR=/backups \
-e S3_BUCKET_NAME=my-aws-bucket \
-e S3_ACCESS_KEY_ID=AWS_ACCESS_KEY_HERE \
-e S3_SECRET_ACCESS_KEY=AWS_SECRET_KEY_HERE \
-v /local/backup:/backups \
s3-backup-app
docker run --rm \
-e BACKUP_DIR=/backups \
-e S3_BUCKET_NAME=my-digitalocean-space \
-e S3_ACCESS_KEY_ID=DO_ACCESS_KEY_HERE \
-e S3_SECRET_ACCESS_KEY=DO_SECRET_KEY_HERE \
-e S3_ENDPOINT_URL=https://nyc3.digitaloceanspaces.com \
-v /local/backup:/backups \
s3-backup-app
docker run --rm \
-e BACKUP_DIR=/backups \
-e S3_BUCKET_NAME=minio-bucket \
-e S3_ACCESS_KEY_ID=minio-access-key \
-e S3_SECRET_ACCESS_KEY=minio-secret-key \
-e S3_ENDPOINT_URL=http://localhost:9000 \
-v /local/backup:/backups \
s3-backup-app
If you'd like to run the script locally without Docker, you can do so by following these steps:
Install Python Dependencies:
pip install -r requirements.txt
Run the Script:
export BACKUP_DIR=./backup
export S3_BUCKET_NAME=my-example-bucket
export S3_ACCESS_KEY_ID=your-access-key-id
export S3_SECRET_ACCESS_KEY=your-secret-access-key
export S3_ENDPOINT_URL=https://nyc3.digitaloceanspaces.com # Optional
python backup_script.py
When the backup runs successfully, the files will be downloaded into the specified BACKUP_DIR, organized with the following structure:
This project is licensed under the MIT License. Please refer to the LICENSE file for more details.
Content type
Image
Digest
sha256:ebe59955c…
Size
78.1 MB
Last updated
6 months ago
docker pull rymedia/s3backup