最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

sqlite - How do I synchronize two databases? - Stack Overflow

programmeradmin1浏览0评论

I need to regularly synchronize two SQLite databases. One on a Raspberry Pi 4 running Node.js/NestJS (always on but without internet connection), another provided to the Raspberry Pi 4 via a USB drive from a dev system.

I want to connect the USB drive to the Raspberry Pi, wait for a while and unplug it knowing it got synchronized. I will run a NestJS cron job checking for the USB drive using libusb. My problem is the synchronization.

My ideas:

  • Running a second NestJS instance which dynamically uses the database whenever it is on and then calls an internal synchronization REST-endpoint/CLI tool on the Raspberry Pi's main NestJS instance.
  • Making the NestJS instance access the second database and run synchronization logic on it.
  • Using some magic synchronize SQLite databases command line tool that I do not know.

I need to regularly synchronize two SQLite databases. One on a Raspberry Pi 4 running Node.js/NestJS (always on but without internet connection), another provided to the Raspberry Pi 4 via a USB drive from a dev system.

I want to connect the USB drive to the Raspberry Pi, wait for a while and unplug it knowing it got synchronized. I will run a NestJS cron job checking for the USB drive using libusb. My problem is the synchronization.

My ideas:

  • Running a second NestJS instance which dynamically uses the database whenever it is on and then calls an internal synchronization REST-endpoint/CLI tool on the Raspberry Pi's main NestJS instance.
  • Making the NestJS instance access the second database and run synchronization logic on it.
  • Using some magic synchronize SQLite databases command line tool that I do not know.
Share Improve this question edited Feb 8 at 3:21 user4157124 3,00214 gold badges31 silver badges46 bronze badges asked Feb 4 at 15:14 Andresch SerjAndresch Serj 37.5k16 gold badges61 silver badges104 bronze badges 2
  • if one db is the master db then you might just copy the sqilte file? – jhole Commented Feb 4 at 15:20
  • No, sadly not. In the one on the raspberry pi, data might change, on the one on the usb, datan and even structure might change. – Andresch Serj Commented Feb 4 at 19:08
Add a comment  | 

1 Answer 1

Reset to default 1

With sqldiff you could plug in the USB drive and run:

$ sqldiff /usb/my_db.db primary.db > changes.sql

This produces an SQL file that if ran on the database file on your USB drive should result in a replicated primary database file.

sqlite3 /usb/my_db.db < changes.sql should do the trick, but if you change the file on your USB drive it may not work reliably if there are conflicts.

发布评论

评论列表(0)

  1. 暂无评论