Skip to content

Instantly share code, notes, and snippets.

@johnongit
Created February 6, 2023 08:52
Show Gist options
  • Save johnongit/2a5adc8f5ea759f1a513d02b987b86a4 to your computer and use it in GitHub Desktop.
Save johnongit/2a5adc8f5ea759f1a513d02b987b86a4 to your computer and use it in GitHub Desktop.
############### backup script to backup nostream postgres sql database ###############
#!/bin/bash
# set current directory
BASEDIR=$(dirname $0)
# set the backup directory
BACKUP_DIR=$BASEDIR/backup
#backup file name (with date)
BACKUP_FILE=nostream-$(date +%Y-%m-%d).sql
# host, database name user and password
HOST=10.10.10.2
host=nostr_ts_relay
password=nostr_ts_relay
username=nostr_ts_relay
db=nostr_ts_relay
export PGPASSWORD=$password
# check argument. Should be 1 and should be the aws s3 bucket name
if [ $# -ne 1 ]; then
echo "Usage: $0 <s3 bucket name>"
exit 1
fi
S3_BUCKET=$1
S3_PREFIX=nostream
S3_TARGET=$S3_BUCKET/$S3_PREFIX
# check if backup directory exists. if not create it
if [ ! -d $BACKUP_DIR ]; then
echo "Backup directory does not exist. Creating $BACKUP_DIR"
mkdir -p $BACKUP_DIR
fi
# function that creates the backup
backup () {
echo "Creating backup of $db database"
pg_dumpall -h $HOST -U $username -f $BACKUP_DIR/$BACKUP_FILE
}
# function that deletes old backups (1 day)
delete_old_backups () {
echo "Deleting old backups"
find $BACKUP_DIR/* -mtime +1 -exec rm {} \;
}
# function that uploads the backup to s3
upload_to_s3 () {
echo "Uploading backup to s3"
aws s3 cp $BACKUP_DIR/$BACKUP_FILE s3://$S3_TARGET/$BACKUP_FILE
}
backup
upload_to_s3
delete_old_backups
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment