Skip to content

Commit 600d31e

Browse files
committed
fix directory of backup
1 parent 642f96b commit 600d31e

File tree

1 file changed

+27
-34
lines changed

1 file changed

+27
-34
lines changed

backup_database.sh

Lines changed: 27 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,13 @@ if ! command -v aws &> /dev/null; then
4646
fi
4747

4848
# currently the backup directory is hard coded
49-
BACKUP_DIR=/home/ubuntu/buildingsync-website-backups
50-
mkdir -p ${BACKUP_DIR}
49+
BACKUP_BASE_DIR=/home/ubuntu/buildingsync-website-backups
50+
mkdir -p ${BACKUP_BASE_DIR}
5151

52-
# get the run date to save as the s3 folder name
52+
# get the run date to save as the s3 folder name and create date-specific backup directory
5353
RUN_DATE=$(date +%Y-%m-%d)
54+
BACKUP_DIR=${BACKUP_BASE_DIR}/${RUN_DATE}
55+
mkdir -p ${BACKUP_DIR}
5456

5557
function file_name(){
5658
echo ${BACKUP_DIR}/${DB_NAME}_$(date '+%Y%m%d_%H%M%S').dump
@@ -75,34 +77,25 @@ CURRENT_GID=$(id -g)
7577
echo "docker run --rm --user $CURRENT_UID:$CURRENT_GID -v buildingsync_media:/backup/media -v $BACKUP_DIR:/backup/dir/ alpine:3.19 tar zcvf /backup/dir/$MEDIA_BACKUP_BASENAME /backup/media"
7678
docker run --rm --user $CURRENT_UID:$CURRENT_GID -v buildingsync_media:/backup/media -v $BACKUP_DIR:/backup/dir/ alpine:3.19 tar zcvf /backup/dir/$MEDIA_BACKUP_BASENAME /backup/media
7779

78-
# Delete files older than 30 days.
79-
find ${BACKUP_DIR} -mtime +30 -type f -name '*.dump' -delete
80-
find ${BACKUP_DIR} -mtime +30 -type f -name '*.tgz' -delete
81-
82-
# upload to s3
83-
for file in "$BACKUP_DIR"/*.dump
84-
do
85-
echo "Backing up $file to $S3_BUCKET/buildingsync-website/$RUN_DATE/"
86-
if [ ! -s "$file" ]; then
87-
# the file is empty, send an error
88-
echo "[ERROR]-PostgreSQL-backup-file-was-empty-or-missing"
89-
else
90-
# can't pass spaces to slack notifications, for now
91-
aws s3 cp "$file" "s3://$S3_BUCKET/buildingsync-website/$RUN_DATE/"
92-
echo "[SUCCESS]-PostgreSQL-uploaded-to-s3://$S3_BUCKET/buildingsync-website/$RUN_DATE/$(basename "$file")"
93-
fi
94-
done
95-
96-
for file in "$BACKUP_DIR"/*.tgz
97-
do
98-
echo "Backing up $file to $S3_BUCKET/buildingsync-website/$RUN_DATE/"
99-
100-
if [ ! -s "$file" ]; then
101-
# the file is empty, send an error
102-
echo "[ERROR]-Mediadata-backup-file-was-empty-or-missing"
103-
else
104-
# can't pass spaces to slack notifications, for now
105-
aws s3 cp "$file" "s3://$S3_BUCKET/buildingsync-website/$RUN_DATE/"
106-
echo "[SUCCESS]-Mediadata-uploaded-to-s3://$S3_BUCKET/buildingsync-website/$RUN_DATE/$(basename "$file")"
107-
fi
108-
done
80+
# Delete backup directories older than 30 days
81+
find ${BACKUP_BASE_DIR} -maxdepth 1 -type d -mtime +30 -name "20*" -exec rm -rf {} \;
82+
83+
# Upload the current date's backup folder to S3 using sync
84+
echo "Syncing backup folder $BACKUP_DIR to s3://$S3_BUCKET/buildingsync-website/$RUN_DATE/"
85+
86+
# Check if backup files exist
87+
if [ ! "$(ls -A $BACKUP_DIR)" ]; then
88+
echo "[ERROR]-No backup files were created"
89+
exit 1
90+
fi
91+
92+
# Sync the entire date folder to S3
93+
aws s3 sync "$BACKUP_DIR" "s3://$S3_BUCKET/buildingsync-website/$RUN_DATE/" --delete
94+
95+
if [ $? -eq 0 ]; then
96+
echo "[SUCCESS]-Backup folder synced to s3://$S3_BUCKET/buildingsync-website/$RUN_DATE/"
97+
echo "[SUCCESS]-Files uploaded: $(ls -la "$BACKUP_DIR")"
98+
else
99+
echo "[ERROR]-Failed to sync backup folder to S3"
100+
exit 1
101+
fi

0 commit comments

Comments
 (0)