The node ID is the same by ipfs.create() in 2 executions

I ran the example on ipfg github for node creation. Here is the code:

    const node = await IPFS.create()

Then executing the code twice. Here is the output:

PS C:\d\code\js\ipat> node index.js
Swarm listening on /ip4/192.168.2.133/tcp/4002/ipfs/QmVxfGtbh95P1yiNVL9TmEm1tZm7SjcFbLo6UbG6SEfHJc
Swarm listening on /ip4/127.0.0.1/tcp/4002/ipfs/QmVx1fGtbh95P1yiNVL9TmEm1tZm7SjcFbLo6UbG6SEfHJc
Swarm listening on /ip4/127.0.0.1/tcp/4003/ws/ipfs/QmVxfGtbh95P1yiNVL9TmEm1tZm7SjcFbLo6UbG6SEfHJc
Swarm listening on /p2p-circuit/ipfs/QmVx1fGtbh95P1yiNVL9TmEm1tZm7SjcFbLo6UbG6SEfHJc
Swarm listening on /p2p-circuit/ip4/192.168.2.133/tcp/4002/ipfs/QmVx1fGtbh95P1yiNVL9TmEm1tZm7SjcFbLo6UbG6SEfHJc
Swarm listening on /p2p-circuit/ip4/127.0.0.1/tcp/4002/ipfs/QmVx1fGtbh95P1yiNVL9TmEm1tZm7SjcFbLo6UbG6SEfHJc
Swarm listening on /p2p-circuit/ip4/127.0.0.1/tcp/4003/ws/ipfs/QmVx1fGtbh95P1yiNVL9TmEm1tZm7SjcFbLo6UbG6SEfHJc
Version: 0.40.0
Added file: hello.txt QmP93Hjdi17SKyDgfipEhZsPrAYbZjP1kHCEHVxzfGEGuc
Added file contents : Hello World of Antique Trading
PS C:\d\code\js\ipat> node --version
v12.14.0
PS C:\d\code\js\ipat> node index.js
Swarm listening on /ip4/192.168.2.133/tcp/4002/ipfs/QmVx1fGtbh95P1yiNVL9TmEm1tZm7SjcFbLo6UbG6SEfHJc
Swarm listening on /ip4/127.0.0.1/tcp/4002/ipfs/QmVx1fGtbh95P1yiNVL9TmEm1tZm7SjcFbLo6UbG6SEfHJc
Swarm listening on /ip4/127.0.0.1/tcp/4003/ws/ipfs/QmVx1fGtbh95P1yiNVL9TmEm1tZm7SjcFbLo6UbG6SEfHJc
Swarm listening on /p2p-circuit/ipfs/QmVxfGtbh95P1yiNVL9TmEm1tZm7SjcFbLo6UbG6SEfHJc
Swarm listening on /p2p-circuit/ip4/192.168.2.133/tcp/4002/ipfs/QmVxfGtbh95P1yiNVL9TmEm1tZm7SjcFbLo6UbG6SEfHJc
Swarm listening on /p2p-circuit/ip4/127.0.0.1/tcp/4002/ipfs/QmVx1fGtbh95P1yiNVL9TmEm1tZm7SjcFbLo6UbG6SEfHJc
Swarm listening on /p2p-circuit/ip4/127.0.0.1/tcp/4003/ws/ipfs/QmVx1fGtbh95P1yiNVL9TmEm1tZm7SjcFbLo6UbG6SEfHJc
Version: 0.40.0
Added file: hello.txt QmP93Hjdi17SKyDgfipEhZsPrAYbZjP1kHCEHVxzfGEGuc
Added file contents : Hello World of Antique Trading

To my surprise, the node id from two executions is the same. Should the node id be randomly generated and not be the same?

Randomly generated the first time (on init), then afterward the node id (peer id) should not change. That is your node’s identity to the ipfs/p2p network from then on. Oddly enough, some of your hashes are missing ‘1’ … like QmVx1fGt vs QmVxfGt … but then the rest of hash is complete

It seems that the node id get stored somewhere on the device. Just for example, If I wipe out the OS system and reinstall it again, then execute the ipfs.create(), is the node id remain unchanged? Or a new node id will be generated.

In your user’s .ipfs directory, it’s stored in ‘config’ file. If you wipe out OS then it would have to regen a new node id when you ipfs init for the first time. Understand that if you ‘add’ anything to your own ipfs node, but then wipe out your node later, then your files/work is likely gone, too.

It would be hard if all the files/works is gone with the system wipeout or hardware grade. Is there any tools helping with the graceful migration or backup/restore?

Backup/migrate the .ipfs directory. Backup any mounted filesystems.