Skip to content

Commit 4731441

Browse files
readme
1 parent 2cb39bb commit 4731441

File tree

9 files changed

+149
-38
lines changed

9 files changed

+149
-38
lines changed

rust/canister-snapshot-download/Cargo.lock

Lines changed: 19 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[workspace]
22
members = [
3-
"src/wotd"
3+
"src/quotes"
44
]
55
resolver = "2"

rust/canister-snapshot-download/Makefile

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,23 @@ build:
1010
.PHONY: install
1111
.SILENT: install
1212
install: build
13-
dfx canister install wotd
13+
dfx canister install quotes
1414

1515
.PHONY: upgrade
1616
.SILENT: upgrade
1717
upgrade: build
18-
dfx canister install wotd --mode=upgrade
18+
dfx canister install quotes --mode=upgrade
1919

2020
.PHONY: deploy
2121
.SILENT: deploy
2222
deploy:
23-
dfx deploy wotd
23+
dfx deploy quotes
2424

2525
.PHONY: test
2626
.SILENT: test
2727
test: deploy
28-
dfx canister call wotd setup | grep -qw '()' && echo 'PASS'
29-
dfx canister call wotd print | grep -qw 'Colourless green ideas sleep furiously.' && echo 'PASS'
28+
dfx canister call quotes setup | grep -qw '()' && echo 'PASS'
29+
dfx canister call quotes print | grep -qw 'Colourless green ideas sleep furiously.' && echo 'PASS'
3030

3131
.PHONY: clean
3232
.SILENT: clean
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# Canister snapshot download and upload
2+
3+
This example demonstrates the process of downloading and uploading canister snapshots. It features a canister called `quotes` which has some faulty data in its stable memory. You may think of it as data corrupted during a data migration or something similar. For the purposes of this example, it's simply a quote with a typo.
4+
5+
The steps in this readme can be run all in sequence by invoking the `run.sh` script in bash.
6+
7+
## Prerequisites
8+
9+
This example requires an installation of:
10+
11+
- [x] Install the [IC SDK](https://internetcomputer.org/docs/current/developer-docs/getting-started/install). Note: the Canister Snapshots feature requires `dfx` version `0.23.0-beta.3` or later.
12+
- [x] Clone the example dapp project: `git clone https://github.com/dfinity/examples`
13+
14+
Begin by opening a terminal window.
15+
16+
## Step 1: Setup the project environment
17+
18+
Navigate into the folder containing the project's files and start a local instance of the Internet Computer with the commands:
19+
20+
```bash
21+
cd examples/rust/canister-snapshot-download
22+
dfx start --clean --background
23+
```
24+
25+
## Step 2: Compile and deploy `quotes` canister
26+
27+
```bash
28+
dfx deploy
29+
```
30+
31+
Also setup its initial state:
32+
33+
```bash
34+
dfx canister call quotes "setup"
35+
```
36+
37+
And check what the `print` endpoint returns:
38+
39+
```bash
40+
dfx canister call quotes "print"
41+
```
42+
43+
Output:
44+
```bash
45+
Colourless green ideas sleep furiously.
46+
```
47+
48+
Clearly, this British spelling is not correct, as the author of the quote is American. Let's fix it by downloading the canister state, fixing the stable memory and loading the fixed version.
49+
50+
## Step 3: Create and download snapshot
51+
52+
```bash
53+
dfx canister stop quotes
54+
dfx canister snapshot create quotes
55+
```
56+
57+
This returns a snapshot id, similar to
58+
```bash
59+
0000000000000000ffffffffff9000010101
60+
```
61+
62+
Create a local directory and download the new snapshot:
63+
```bash
64+
mkdir ./snapshots
65+
dfx canister snapshot download --dir ./snapshots quotes 0000000000000000ffffffffff9000010101
66+
```
67+
68+
## Step 4: Manipulate and upload the snapshot
69+
View the file in ./snapshot/stable_memory.bin. This is a binary file, but the first few bytes are ASCII characters.
70+
71+
Fix the spelling by running:
72+
```bash
73+
sed -i -e 's/Colour/Color/g' ./snapshots/stable_memory.bin
74+
```
75+
76+
Upload the fixed snapshot.
77+
```bash
78+
dfx canister snapshot upload --dir ./snapshots quotes
79+
```
80+
81+
This will return a new snapshot id, similar to
82+
```bash
83+
0000000000000001ffffffffff9000010101
84+
```
85+
86+
## Step 5: Load the snapshot and verify
87+
88+
```bash
89+
dfx canister snapshot load quotes 0000000000000001ffffffffff9000010101
90+
dfx canister start quotes
91+
dfx canister call quotes "print"
92+
```
93+
94+
Output:
95+
```bash
96+
Colorless green ideas sleep furiously.
97+
```
98+
99+
Clean up.
100+
```bash
101+
rm -rf ./snapshots
102+
dfx stop
103+
```
104+
105+
## Conclusion
106+
107+
The ability to download and upload snapshots enables various new use cases:
108+
- Keeping canister backups on disk rather than on-chain.
109+
- Cloning canister state into another canister.
110+
- Migrating canister state to a different subnet.
111+
- Fixing faulty state or performing data migrations that would be prohibitive on-chain.

rust/canister-snapshot-download/dfx.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"canisters": {
3-
"wotd": {
4-
"candid": "src/wotd/wotd.did",
5-
"package": "wotd",
3+
"quotes": {
4+
"candid": "src/quotes/quotes.did",
5+
"package": "quotes",
66
"type": "rust"
77
}
88
},

rust/canister-snapshot-download/run.sh

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,22 @@ dfx stop
88
dfx start --clean --background
99
dfx deploy
1010

11-
dfx canister call uxrrr-q7777-77774-qaaaq-cai "setup"
12-
dfx canister call uxrrr-q7777-77774-qaaaq-cai "print"
11+
dfx canister call quotes "setup"
12+
dfx canister call quotes "print"
1313

14-
dfx canister stop uxrrr-q7777-77774-qaaaq-cai
15-
dfx canister snapshot create uxrrr-q7777-77774-qaaaq-cai
14+
dfx canister stop quotes
15+
dfx canister snapshot create quotes
1616
# snapshot id 0000000000000000ffffffffff9000010101
1717

18-
dfx canister snapshot download --dir ./snapshots uxrrr-q7777-77774-qaaaq-cai 0000000000000000ffffffffff9000010101
18+
dfx canister snapshot download --dir ./snapshots quotes 0000000000000000ffffffffff9000010101
1919

2020
# manipulate file
2121
sed -i -e 's/Colour/Color/g' ./snapshots/stable_memory.bin
2222

23-
dfx canister snapshot upload --dir ./snapshots uxrrr-q7777-77774-qaaaq-cai
23+
dfx canister snapshot upload --dir ./snapshots quotes
2424

25-
dfx canister snapshot load uxrrr-q7777-77774-qaaaq-cai 0000000000000001ffffffffff9000010101
26-
dfx canister start uxrrr-q7777-77774-qaaaq-cai
27-
dfx canister call uxrrr-q7777-77774-qaaaq-cai "print"
25+
dfx canister snapshot load quotes 0000000000000001ffffffffff9000010101
26+
dfx canister start quotes
27+
dfx canister call quotes "print"
2828

2929

rust/canister-snapshot-download/src/wotd/Cargo.toml renamed to rust/canister-snapshot-download/src/quotes/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
edition = "2021"
3-
name = "wotd"
3+
name = "quotes"
44
version = "0.1.0"
55

66
[lib]

0 commit comments

Comments
 (0)