Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore/backup minio #51

Merged
merged 5 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ dist
pgdata/
minio_data/
dump/
minio-dump/

*.csv
*.sql
3 changes: 3 additions & 0 deletions docker-compose.dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ services:
- type: bind
source: ./dump
target: /tmp/dump
- type: bind
source: ./minio-dump
target: /tmp/minio-dump

minio:
container_name: interapp-minio
Expand Down
3 changes: 2 additions & 1 deletion docker-compose.prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ services:
context: ./interapp-backend
dockerfile: scheduler/prod.Dockerfile
env_file:
- ./interapp-backend/.env.development
- ./interapp-backend/.env.production
networks:
- interapp-network
depends_on:
Expand All @@ -52,6 +52,7 @@ services:
condition: service_healthy
volumes:
- ./dump:/tmp/dump
- ./minio-dump:/tmp/minio-dump
SebassNoob marked this conversation as resolved.
Show resolved Hide resolved

minio:
container_name: interapp-minio
Expand Down
13 changes: 13 additions & 0 deletions interapp-backend/scheduler/dev.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@ RUN echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg m
# update and install postgresql-client-16, tzdata
RUN apt-get update && apt-get install -y --fix-missing postgresql-client-16 tzdata && apt-get clean

RUN ARCH=$(case "$(uname -m)" in "x86_64") echo "amd64";; "ppc64le") echo "ppc64le";; *) echo "Unsupported architecture"; exit 1;; esac) && \
DOWNLOAD_URL=$(case "$ARCH" in "amd64") echo "https://dl.min.io/client/mc/release/linux-amd64/mc";; "ppc64le") echo "https://dl.min.io/client/mc/release/linux-ppc64le/mc";; *) echo "Unsupported architecture"; exit 1;; esac) && \
# Install wget to download MinIO client
apt-get update && apt-get install -y wget && \
# Download MinIO client binary
wget -O /usr/local/bin/mc "$DOWNLOAD_URL" && \
# Make MinIO client binary executable
chmod +x /usr/local/bin/mc && \
# Clean up
apt-get clean && rm -rf /var/lib/apt/lists/* && \
# Output success message
echo "MinIO client installed successfully."
SebassNoob marked this conversation as resolved.
Show resolved Hide resolved

ENV TZ=Asia/Singapore
RUN ln -snf /usr/share/zoneinfo/"$TZ" /etc/localtime && echo "$TZ" > /etc/timezone
RUN apt-get clean
Expand Down
13 changes: 13 additions & 0 deletions interapp-backend/scheduler/prod.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@ RUN echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg m
# update and install postgresql-client-16, tzdata
RUN apt-get update && apt-get install -y --fix-missing postgresql-client-16 tzdata && apt-get clean

RUN ARCH=$(case "$(uname -m)" in "x86_64") echo "amd64";; "ppc64le") echo "ppc64le";; *) echo "Unsupported architecture"; exit 1;; esac) && \
DOWNLOAD_URL=$(case "$ARCH" in "amd64") echo "https://dl.min.io/client/mc/release/linux-amd64/mc";; "ppc64le") echo "https://dl.min.io/client/mc/release/linux-ppc64le/mc";; *) echo "Unsupported architecture"; exit 1;; esac) && \
# Install wget to download MinIO client
apt-get update && apt-get install -y wget && \
# Download MinIO client binary
wget -O /usr/local/bin/mc "$DOWNLOAD_URL" && \
# Make MinIO client binary executable
chmod +x /usr/local/bin/mc && \
# Clean up
apt-get clean && rm -rf /var/lib/apt/lists/* && \
# Output success message
echo "MinIO client installed successfully."

ENV TZ=Asia/Singapore
RUN ln -snf /usr/share/zoneinfo/"$TZ" /etc/localtime && echo "$TZ" > /etc/timezone
RUN apt-get clean
Expand Down
46 changes: 45 additions & 1 deletion interapp-backend/scheduler/scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,55 @@ schedule('0 0 0 */1 * *', async () => {
await $`find ${path} -type f -mtime +7 -exec rm {} +`;

const d = new Date();
const fmted = `interapp_${d.toLocaleDateString('en-GB').replace(/\//g, '_')}`;
const fmted = `interapp_db_${d.toLocaleDateString('en-GB').replace(/\//g, '_')}`;

const newFile = `${path}/${fmted}.sql`;
await $`touch ${newFile}`;
await $`PGPASSWORD=postgres pg_dump -U postgres -a interapp -h interapp-postgres > ${newFile}`;

console.info('db snapshot taken at location: ', newFile);
});

// backup minio bucket
const requiredEnv = [
'MINIO_ENDPOINT',
'MINIO_ADDRESS',
'MINIO_ROOT_USER',
'MINIO_ROOT_PASSWORD',
'MINIO_BUCKETNAME',
];
const missingEnv = requiredEnv.filter((env) => !process.env[env]);
if (missingEnv.length > 0) {
console.error('Missing required environment variables: ', missingEnv);
process.exit(1);
}

const minioURL = `http://${process.env.MINIO_ENDPOINT}${process.env.MINIO_ADDRESS}`;
const minioAccessKey = process.env.MINIO_ROOT_USER as string;
const minioSecretKey = process.env.MINIO_ROOT_PASSWORD as string;
const minioBucketName = process.env.MINIO_BUCKETNAME as string;
const minioAliasName = 'minio';

const minioBackupTask = schedule(
'0 0 0 */1 * *',
async () => {
// this is mounted on the host machine -- see docker compose file
const path = '/tmp/minio-dump';
if (!existsSync(path)) mkdirSync(path);

const d = new Date();
const fmted = `interapp_minio_${d.toLocaleDateString('en-GB').replace(/\//g, '_')}`;

const newFile = `${path}/${fmted}.tar.gz`;

// create a new backup
await $`mc mirror ${minioAliasName}/${minioBucketName} /tmp/minio-dump/temp`;
await $`cd /tmp && tar -cvf ${newFile} minio-dump/temp`;
await $`rm -rf /tmp/minio-dump/temp`;
},
{ scheduled: false },
);

// set up mc alias first
await $`mc alias set ${minioAliasName} ${minioURL} ${minioAccessKey} ${minioSecretKey}`;
minioBackupTask.start();
SebassNoob marked this conversation as resolved.
Show resolved Hide resolved