https://github.com/sypper-pit/postgresql-backup-ftp
This script facilitates automated backups of a PostgreSQL database in Docker, handles their upload to FTP, and manages backup file retention by deleting old files.
https://github.com/sypper-pit/postgresql-backup-ftp
backup-script bash-script docker ftp postgresql
Last synced: 2 months ago
JSON representation
This script facilitates automated backups of a PostgreSQL database in Docker, handles their upload to FTP, and manages backup file retention by deleting old files.
- Host: GitHub
- URL: https://github.com/sypper-pit/postgresql-backup-ftp
- Owner: sypper-pit
- License: gpl-3.0
- Created: 2024-03-15T16:59:51.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-03-15T20:27:30.000Z (over 2 years ago)
- Last Synced: 2025-03-28T22:35:01.187Z (over 1 year ago)
- Topics: backup-script, bash-script, docker, ftp, postgresql
- Language: Shell
- Homepage: https://cctv-m.ru/pgsql-bash-backup
- Size: 32.2 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
BTC: bc1qttzg9yww3nv5dg2d5ja95txmt0mrw9dltfqj57
Monero(XMR): 8AyWrMwPCxrcbcmVDj3Y5RCfcSQtBBVE2JK9qJ4WqrPpaoa3uNvLReQXPXGj7D5zEsMjBKeWWdyDD4gerqzTtKKS36zSfnM
LTC: LfMJCyxxg65sA3X9XEze157D16ztszndqk
USDT(TRC-20): TTZGfnhurU62VRRGYUHMPJ8q6U8rn5xG5a
USDT(ERC-20): 0xbdfec67586a78e5d3b58dfb70aa181823c8deafa
DOGE: D6kb8jcVXYTi82nsoACAYKYhtA5EJ4D9Jg
# Backup and Cleanup Script for PostgreSQL in Docker with FTP Upload
## Overview
This guide explains how to modify a Bash script for backing up a PostgreSQL database inside a Docker container, uploading the backup to an FTP server, and deleting old backup files.
## Prerequisites
- Docker installed
- Access to a PostgreSQL container
- FTP server credentials
## Steps
0. **Install pack on ubuntu**
```bash
sudo apt install curl lftp
```
2. **Set Variables**: Define variables for directory paths, container names, database details, and FTP credentials at the beginning of your script.
```bash
BACKUP_DIR="/path/to/backup"
CONTAINER_NAME_PART="db-container-name-part"
POSTGRES_DB="your_db_name"
POSTGRES_USER="your_db_user"
FTP_USER="your_ftp_user"
FTP_PASSWORD="your_ftp_password"
FTP_HOST="ftp.server.com"
FTP_DIR="/path/on/ftp"
DAYS_OLD=7
```
3. **Find Container ID**: Use `docker ps` to find your container's ID by matching part of its name.
```bash
CONTAINER_ID=$(docker ps | grep $CONTAINER_NAME_PART | awk '{print $1}')
```
4. **Backup Database**: Perform the `pg_dump` command inside your container to create the backup.
```bash
docker exec -e PGPASSWORD=$POSTGRES_PASSWORD $CONTAINER_ID pg_dump -U $POSTGRES_USER $POSTGRES_DB > $BACKUP_FILE_NAME
```
5. **Upload to FTP**: Use `curl` to upload the backup file to your FTP server.
```bash
curl -T ${BACKUP_FILE_NAME} --user ${FTP_USER}:${FTP_PASSWORD} ftp://${FTP_HOST}${FTP_DIR}/
```
6. **Delete Old Files Locally**: Use `find` to delete backup files older than `DAYS_OLD` days.
```bash
find $BACKUP_DIR -type f -name "*.sql" -mtime +$DAYS_OLD -exec rm {} \;
```
7. **Delete Old Files on FTP**: Utilize `lftp` to connect to the FTP server and delete old files.
```bash
lftp -u ${FTP_USER},${FTP_PASSWORD} ${FTP_HOST} <$DAYS_OLD' | xargs -r -d '\n' rm
bye
EOF
```