# Tự động sao lưu cơ sở dữ liệu lên Object Storage tại X-OR Cloud

## Mô hình triển khai

<figure><img src="/files/Vs2NkkP8Nwu8g2qaCCqf" alt=""><figcaption></figcaption></figure>

## Yêu cầu

* Network của các host phải được truy cập với nhau
* vCPU / vRAM : 2c / 4G ( Tùy thuộc vào dung lượng database )
* Host yêu cầu cài đặt Mysql , Mongodb
* Có cài đặt NodeJS ( version > 14 )
* OS của host triển khai : Ubuntu 20&#x20;

## Hướng dẫn sử dụng

* Cài đặt NodeJS

```bash
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -

apt install nodejs -y
```

* Tải project về từ git của X-OR Cloud

```bash
git clone https://github.com/X-OR-Cloud/xor-backup-db-s3-destination.git
```

* Cài đặt các package yêu cầu

```bash
npm install -g pm2
```

* Khai báo env

```bash
cd xor-backup-db-s3-destination
```

```bash
vim .env
```

* S3\_ACCESS\_KEY và S3\_SECRET\_KEY lấy tại : <https://data.x-or.cloud>
* TELEGRAM\_BOT\_TOKEN, TELEGRAM\_ROOM\_ID để gửi thông báo về Telegram mỗi khi backup thành công ( Không yêu cầu )
* TIME\_START\_BACKUP\_MYSQL, TIME\_START\_BACKUP\_MONGODB là thời gian backup, sử dụng time của cron ( minute - hour - day(mon) - month - day(week) )

{% hint style="warning" %}
Khi tạo bucket tại X-OR Storage , nên chọn `Object lock`&#x20;
{% endhint %}

{% hint style="warning" %}
Chỉ nên sử dụng user của database có quyền readOnly
{% endhint %}

```
APP_NAME="XOR Database Backup S3 Destination"

API_PORT=8000

S3_HOST="https://s3.xorcloud.net"
S3_ACCESS_KEY=""
S3_SECRET_KEY=""
S3_REGION=default
S3_BUCKET=""

TELEGRAM_BOT_TOKEN=
TELEGRAM_ROOM_ID=

MYSQL_HOST=
MYSQL_PORT=
MYSQL_USERNAME=
MYSQL_PASSWORD=
TIME_START_BACKUP_MYSQL="0 0 * * *"

MONGODB_HOST=
MONGODB_PORT=
MONGODB_USERNAME=
MONGODB_PASSWORD=
TIME_START_BACKUP_MONGODB="0 0 * * *"
```

* Khởi dạy dự án

```bash
npm install
pm2 start "npm start" --name backup
```

* Xem logs khi chạy&#x20;

```bash
tail -f ./logs.txt
```

<figure><img src="/files/yRi7gLbp7iLoThniSct1" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.x-or.cloud/tutorials/tu-dong-sao-luu-co-so-du-lieu-len-object-storage-tai-x-or-cloud.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
