|
| 1 | + |
| 2 | +# Python AWS Backup CLI |
| 3 | + |
| 4 | +A simple, production-ready Python script to compress a directory and upload the archive to **AWS S3**. |
| 5 | +Includes unit tests (pytest) and CI automation with **GitHub Actions**. |
| 6 | + |
| 7 | +## Features |
| 8 | +- Compress any folder into a timestamped **.zip** (default) or **.tar.gz** |
| 9 | +- Upload the archive to **Amazon S3** |
| 10 | +- Simple CLI interface with sensible defaults |
| 11 | +- Unit tests with **pytest** |
| 12 | +- **GitHub Actions** workflow to run tests on every push/PR |
| 13 | + |
| 14 | +--- |
| 15 | + |
| 16 | +## Quickstart |
| 17 | + |
| 18 | +### 1) Clone & set up |
| 19 | +```bash |
| 20 | +git clone https://github.com/your-username/python-aws-backup-cli.git |
| 21 | +cd python-aws-backup-cli |
| 22 | +python -m venv .venv |
| 23 | +# Windows |
| 24 | +. .venv/Scripts/activate |
| 25 | +# macOS/Linux |
| 26 | +# source .venv/bin/activate |
| 27 | +pip install -r requirements.txt |
| 28 | +``` |
| 29 | + |
| 30 | +### 2) Configure AWS credentials (one-time) |
| 31 | +Use the AWS CLI or environment variables. |
| 32 | + |
| 33 | +```bash |
| 34 | +# Option A: AWS CLI (recommended) |
| 35 | +aws configure |
| 36 | +# Provide AWS Access Key ID, Secret Access Key, region, output format |
| 37 | +``` |
| 38 | + |
| 39 | +Alternatively set env vars before running: |
| 40 | +```bash |
| 41 | +export AWS_ACCESS_KEY_ID=... |
| 42 | +export AWS_SECRET_ACCESS_KEY=... |
| 43 | +export AWS_DEFAULT_REGION=ap-south-1 |
| 44 | +``` |
| 45 | + |
| 46 | +### 3) Run a local backup (no upload) |
| 47 | +```bash |
| 48 | +python backup.py --source ./my_data --outdir ./backups --format zip |
| 49 | +``` |
| 50 | + |
| 51 | +### 4) Backup and upload to S3 |
| 52 | +```bash |
| 53 | +python backup_and_s3.py --source ./my_data --outdir ./backups --format zip --bucket your-bucket-name --prefix optional/folder/path/ |
| 54 | +``` |
| 55 | + |
| 56 | +- `--bucket` is your S3 bucket name. |
| 57 | +- `--prefix` is optional (folder path inside the bucket). |
| 58 | +- Result example: `s3://your-bucket-name/optional/folder/path/backup_my_data_20250101_123000.zip` |
| 59 | + |
| 60 | +--- |
| 61 | + |
| 62 | +## Project Structure |
| 63 | +``` |
| 64 | +python-aws-backup-cli/ |
| 65 | +├─ backup.py # create compressed backups (zip/tar.gz) |
| 66 | +├─ s3_upload.py # upload helper for S3 |
| 67 | +├─ backup_and_s3.py # one-shot: create backup + upload |
| 68 | +├─ requirements.txt |
| 69 | +├─ .gitignore |
| 70 | +├─ README.md |
| 71 | +├─ tests/ |
| 72 | +│ └─ test_backup.py |
| 73 | +└─ .github/ |
| 74 | + └─ workflows/ |
| 75 | + └─ python-tests.yml # CI: run pytest on push/PR |
| 76 | +``` |
| 77 | + |
| 78 | +--- |
| 79 | + |
| 80 | +## Examples |
| 81 | + |
| 82 | +Create a **.zip** archive: |
| 83 | +```bash |
| 84 | +python backup.py -s ./my_data -o ./backups -f zip |
| 85 | +``` |
| 86 | + |
| 87 | +Create a **.tar.gz** archive: |
| 88 | +```bash |
| 89 | +python backup.py -s ./my_data -o ./backups -f tar |
| 90 | +``` |
| 91 | + |
| 92 | +Backup and **upload to S3** with a prefix: |
| 93 | +```bash |
| 94 | +python backup_and_s3.py -s ./my_data -o ./backups -f zip --bucket my-bucket --prefix daily/ahad/ |
| 95 | +``` |
| 96 | + |
| 97 | +--- |
| 98 | + |
| 99 | +## Notes |
| 100 | +- Windows users: prefer absolute paths or use `./my_data` relative to the project root. |
| 101 | +- Make sure your AWS IAM user/role has `s3:PutObject` permission for your bucket (and `s3:ListBucket` if needed). |
| 102 | +- Large directories: consider excluding patterns (future enhancement). |
| 103 | + |
| 104 | +--- |
| 105 | + |
| 106 | +## License |
| 107 | +MIT |
0 commit comments