---
# System prepended metadata

title: Download remote db and restore it to your local
tags: [db]

---

---
title: Download remote db and restore it to your local
tags: db
---

## CLI to download `tms-dev`

download database `tms-dev`

```bash=
FILENAME=`gsutil ls -l gs://fio-db-dev/tms_dev | sort -k 2 | tail -n 2 | head -n 1 | awk '{print $NF}'`
gsutil cp $FILENAME tms_dev-hourly-UTC8.gz
```
### If you get 401 error, please do login first

```bash=
gcloud config
```

1. 複製網址，透過 browser 打開、授權，Google 會給你一段 code ，請複製起來，貼到你的 command line 小黑窗 
2. 設定 project id: `hardy-thinker-257409`
3. 是否提供使用經驗給 Google？ y/N

設定完成，測試權限：

```bash=
gsutil ls gs://fio-db-dev
```

## Install

### gsutil
- gsutil 介紹
https://cloud.google.com/storage/docs/gsutil
- gsutil 下載
https://cloud.google.com/storage/docs/gsutil_install

- gsutil 指令
    - ls : 可以查看我們這個 bucket 下面有什麼檔案
        - `gsutil ls gs://fio-db-dev` 
    - cp : 下載資裡庫
        -  `gsutil cp gs://fio-db-dev/tms_dev/tms_dev-hourly-UTC8.gz ${本地端路徑}`
command allows you to copy data between your local file system and the cloud https://cloud.google.com/storage/docs/gsutil/commands/cp


### mongoDB
- install
    - macOS
https://www.mongodb.com/docs/manual/tutorial/install-mongodb-on-os-x/

- run by brew
`brew services start mongodb-community@5.0`
- run in background
    - intel chip
    `mongod --config /usr/local/etc/mongod.conf --fork`
    - M1 chip
    `mongod --config /opt/homebrew/etc/mongod.conf --fork`
- restore gzip
`mongorestore --gzip --archive=tms_dev-hourly-UTC8.gz --nsInclude="tms_dev.*"`
- nsInclude="資料庫名稱.collection" *代表全部
#### 注意: restore 時, 資料庫名稱必須相同, nsInclude="tms_dev.*"

- drop
    - 加上 --drop 可以先清空裡面的 collenction 然後 restore
    - https://www.mongodb.com/docs/v2.4/reference/program/mongorestore/#cmdoption-mongorestore-drop 


### mongoDB 可能遇到問題
- 路徑不對
- 預設路徑
https://stackoverflow.com/questions/13827915/location-of-the-mongodb-database-on-mac
- 解法參考資料
https://www.mongodb.com/community/forums/t/error-couldnt-connect-to-server-127-0-0-1-27017/705/10

### homebrew 可能遇到問題

- homebrew intel 跟 m1 晶片會安裝在不一樣的資料夾下, 需要重新安裝一次並且將 PATH 設定成新的才能正常運作

- How to migrate to native Homebrew on an M1 Mac
https://blog.smittytone.net/2021/02/07/how-to-migrate-to-native-homebrew-on-an-m1-mac/


