commit 387fa38e656f21bad06dcab2ea97b440a85196bc Author: Khairul Hidayat Date: Fri May 10 09:43:18 2024 +0700 chore: init project diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c07ea0f --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules/ +pnpm-lock.yaml diff --git a/.npmrc b/.npmrc new file mode 100644 index 0000000..ae90f70 --- /dev/null +++ b/.npmrc @@ -0,0 +1 @@ +ignore-workspace-root-check=true diff --git a/backend/.gitignore b/backend/.gitignore new file mode 100644 index 0000000..f3f23b8 --- /dev/null +++ b/backend/.gitignore @@ -0,0 +1,5 @@ +dist/ +node_modules/ +storage/ +package-lock.json +bun.lockb diff --git a/backend/Dockerfile.dev b/backend/Dockerfile.dev new file mode 100644 index 0000000..52a3e35 --- /dev/null +++ b/backend/Dockerfile.dev @@ -0,0 +1,24 @@ +FROM alpine:3.19.0 + +ENV GLIBC_VERSION 2.34-r0 + +WORKDIR /app + +# Install bun +ADD https://github.com/oven-sh/bun/releases/latest/download/bun-linux-x64.zip bun-linux-x64.zip +RUN apk add --no-cache --update unzip curl && \ + curl -Lo /etc/apk/keys/sgerrand.rsa.pub https://alpine-pkgs.sgerrand.com/sgerrand.rsa.pub && \ + curl -Lo glibc.apk "https://github.com/sgerrand/alpine-pkg-glibc/releases/download/${GLIBC_VERSION}/glibc-${GLIBC_VERSION}.apk" && \ + curl -Lo glibc-bin.apk "https://github.com/sgerrand/alpine-pkg-glibc/releases/download/${GLIBC_VERSION}/glibc-bin-${GLIBC_VERSION}.apk" && \ + apk add --force-overwrite glibc-bin.apk glibc.apk && \ + /usr/glibc-compat/sbin/ldconfig /lib /usr/glibc-compat/lib && \ + echo 'hosts: files mdns4_minimal [NOTFOUND=return] dns mdns4' >> /etc/nsswitch.conf && \ + apk del curl && \ + rm -rf /var/cache/apk/* glibc.apk glibc-bin.apk + +RUN unzip bun-linux-x64.zip && chmod +x ./bun-linux-x64/bun && mv ./bun-linux-x64/bun /usr/bin && rm -f bun-linux-x64.zip + +# Add db clients +RUN apk --no-cache add postgresql16-client + +ENTRYPOINT ["bun", "run", "dev"] diff --git a/backend/Dockerfile.dev.bak b/backend/Dockerfile.dev.bak new file mode 100644 index 0000000..67ad9ed --- /dev/null +++ b/backend/Dockerfile.dev.bak @@ -0,0 +1,28 @@ +FROM alpine:3.19 +WORKDIR /app + +ENV GLIBC_VERSION 2.35-r1 + +RUN apk update && \ + apk add --no-cache --update unzip curl + # curl -Lo /etc/apk/keys/sgerrand.rsa.pub https://alpine-pkgs.sgerrand.com/sgerrand.rsa.pub && \ + # curl -Lo glibc.apk "https://github.com/sgerrand/alpine-pkg-glibc/releases/download/${GLIBC_VERSION}/glibc-${GLIBC_VERSION}.apk" && \ + # curl -Lo glibc-bin.apk "https://github.com/sgerrand/alpine-pkg-glibc/releases/download/${GLIBC_VERSION}/glibc-bin-${GLIBC_VERSION}.apk" && \ + # apk add --force-overwrite glibc-bin.apk glibc.apk && \ + # /usr/glibc-compat/sbin/ldconfig /lib /usr/glibc-compat/lib && \ + # echo 'hosts: files mdns4_minimal [NOTFOUND=return] dns mdns4' >> /etc/nsswitch.conf && \ + # apk del curl && \ + # rm -rf /var/cache/apk/* glibc.apk glibc-bin.apk + +ADD https://github.com/oven-sh/bun/releases/latest/download/bun-linux-x64.zip bun-linux-x64.zip +# RUN unzip bun-linux-x64.zip && chmod +x ./bun-linux-x64/bun && mv ./bun-linux-x64/bun /usr/local/bin && rm -rf bun-linux-x64.zip +RUN unzip bun-linux-x64.zip && ls bun-linux-x64 && ./bun-linux-x64/bun --version + +RUN chmod +x /usr/local/bin/bun +RUN /usr/local/bin/bun --version + +# CMD ["bun", "--version"] + +# RUN apk --no-cache add postgresql16-client + +# ENTRYPOINT ["bun", "run", "dev"] diff --git a/backend/README.md b/backend/README.md new file mode 100644 index 0000000..8651121 --- /dev/null +++ b/backend/README.md @@ -0,0 +1,15 @@ +# backend + +To install dependencies: + +```bash +bun install +``` + +To run: + +```bash +bun run index.ts +``` + +This project was created using `bun init` in bun v1.1.7. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime. diff --git a/backend/docker-compose.dev.yml b/backend/docker-compose.dev.yml new file mode 100644 index 0000000..41efbfc --- /dev/null +++ b/backend/docker-compose.dev.yml @@ -0,0 +1,12 @@ +version: "3" + +services: + backend: + container_name: db-backup-be + build: + context: . + dockerfile: Dockerfile.dev + volumes: + - ./:/app:rw + extra_hosts: + - "host.docker.internal:host-gateway" diff --git a/backend/index.ts b/backend/index.ts new file mode 100644 index 0000000..31841b5 --- /dev/null +++ b/backend/index.ts @@ -0,0 +1,35 @@ +import DatabaseUtil from "@/lib/database"; +import { DOCKER_HOST, STORAGE_DIR } from "@/utility/consts"; +import { mkdir } from "@/utility/utils"; +import path from "path"; + +const main = async () => { + try { + const db = new DatabaseUtil({ + type: "postgres", + host: DOCKER_HOST, + user: "postgres", + pass: "postgres", + port: 5432, + }); + + const databases = await db.getDatabases(); + console.log(databases); + + const dbName = "test"; + + // Create backup + const outDir = path.join(STORAGE_DIR, db.config.host, dbName); + mkdir(outDir); + const outFile = path.join(outDir, `/${Date.now()}.tar`); + console.log(await db.dump(dbName, outFile)); + console.log(outFile); + + // Restore backup + console.log(await db.restore(outFile)); + } catch (err) { + console.log((err as any).message); + } +}; + +main(); diff --git a/backend/package.json b/backend/package.json new file mode 100644 index 0000000..ca2c39a --- /dev/null +++ b/backend/package.json @@ -0,0 +1,17 @@ +{ + "name": "backend", + "module": "index.ts", + "type": "module", + "scripts": { + "dev": "bun --watch index.ts", + "dev:compose": "docker compose -f docker-compose.dev.yml up --build", + "build": "bun build index.ts --outdir dist --target bun", + "start": "bun dist/index.js" + }, + "devDependencies": { + "@types/bun": "latest" + }, + "peerDependencies": { + "typescript": "^5.0.0" + } +} \ No newline at end of file diff --git a/backend/src/dbms/base.ts b/backend/src/dbms/base.ts new file mode 100644 index 0000000..5a5a2b0 --- /dev/null +++ b/backend/src/dbms/base.ts @@ -0,0 +1,17 @@ +import type { DatabaseListItem } from "../types/database.types"; + +class BaseDbms { + async getDatabases(): Promise { + return []; + } + + async dump(_dbName: string, _path: string): Promise { + return ""; + } + + async restore(_path: string): Promise { + return ""; + } +} + +export default BaseDbms; diff --git a/backend/src/dbms/postgres.ts b/backend/src/dbms/postgres.ts new file mode 100644 index 0000000..899d8e0 --- /dev/null +++ b/backend/src/dbms/postgres.ts @@ -0,0 +1,49 @@ +import type { DatabaseListItem, PostgresConfig } from "../types/database.types"; +import { exec } from "../utility/process"; +import BaseDbms from "./base"; + +class PostgresDbms extends BaseDbms { + constructor(private config: PostgresConfig) { + super(); + } + + async getDatabases() { + return this.sql( + "SELECT datname AS name, pg_size_pretty(pg_database_size(datname)) AS size \ + FROM pg_database WHERE datistemplate=false ORDER BY name ASC" + ); + } + + async dump(dbName: string, path: string) { + return exec(["pg_dump", this.dbUrl + `/${dbName}`, "-Ftar", "-f", path]); + } + + async restore(path: string) { + return exec([ + "pg_restore", + "-d", + this.dbUrl, + "-cC", + "--if-exists", + "--exit-on-error", + "-Ftar", + path, + ]); + } + + private async sql(query: string) { + const sql = `SELECT row_to_json(row) FROM (${query}) row`; + return exec(["psql", this.dbUrl, "-t", "-c", sql]) + .then((res) => res.split("\n").map((i) => i.trim())) + .then((res) => res.filter((i) => i.length > 0)) + .then((i) => i.map((data) => JSON.parse(data) as T)); + } + + private get dbUrl() { + const { user, pass, host } = this.config; + const port = this.config.port || 5432; + return `postgresql://${user}:${pass}@${host}:${port}`; + } +} + +export default PostgresDbms; diff --git a/backend/src/lib/database.ts b/backend/src/lib/database.ts new file mode 100644 index 0000000..b7d0f6f --- /dev/null +++ b/backend/src/lib/database.ts @@ -0,0 +1,31 @@ +import BaseDbms from "../dbms/base"; +import PostgresDbms from "../dbms/postgres"; +import type { DatabaseConfig, DatabaseListItem } from "../types/database.types"; + +class DatabaseUtil { + private db = new BaseDbms(); + + constructor(public config: DatabaseConfig) { + switch (config.type) { + case "postgres": + this.db = new PostgresDbms(config); + break; + default: + throw Error("Database type not supported: " + config.type); + } + } + + async getDatabases(): Promise { + return this.db.getDatabases(); + } + + async dump(dbName: string, path: string): Promise { + return this.db.dump(dbName, path); + } + + async restore(path: string): Promise { + return this.db.restore(path); + } +} + +export default DatabaseUtil; diff --git a/backend/src/types/database.types.ts b/backend/src/types/database.types.ts new file mode 100644 index 0000000..8fa724c --- /dev/null +++ b/backend/src/types/database.types.ts @@ -0,0 +1,14 @@ +export type DatabaseConfig = PostgresConfig; + +export type PostgresConfig = { + type: "postgres"; + host: string; + user: string; + pass: string; + port?: number; +}; + +export type DatabaseListItem = { + name: string; + size: string; +}; diff --git a/backend/src/utility/consts.ts b/backend/src/utility/consts.ts new file mode 100644 index 0000000..33d3a26 --- /dev/null +++ b/backend/src/utility/consts.ts @@ -0,0 +1,4 @@ +import path from "path"; + +export const DOCKER_HOST = "host.docker.internal"; +export const STORAGE_DIR = path.resolve(__dirname, "../../storage"); diff --git a/backend/src/utility/process.ts b/backend/src/utility/process.ts new file mode 100644 index 0000000..cce635b --- /dev/null +++ b/backend/src/utility/process.ts @@ -0,0 +1,23 @@ +type ExecOptions = { + env?: any; + // TODO: add ssh wrapper + ssh?: any; +}; + +export const exec = async ( + cmds: string[], + options: Partial = {} +) => { + const proc = Bun.spawn(cmds, { + env: options.env, + stderr: "pipe", + }); + const err = await new Response(proc.stderr).text(); + const res = await new Response(proc.stdout).text(); + + if (err) { + throw new Error(err); + } + + return res; +}; diff --git a/backend/src/utility/utils.ts b/backend/src/utility/utils.ts new file mode 100644 index 0000000..a42188d --- /dev/null +++ b/backend/src/utility/utils.ts @@ -0,0 +1,7 @@ +import fs from "fs"; + +export const mkdir = (dir: string) => { + if (!fs.existsSync(dir)) { + fs.mkdirSync(dir, { recursive: true }); + } +}; diff --git a/backend/tsconfig.json b/backend/tsconfig.json new file mode 100644 index 0000000..6138132 --- /dev/null +++ b/backend/tsconfig.json @@ -0,0 +1,32 @@ +{ + "compilerOptions": { + // Enable latest features + "lib": ["ESNext", "DOM"], + "target": "ESNext", + "module": "ESNext", + "moduleDetection": "force", + "jsx": "react-jsx", + "allowJs": true, + + // Bundler mode + "moduleResolution": "bundler", + "allowImportingTsExtensions": true, + "verbatimModuleSyntax": true, + "noEmit": true, + + // Best practices + "strict": true, + "skipLibCheck": true, + "noFallthroughCasesInSwitch": true, + + // Some stricter flags (disabled by default) + "noUnusedLocals": false, + "noUnusedParameters": false, + "noPropertyAccessFromIndexSignature": false, + + "baseUrl": ".", + "paths": { + "@/*": ["src/*"] + } + } +} diff --git a/frontend/.eslintrc.cjs b/frontend/.eslintrc.cjs new file mode 100644 index 0000000..d6c9537 --- /dev/null +++ b/frontend/.eslintrc.cjs @@ -0,0 +1,18 @@ +module.exports = { + root: true, + env: { browser: true, es2020: true }, + extends: [ + 'eslint:recommended', + 'plugin:@typescript-eslint/recommended', + 'plugin:react-hooks/recommended', + ], + ignorePatterns: ['dist', '.eslintrc.cjs'], + parser: '@typescript-eslint/parser', + plugins: ['react-refresh'], + rules: { + 'react-refresh/only-export-components': [ + 'warn', + { allowConstantExport: true }, + ], + }, +} diff --git a/frontend/.gitignore b/frontend/.gitignore new file mode 100644 index 0000000..a547bf3 --- /dev/null +++ b/frontend/.gitignore @@ -0,0 +1,24 @@ +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* +pnpm-debug.log* +lerna-debug.log* + +node_modules +dist +dist-ssr +*.local + +# Editor directories and files +.vscode/* +!.vscode/extensions.json +.idea +.DS_Store +*.suo +*.ntvs* +*.njsproj +*.sln +*.sw? diff --git a/frontend/README.md b/frontend/README.md new file mode 100644 index 0000000..0d6babe --- /dev/null +++ b/frontend/README.md @@ -0,0 +1,30 @@ +# React + TypeScript + Vite + +This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules. + +Currently, two official plugins are available: + +- [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react/README.md) uses [Babel](https://babeljs.io/) for Fast Refresh +- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh + +## Expanding the ESLint configuration + +If you are developing a production application, we recommend updating the configuration to enable type aware lint rules: + +- Configure the top-level `parserOptions` property like this: + +```js +export default { + // other rules... + parserOptions: { + ecmaVersion: 'latest', + sourceType: 'module', + project: ['./tsconfig.json', './tsconfig.node.json'], + tsconfigRootDir: __dirname, + }, +} +``` + +- Replace `plugin:@typescript-eslint/recommended` to `plugin:@typescript-eslint/recommended-type-checked` or `plugin:@typescript-eslint/strict-type-checked` +- Optionally add `plugin:@typescript-eslint/stylistic-type-checked` +- Install [eslint-plugin-react](https://github.com/jsx-eslint/eslint-plugin-react) and add `plugin:react/recommended` & `plugin:react/jsx-runtime` to the `extends` list diff --git a/frontend/index.html b/frontend/index.html new file mode 100644 index 0000000..e4b78ea --- /dev/null +++ b/frontend/index.html @@ -0,0 +1,13 @@ + + + + + + + Vite + React + TS + + +
+ + + diff --git a/frontend/package.json b/frontend/package.json new file mode 100644 index 0000000..97894a9 --- /dev/null +++ b/frontend/package.json @@ -0,0 +1,28 @@ +{ + "name": "frontend", + "private": true, + "version": "0.0.0", + "type": "module", + "scripts": { + "dev": "vite", + "build": "tsc && vite build", + "lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0", + "preview": "vite preview" + }, + "dependencies": { + "react": "^18.2.0", + "react-dom": "^18.2.0" + }, + "devDependencies": { + "@types/react": "^18.2.66", + "@types/react-dom": "^18.2.22", + "@typescript-eslint/eslint-plugin": "^7.2.0", + "@typescript-eslint/parser": "^7.2.0", + "@vitejs/plugin-react-swc": "^3.5.0", + "eslint": "^8.57.0", + "eslint-plugin-react-hooks": "^4.6.0", + "eslint-plugin-react-refresh": "^0.4.6", + "typescript": "^5.2.2", + "vite": "^5.2.0" + } +} diff --git a/frontend/public/vite.svg b/frontend/public/vite.svg new file mode 100644 index 0000000..e7b8dfb --- /dev/null +++ b/frontend/public/vite.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/frontend/src/App.css b/frontend/src/App.css new file mode 100644 index 0000000..b9d355d --- /dev/null +++ b/frontend/src/App.css @@ -0,0 +1,42 @@ +#root { + max-width: 1280px; + margin: 0 auto; + padding: 2rem; + text-align: center; +} + +.logo { + height: 6em; + padding: 1.5em; + will-change: filter; + transition: filter 300ms; +} +.logo:hover { + filter: drop-shadow(0 0 2em #646cffaa); +} +.logo.react:hover { + filter: drop-shadow(0 0 2em #61dafbaa); +} + +@keyframes logo-spin { + from { + transform: rotate(0deg); + } + to { + transform: rotate(360deg); + } +} + +@media (prefers-reduced-motion: no-preference) { + a:nth-of-type(2) .logo { + animation: logo-spin infinite 20s linear; + } +} + +.card { + padding: 2em; +} + +.read-the-docs { + color: #888; +} diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx new file mode 100644 index 0000000..afe48ac --- /dev/null +++ b/frontend/src/App.tsx @@ -0,0 +1,35 @@ +import { useState } from 'react' +import reactLogo from './assets/react.svg' +import viteLogo from '/vite.svg' +import './App.css' + +function App() { + const [count, setCount] = useState(0) + + return ( + <> + +

Vite + React

+
+ +

+ Edit src/App.tsx and save to test HMR +

+
+

+ Click on the Vite and React logos to learn more +

+ + ) +} + +export default App diff --git a/frontend/src/assets/react.svg b/frontend/src/assets/react.svg new file mode 100644 index 0000000..6c87de9 --- /dev/null +++ b/frontend/src/assets/react.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/frontend/src/index.css b/frontend/src/index.css new file mode 100644 index 0000000..6119ad9 --- /dev/null +++ b/frontend/src/index.css @@ -0,0 +1,68 @@ +:root { + font-family: Inter, system-ui, Avenir, Helvetica, Arial, sans-serif; + line-height: 1.5; + font-weight: 400; + + color-scheme: light dark; + color: rgba(255, 255, 255, 0.87); + background-color: #242424; + + font-synthesis: none; + text-rendering: optimizeLegibility; + -webkit-font-smoothing: antialiased; + -moz-osx-font-smoothing: grayscale; +} + +a { + font-weight: 500; + color: #646cff; + text-decoration: inherit; +} +a:hover { + color: #535bf2; +} + +body { + margin: 0; + display: flex; + place-items: center; + min-width: 320px; + min-height: 100vh; +} + +h1 { + font-size: 3.2em; + line-height: 1.1; +} + +button { + border-radius: 8px; + border: 1px solid transparent; + padding: 0.6em 1.2em; + font-size: 1em; + font-weight: 500; + font-family: inherit; + background-color: #1a1a1a; + cursor: pointer; + transition: border-color 0.25s; +} +button:hover { + border-color: #646cff; +} +button:focus, +button:focus-visible { + outline: 4px auto -webkit-focus-ring-color; +} + +@media (prefers-color-scheme: light) { + :root { + color: #213547; + background-color: #ffffff; + } + a:hover { + color: #747bff; + } + button { + background-color: #f9f9f9; + } +} diff --git a/frontend/src/main.tsx b/frontend/src/main.tsx new file mode 100644 index 0000000..3d7150d --- /dev/null +++ b/frontend/src/main.tsx @@ -0,0 +1,10 @@ +import React from 'react' +import ReactDOM from 'react-dom/client' +import App from './App.tsx' +import './index.css' + +ReactDOM.createRoot(document.getElementById('root')!).render( + + + , +) diff --git a/frontend/src/vite-env.d.ts b/frontend/src/vite-env.d.ts new file mode 100644 index 0000000..11f02fe --- /dev/null +++ b/frontend/src/vite-env.d.ts @@ -0,0 +1 @@ +/// diff --git a/frontend/tsconfig.json b/frontend/tsconfig.json new file mode 100644 index 0000000..a7fc6fb --- /dev/null +++ b/frontend/tsconfig.json @@ -0,0 +1,25 @@ +{ + "compilerOptions": { + "target": "ES2020", + "useDefineForClassFields": true, + "lib": ["ES2020", "DOM", "DOM.Iterable"], + "module": "ESNext", + "skipLibCheck": true, + + /* Bundler mode */ + "moduleResolution": "bundler", + "allowImportingTsExtensions": true, + "resolveJsonModule": true, + "isolatedModules": true, + "noEmit": true, + "jsx": "react-jsx", + + /* Linting */ + "strict": true, + "noUnusedLocals": true, + "noUnusedParameters": true, + "noFallthroughCasesInSwitch": true + }, + "include": ["src"], + "references": [{ "path": "./tsconfig.node.json" }] +} diff --git a/frontend/tsconfig.node.json b/frontend/tsconfig.node.json new file mode 100644 index 0000000..97ede7e --- /dev/null +++ b/frontend/tsconfig.node.json @@ -0,0 +1,11 @@ +{ + "compilerOptions": { + "composite": true, + "skipLibCheck": true, + "module": "ESNext", + "moduleResolution": "bundler", + "allowSyntheticDefaultImports": true, + "strict": true + }, + "include": ["vite.config.ts"] +} diff --git a/frontend/vite.config.ts b/frontend/vite.config.ts new file mode 100644 index 0000000..861b04b --- /dev/null +++ b/frontend/vite.config.ts @@ -0,0 +1,7 @@ +import { defineConfig } from 'vite' +import react from '@vitejs/plugin-react-swc' + +// https://vitejs.dev/config/ +export default defineConfig({ + plugins: [react()], +}) diff --git a/package.json b/package.json new file mode 100644 index 0000000..5448f93 --- /dev/null +++ b/package.json @@ -0,0 +1,17 @@ +{ + "name": "db-backup-tool", + "version": "0.1.0", + "author": { + "name": "Khairul Hidayat", + "email": "khai@rul.sh", + "url": "https://github.com/khairul169" + }, + "private": false, + "license": "MIT", + "scripts": { + "dev": "concurrently \"cd backend && pnpm dev\" \"cd frontend && pnpm dev\"" + }, + "devDependencies": { + "concurrently": "^8.2.2" + } +} diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml new file mode 100644 index 0000000..507e28e --- /dev/null +++ b/pnpm-workspace.yaml @@ -0,0 +1,3 @@ +packages: + - backend + - frontend