-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshopify-restore.mjs
More file actions
1131 lines (988 loc) · 32.5 KB
/
Copy pathshopify-restore.mjs
File metadata and controls
1131 lines (988 loc) · 32.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env node
/**
* Shopify Store Restore Script
*
* Restores Shopify store data from a backup JSON file created by shopify-backup.mjs.
* Supports selective restore by resource type.
*
* Usage:
* node shopify-restore.mjs --file /path/to/backup.json [--resources products,inventory,collections,...]
*
* Environment variables:
* SHOPIFY_STORE – mystore.myshopify.com (without https://)
* SHOPIFY_ACCESS_TOKEN – Admin API access token (legacy admin-managed custom
* apps). Optional if CLIENT_ID/SECRET are set.
* SHOPIFY_CLIENT_ID – Dev Dashboard app Client ID (Klant-ID).
* SHOPIFY_CLIENT_SECRET – Dev Dashboard app Client Secret (Geheim).
* SHOPIFY_API_VERSION – API version (default: 2025-01)
* SHOPIFY_MIN_REQUEST_INTERVAL_MS – Min delay between API requests in ms
* (default: 560). Raise to ~N × 560 when running N
* jobs concurrently against the same token.
*
* Arguments:
* --file <path> – Path to backup JSON file (required)
* --resources <list> – Comma-separated list of resources to restore (default: all restorable)
* --dry-run – Preview what would be restored without making changes
* --skip-existing – Skip resources that already exist (don't update)
*
* Restorable resources:
* products, product_metafields, variant_metafields, inventory,
* custom_collections, smart_collections, collection_metafields,
* customers, customer_metafields, pages, page_metafields,
* blogs, articles, redirects, theme_assets,
* metafield_definitions, metaobject_definitions, metaobjects, shop_metafields,
* price_rules, script_tags
*
* NOT restorable via API:
* orders (read-only historical data), draft_orders (complex state),
* shop settings (mostly read-only), policies (managed in admin),
* shipping_zones (complex nested config), gift_cards (security restrictions)
*
* @version 1.0.0
* @license GPL-3.0-or-later
*/
import { readFileSync } from 'node:fs';
// ---------------------------------------------------------------------------
// Argument parsing
// ---------------------------------------------------------------------------
const args = process.argv.slice(2);
const flags = {};
for (let i = 0; i < args.length; i++) {
if (args[i] === '--file' && args[i + 1]) {
flags.file = args[++i];
} else if (args[i] === '--resources' && args[i + 1]) {
flags.resources = args[++i].split(',').map((s) => s.trim());
} else if (args[i] === '--dry-run') {
flags.dryRun = true;
} else if (args[i] === '--skip-existing') {
flags.skipExisting = true;
}
}
if (!flags.file) {
console.error('ERROR: --file argument is required.');
console.error(
'Usage: node shopify-restore.mjs --file /path/to/backup.json [--resources products,inventory] [--dry-run]'
);
process.exit(1);
}
// ---------------------------------------------------------------------------
// Configuration
// ---------------------------------------------------------------------------
const STORE = process.env.SHOPIFY_STORE;
let TOKEN = process.env.SHOPIFY_ACCESS_TOKEN;
const CLIENT_ID = process.env.SHOPIFY_CLIENT_ID;
const CLIENT_SECRET = process.env.SHOPIFY_CLIENT_SECRET;
const API_VERSION = process.env.SHOPIFY_API_VERSION || '2025-01';
if (!STORE || (!TOKEN && !(CLIENT_ID && CLIENT_SECRET))) {
console.error(
'ERROR: SHOPIFY_STORE is required, plus either SHOPIFY_ACCESS_TOKEN ' +
'(legacy admin-managed custom apps) or both SHOPIFY_CLIENT_ID and ' +
'SHOPIFY_CLIENT_SECRET (Dev Dashboard apps).'
);
process.exit(1);
}
const BASE_URL = `https://${STORE}/admin/api/${API_VERSION}`;
const GQL_URL = `https://${STORE}/admin/api/${API_VERSION}/graphql.json`;
const DRY_RUN = flags.dryRun ?? false;
const ALL_RESTORABLE = [
'products',
'product_metafields',
'variant_metafields',
'inventory',
'custom_collections',
'smart_collections',
'collection_metafields',
'customers',
'customer_metafields',
'pages',
'page_metafields',
'blogs',
'articles',
'redirects',
'theme_assets',
'metafield_definitions',
'metaobject_definitions',
'metaobjects',
'shop_metafields',
'price_rules',
'script_tags',
];
const resourcesToRestore = flags.resources || ALL_RESTORABLE;
// Validate resource names
for (const r of resourcesToRestore) {
if (!ALL_RESTORABLE.includes(r)) {
console.error(`ERROR: Unknown resource "${r}".`);
console.error(`Valid resources: ${ALL_RESTORABLE.join(', ')}`);
process.exit(1);
}
}
// ---------------------------------------------------------------------------
// Rate limiting & HTTP helpers (same as backup script)
// ---------------------------------------------------------------------------
// Per store/access token, shared across all processes. Override via
// SHOPIFY_MIN_REQUEST_INTERVAL_MS when running alongside other backup/restore
// jobs on the same token (use ~N × 560 ms for N concurrent jobs).
const MIN_REQUEST_INTERVAL_MS = Math.max(
0,
parseInt(process.env.SHOPIFY_MIN_REQUEST_INTERVAL_MS || '560', 10) || 560
);
let lastRequestTime = 0;
async function throttle() {
const now = Date.now();
const elapsed = now - lastRequestTime;
if (elapsed < MIN_REQUEST_INTERVAL_MS) {
await new Promise((r) => setTimeout(r, MIN_REQUEST_INTERVAL_MS - elapsed));
}
lastRequestTime = Date.now();
}
const FETCH_RETRIES = 5;
async function shopifyFetch(url, options = {}, retries = FETCH_RETRIES) {
await throttle();
let response;
try {
response = await fetch(url, {
...options,
headers: {
'X-Shopify-Access-Token': TOKEN,
'Content-Type': 'application/json',
Accept: 'application/json',
...options.headers,
},
});
} catch (err) {
if (retries > 0) {
const wait = 2000 * 2 ** (FETCH_RETRIES - retries);
log(` ⏳ Network error (${err.message}), retrying in ${wait}ms...`);
await new Promise((r) => setTimeout(r, wait));
return shopifyFetch(url, options, retries - 1);
}
throw new Error(`Network failure after retries: ${err.message} (${url})`);
}
if (response.status === 429 || response.status >= 500) {
if (retries > 0) {
const retryAfter =
Math.max(1000, (parseFloat(response.headers.get('Retry-After')) || 4) * 1000) +
Math.floor(Math.random() * 1000);
log(
` ⏳ Rate limited (${response.status}), retrying in ${retryAfter}ms...`
);
await new Promise((r) => setTimeout(r, retryAfter));
return shopifyFetch(url, options, retries - 1);
}
throw new Error(
`Shopify API error ${response.status} after retries: ${url}`
);
}
return response;
}
async function graphqlRequest(query, variables = {}) {
await throttle();
const response = await shopifyFetch(GQL_URL, {
method: 'POST',
body: JSON.stringify({ query, variables }),
});
const result = await response.json();
if (result.errors) {
throw new Error(`GraphQL errors: ${JSON.stringify(result.errors)}`);
}
return result.data;
}
function log(msg) {
process.stderr.write(`[${new Date().toISOString()}] ${msg}\n`);
}
// ---------------------------------------------------------------------------
// Restore statistics
// ---------------------------------------------------------------------------
const stats = {
created: 0,
updated: 0,
skipped: 0,
failed: 0,
errors: [],
};
function recordError(resource, id, error) {
stats.failed++;
const msg = `${resource} #${id}: ${error.message || error}`;
stats.errors.push(msg);
log(` ❌ ${msg}`);
}
// ---------------------------------------------------------------------------
// REST restore helpers
// ---------------------------------------------------------------------------
/**
* Create or update a REST resource.
*
* Attempts to GET the resource by ID first. If it exists, PUTs to update.
* If it doesn't exist, POSTs to create.
*
* @param {string} resourcePath – e.g. '/products'
* @param {string} singularKey – e.g. 'product'
* @param {object} item – The backup item data
* @param {string[]} [excludeFields] – Fields to strip before sending
* @returns {Promise<object|null>} – The created/updated resource or null
*/
async function upsertResource(resourcePath, singularKey, item, excludeFields = []) {
if (DRY_RUN) {
log(` [DRY RUN] Would upsert ${singularKey} #${item.id}`);
stats.skipped++;
return null;
}
// Clean the item: remove read-only or server-generated fields
const cleanItem = { ...item };
const alwaysExclude = [
'admin_graphql_api_id',
'created_at',
'updated_at',
'published_at',
];
for (const field of [...alwaysExclude, ...excludeFields]) {
delete cleanItem[field];
}
// Try to check if exists
try {
const checkResponse = await shopifyFetch(
`${BASE_URL}${resourcePath}/${item.id}.json`
);
if (checkResponse.ok) {
if (flags.skipExisting) {
stats.skipped++;
return null;
}
// Update existing
const updateResponse = await shopifyFetch(
`${BASE_URL}${resourcePath}/${item.id}.json`,
{
method: 'PUT',
body: JSON.stringify({ [singularKey]: cleanItem }),
}
);
if (updateResponse.ok) {
stats.updated++;
const data = await updateResponse.json();
return data[singularKey];
} else {
const errBody = await updateResponse.text();
throw new Error(`PUT ${updateResponse.status}: ${errBody}`);
}
}
} catch (err) {
// Resource doesn't exist or error checking — try to create
if (err.message && !err.message.includes('404')) {
// If it's not a 404, it's a real error during update
if (err.message.includes('PUT ')) {
recordError(singularKey, item.id, err);
return null;
}
}
}
// Create new
try {
// Remove the ID so Shopify generates a new one if needed
const createItem = { ...cleanItem };
// Keep the original ID for reference but Shopify may assign a new one
delete createItem.id;
const createResponse = await shopifyFetch(
`${BASE_URL}${resourcePath}.json`,
{
method: 'POST',
body: JSON.stringify({ [singularKey]: createItem }),
}
);
if (createResponse.ok) {
stats.created++;
const data = await createResponse.json();
return data[singularKey];
} else {
const errBody = await createResponse.text();
throw new Error(`POST ${createResponse.status}: ${errBody}`);
}
} catch (err) {
recordError(singularKey, item.id, err);
return null;
}
}
/**
* Restore metafields for a resource.
*
* @param {string} ownerPath – e.g. '/products/123'
* @param {object[]} metafields – Array of metafield objects
*/
async function restoreMetafields(ownerPath, metafields) {
for (const mf of metafields) {
if (DRY_RUN) {
log(` [DRY RUN] Would restore metafield ${mf.namespace}.${mf.key}`);
stats.skipped++;
continue;
}
try {
const response = await shopifyFetch(
`${BASE_URL}${ownerPath}/metafields.json`,
{
method: 'POST',
body: JSON.stringify({
metafield: {
namespace: mf.namespace,
key: mf.key,
value: mf.value,
type: mf.type,
},
}),
}
);
if (response.ok) {
stats.created++;
} else if (response.status === 422) {
// Likely already exists — try to update via GraphQL or skip
stats.skipped++;
} else {
const errBody = await response.text();
throw new Error(`${response.status}: ${errBody}`);
}
} catch (err) {
recordError('metafield', `${mf.namespace}.${mf.key}`, err);
}
}
}
// ---------------------------------------------------------------------------
// Resource restorers
// ---------------------------------------------------------------------------
async function restoreProducts(backup) {
const products = backup.products || [];
log(`🛍️ Restoring ${products.length} products...`);
for (let i = 0; i < products.length; i++) {
const product = products[i];
await upsertResource('/products', 'product', product, [
'variants', // Variants are nested and managed separately by Shopify
'image',
'options',
]);
if ((i + 1) % 25 === 0) {
log(` ...processed ${i + 1}/${products.length}`);
}
}
}
async function restoreProductMetafields(backup) {
const metafieldsMap = backup.product_metafields || {};
const productIds = Object.keys(metafieldsMap);
log(`🏷️ Restoring metafields for ${productIds.length} products...`);
for (const productId of productIds) {
await restoreMetafields(
`/products/${productId}`,
metafieldsMap[productId]
);
}
}
async function restoreVariantMetafields(backup) {
const metafieldsMap = backup.variant_metafields || {};
const variantIds = Object.keys(metafieldsMap);
log(`🏷️ Restoring metafields for ${variantIds.length} variants...`);
for (const variantId of variantIds) {
await restoreMetafields(
`/variants/${variantId}`,
metafieldsMap[variantId]
);
}
}
async function restoreInventory(backup) {
const levels = backup.inventory_levels || [];
log(`📦 Restoring ${levels.length} inventory levels...`);
for (let i = 0; i < levels.length; i++) {
const level = levels[i];
if (DRY_RUN) {
log(
` [DRY RUN] Would set inventory_item ${level.inventory_item_id} at location ${level.location_id} to ${level.available}`
);
stats.skipped++;
continue;
}
// Use the inventory_levels/set endpoint to set absolute quantities
try {
const response = await shopifyFetch(
`${BASE_URL}/inventory_levels/set.json`,
{
method: 'POST',
body: JSON.stringify({
location_id: level.location_id,
inventory_item_id: level.inventory_item_id,
available: level.available,
}),
}
);
if (response.ok) {
stats.updated++;
} else {
const errBody = await response.text();
throw new Error(`${response.status}: ${errBody}`);
}
} catch (err) {
recordError(
'inventory_level',
`item:${level.inventory_item_id}@loc:${level.location_id}`,
err
);
}
if ((i + 1) % 100 === 0) {
log(` ...processed ${i + 1}/${levels.length}`);
}
}
}
async function restoreCustomCollections(backup) {
const collections = backup.custom_collections || [];
log(`📂 Restoring ${collections.length} custom collections...`);
for (const col of collections) {
await upsertResource('/custom_collections', 'custom_collection', col);
}
}
async function restoreSmartCollections(backup) {
const collections = backup.smart_collections || [];
log(`📂 Restoring ${collections.length} smart collections...`);
for (const col of collections) {
await upsertResource('/smart_collections', 'smart_collection', col);
}
}
async function restoreCollectionMetafields(backup) {
const customMf = backup.custom_collection_metafields || {};
const smartMf = backup.smart_collection_metafields || {};
log(`🏷️ Restoring collection metafields...`);
for (const colId of Object.keys(customMf)) {
await restoreMetafields(`/collections/${colId}`, customMf[colId]);
}
for (const colId of Object.keys(smartMf)) {
await restoreMetafields(`/collections/${colId}`, smartMf[colId]);
}
}
async function restoreCustomers(backup) {
const customers = backup.customers || [];
log(`👤 Restoring ${customers.length} customers...`);
log(
' ⚠️ Note: Customer passwords cannot be restored. Customers will need to reset passwords.'
);
for (let i = 0; i < customers.length; i++) {
await upsertResource('/customers', 'customer', customers[i], [
'orders_count',
'total_spent',
'last_order_id',
'last_order_name',
'multipass_identifier',
]);
if ((i + 1) % 50 === 0) {
log(` ...processed ${i + 1}/${customers.length}`);
}
}
}
async function restoreCustomerMetafields(backup) {
const metafieldsMap = backup.customer_metafields || {};
const customerIds = Object.keys(metafieldsMap);
log(`🏷️ Restoring metafields for ${customerIds.length} customers...`);
for (const customerId of customerIds) {
await restoreMetafields(
`/customers/${customerId}`,
metafieldsMap[customerId]
);
}
}
async function restorePages(backup) {
const pages = backup.pages || [];
log(`📄 Restoring ${pages.length} pages...`);
for (const page of pages) {
await upsertResource('/pages', 'page', page);
}
}
async function restorePageMetafields(backup) {
const metafieldsMap = backup.page_metafields || {};
const pageIds = Object.keys(metafieldsMap);
log(`🏷️ Restoring metafields for ${pageIds.length} pages...`);
for (const pageId of pageIds) {
await restoreMetafields(`/pages/${pageId}`, metafieldsMap[pageId]);
}
}
async function restoreBlogs(backup) {
const blogs = backup.blogs || [];
log(`📰 Restoring ${blogs.length} blogs...`);
for (const blog of blogs) {
await upsertResource('/blogs', 'blog', blog);
}
}
async function restoreArticles(backup) {
const articlesMap = backup.articles || {};
log(`📰 Restoring articles...`);
for (const [blogId, articles] of Object.entries(articlesMap)) {
log(` Blog #${blogId}: ${articles.length} articles`);
for (const article of articles) {
await upsertResource(
`/blogs/${blogId}/articles`,
'article',
article,
['blog_id']
);
}
}
}
async function restoreRedirects(backup) {
const redirects = backup.redirects || [];
log(`↩️ Restoring ${redirects.length} redirects...`);
for (const redirect of redirects) {
await upsertResource('/redirects', 'redirect', redirect);
}
}
async function restoreThemeAssets(backup) {
const themesMap = backup.themes || {};
log(`🎨 Restoring theme assets...`);
// Find the published/main theme in the current store
const currentThemesResponse = await shopifyFetch(`${BASE_URL}/themes.json`);
const currentThemes = (await currentThemesResponse.json()).themes || [];
const mainTheme = currentThemes.find((t) => t.role === 'main');
if (!mainTheme) {
log(' ⚠️ No main theme found on target store. Skipping theme restore.');
return;
}
// Find the main theme in the backup
let backupMainTheme = null;
for (const [, themeData] of Object.entries(themesMap)) {
if (themeData.theme_info?.role === 'main') {
backupMainTheme = themeData;
break;
}
}
if (!backupMainTheme) {
log(' ⚠️ No main theme found in backup. Skipping theme restore.');
return;
}
const assets = backupMainTheme.assets || [];
log(
` 🎨 Restoring ${assets.length} assets to theme "${mainTheme.name}" (#${mainTheme.id})...`
);
for (let i = 0; i < assets.length; i++) {
const asset = assets[i];
if (!asset.key || asset._error) continue;
if (DRY_RUN) {
log(` [DRY RUN] Would restore asset ${asset.key}`);
stats.skipped++;
continue;
}
try {
const payload = { asset: { key: asset.key } };
// Theme assets can have either value (text) or attachment (base64 binary)
if (asset.value != null) {
payload.asset.value = asset.value;
} else if (asset.attachment != null) {
payload.asset.attachment = asset.attachment;
} else {
stats.skipped++;
continue;
}
const response = await shopifyFetch(
`${BASE_URL}/themes/${mainTheme.id}/assets.json`,
{ method: 'PUT', body: JSON.stringify(payload) }
);
if (response.ok) {
stats.updated++;
} else {
const errBody = await response.text();
throw new Error(`${response.status}: ${errBody}`);
}
} catch (err) {
recordError('theme_asset', asset.key, err);
}
if ((i + 1) % 50 === 0) {
log(` ...processed ${i + 1}/${assets.length}`);
}
}
}
async function restoreMetafieldDefinitions(backup) {
const definitionsMap = backup.metafield_definitions || {};
log(`📐 Restoring metafield definitions...`);
const mutation = `
mutation CreateMetafieldDefinition($definition: MetafieldDefinitionInput!) {
metafieldDefinitionCreate(definition: $definition) {
createdDefinition {
id
}
userErrors {
field
message
}
}
}
`;
for (const [ownerType, definitions] of Object.entries(definitionsMap)) {
for (const def of definitions) {
if (DRY_RUN) {
log(` [DRY RUN] Would create definition ${def.namespace}.${def.key} for ${ownerType}`);
stats.skipped++;
continue;
}
try {
const result = await graphqlRequest(mutation, {
definition: {
name: def.name,
namespace: def.namespace,
key: def.key,
type: def.type.name,
description: def.description || '',
ownerType,
validations: def.validations || [],
},
});
const errors =
result.metafieldDefinitionCreate?.userErrors || [];
if (errors.length > 0) {
if (errors.some((e) => e.message.includes('already exists'))) {
stats.skipped++;
} else {
throw new Error(errors.map((e) => e.message).join(', '));
}
} else {
stats.created++;
}
} catch (err) {
recordError('metafield_definition', `${def.namespace}.${def.key}`, err);
}
}
}
}
async function restoreMetaobjectDefinitions(backup) {
const definitions = backup.metaobject_definitions || [];
log(`📐 Restoring ${definitions.length} metaobject definitions...`);
const mutation = `
mutation CreateMetaobjectDefinition($definition: MetaobjectDefinitionCreateInput!) {
metaobjectDefinitionCreate(definition: $definition) {
metaobjectDefinition {
id
}
userErrors {
field
message
}
}
}
`;
for (const def of definitions) {
if (DRY_RUN) {
log(` [DRY RUN] Would create metaobject definition "${def.type}"`);
stats.skipped++;
continue;
}
try {
const fieldDefs = (def.fieldDefinitions || []).map((fd) => ({
name: fd.name,
key: fd.key,
description: fd.description || '',
required: fd.required,
type: fd.type.name,
validations: fd.validations || [],
}));
const result = await graphqlRequest(mutation, {
definition: {
name: def.name,
type: def.type,
description: def.description || '',
displayNameKey: def.displayNameKey,
access: def.access || {},
fieldDefinitions: fieldDefs,
},
});
const errors =
result.metaobjectDefinitionCreate?.userErrors || [];
if (errors.length > 0) {
if (errors.some((e) => e.message.includes('already exists') || e.message.includes('taken'))) {
stats.skipped++;
} else {
throw new Error(errors.map((e) => e.message).join(', '));
}
} else {
stats.created++;
}
} catch (err) {
recordError('metaobject_definition', def.type, err);
}
}
}
async function restoreMetaobjects(backup) {
const metaobjectsMap = backup.metaobjects || {};
log(`📦 Restoring metaobjects...`);
const mutation = `
mutation UpsertMetaobject($handle: MetaobjectHandleInput!, $metaobject: MetaobjectUpsertInput!) {
metaobjectUpsert(handle: $handle, metaobject: $metaobject) {
metaobject {
id
}
userErrors {
field
message
}
}
}
`;
for (const [type, objects] of Object.entries(metaobjectsMap)) {
log(` 📦 Type "${type}": ${objects.length} objects`);
for (const obj of objects) {
if (DRY_RUN) {
log(` [DRY RUN] Would upsert metaobject ${type}/${obj.handle}`);
stats.skipped++;
continue;
}
try {
const fields = (obj.fields || []).map((f) => ({
key: f.key,
value: f.value,
}));
const result = await graphqlRequest(mutation, {
handle: { type, handle: obj.handle },
metaobject: { fields },
});
const errors = result.metaobjectUpsert?.userErrors || [];
if (errors.length > 0) {
throw new Error(errors.map((e) => e.message).join(', '));
} else {
stats.created++;
}
} catch (err) {
recordError('metaobject', `${type}/${obj.handle}`, err);
}
}
}
}
async function restoreShopMetafields(backup) {
const metafields = backup.shop_metafields || [];
log(`🏷️ Restoring ${metafields.length} shop metafields...`);
const mutation = `
mutation SetShopMetafield($metafields: [MetafieldsSetInput!]!) {
metafieldsSet(metafields: $metafields) {
metafields {
id
}
userErrors {
field
message
}
}
}
`;
// Process in batches of 25 (Shopify limit for metafieldsSet)
for (let i = 0; i < metafields.length; i += 25) {
const batch = metafields.slice(i, i + 25);
if (DRY_RUN) {
log(` [DRY RUN] Would set ${batch.length} shop metafields`);
stats.skipped += batch.length;
continue;
}
try {
const input = batch.map((mf) => ({
namespace: mf.namespace,
key: mf.key,
value: mf.value,
type: mf.type,
ownerId: `gid://shopify/Shop`, // Shop-level
}));
const result = await graphqlRequest(mutation, { metafields: input });
const errors = result.metafieldsSet?.userErrors || [];
if (errors.length > 0) {
for (const err of errors) {
stats.failed++;
stats.errors.push(`shop_metafield: ${err.message}`);
}
} else {
stats.updated += batch.length;
}
} catch (err) {
recordError('shop_metafields', `batch ${i}`, err);
}
}
}
async function restorePriceRules(backup) {
const priceRules = backup.price_rules_and_discounts || [];
log(`💰 Restoring ${priceRules.length} price rules & discount codes...`);
for (const rule of priceRules) {
const discountCodes = rule.discount_codes || [];
const ruleClean = { ...rule };
delete ruleClean.discount_codes;
const created = await upsertResource('/price_rules', 'price_rule', ruleClean, [
'usage_count',
]);
if (created && discountCodes.length > 0) {
for (const code of discountCodes) {
await upsertResource(
`/price_rules/${created.id}/discount_codes`,
'discount_code',
code,
['usage_count']
);
}
}
}
}
async function restoreScriptTags(backup) {
const scriptTags = backup.script_tags || [];
log(`📜 Restoring ${scriptTags.length} script tags...`);
for (const tag of scriptTags) {
await upsertResource('/script_tags', 'script_tag', tag);
}
}
// ---------------------------------------------------------------------------
// Restore orchestration
// ---------------------------------------------------------------------------
const RESTORE_MAP = {
products: restoreProducts,
product_metafields: restoreProductMetafields,
variant_metafields: restoreVariantMetafields,
inventory: restoreInventory,
custom_collections: restoreCustomCollections,
smart_collections: restoreSmartCollections,
collection_metafields: restoreCollectionMetafields,
customers: restoreCustomers,
customer_metafields: restoreCustomerMetafields,
pages: restorePages,
page_metafields: restorePageMetafields,
blogs: restoreBlogs,
articles: restoreArticles,
redirects: restoreRedirects,
theme_assets: restoreThemeAssets,
metafield_definitions: restoreMetafieldDefinitions,
metaobject_definitions: restoreMetaobjectDefinitions,
metaobjects: restoreMetaobjects,
shop_metafields: restoreShopMetafields,
price_rules: restorePriceRules,
script_tags: restoreScriptTags,
};
async function runRestore() {
const startTime = Date.now();
// Load backup file
log(`📂 Loading backup from: ${flags.file}`);
const raw = readFileSync(flags.file, 'utf-8');
const backup = JSON.parse(raw);
log(` Backup created: ${backup._meta?.created_at}`);
log(` Backup store: ${backup._meta?.store}`);
log(` Target store: ${STORE}`);
log(` Resources to restore: ${resourcesToRestore.join(', ')}`);
log(` Dry run: ${DRY_RUN}`);
log('');
// Safety check: warn if restoring to a different store
if (backup._meta?.store && backup._meta.store !== STORE) {
log('⚠️ WARNING: Backup store does not match target store!');
log(
` Backup: ${backup._meta.store} → Target: ${STORE}`
);
log(' Resource IDs may not match. Restore may create duplicates.');
log('');
}
// Execute restore in dependency order
const orderedResources = [
// First: definitions (metafield + metaobject schemas must exist before values)