如何在本地电脑自动每日全量备份Shopify站点(不依赖插件)
Hey there! Great question about setting up a plugin-free, local automated daily backup for your Shopify store. The key here is leveraging Shopify's Admin API to pull your store data, then using your computer's built-in scheduling tools to run the backup script automatically every day. Let's walk through the process step by step.
Prerequisites First
Before diving in, you'll need to grab your Shopify API credentials:
- Go to your Shopify admin > Apps > Manage private apps (or create a custom app if you're on newer Shopify plans).
- Create a new private app with read access to all the data you want to back up (products, orders, customers, themes, pages, blogs, etc.).
- Note down your API key, Password, and your store's domain (e.g.,
your-store.myshopify.com).
Step 1: Write a Backup Script
You can use any scripting language (Python, Node.js, bash) to fetch data via the Shopify API. Below is a simple Python example that backs up core store data and saves it locally with timestamped folders.
Python Script Example
import os import json import requests from datetime import datetime # Shopify API credentials (replace with your own) SHOPIFY_DOMAIN = "your-store.myshopify.com" API_KEY = "your-api-key" PASSWORD = "your-api-password" API_VERSION = "2024-04" # Use the latest stable API version # Create a timestamped backup folder backup_dir = f"shopify_backup_{datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}" os.makedirs(backup_dir, exist_ok=True) # List of endpoints to back up (add/remove as needed) endpoints = [ "/products.json", "/orders.json", "/customers.json", "/themes.json", "/pages.json", "/blogs.json" ] def fetch_and_save(endpoint): url = f"https://{API_KEY}:{PASSWORD}@{SHOPIFY_DOMAIN}/admin/api/{API_VERSION}{endpoint}" all_data = [] # Handle pagination (Shopify returns 50 items per page by default) while url: response = requests.get(url) response.raise_for_status() # Raise error if API call fails data = response.json() # Extract the resource data (e.g., 'products' from /products.json) resource_key = endpoint.split("/")[1].split(".")[0] all_data.extend(data[resource_key]) # Check for next page url = response.links.get("next", {}).get("url") # Save data to JSON file with open(f"{backup_dir}/{resource_key}.json", "w") as f: json.dump(all_data, f, indent=2) print(f"Backed up {len(all_data)} {resource_key}") # Run backup for all endpoints for endpoint in endpoints: try: fetch_and_save(endpoint) except Exception as e: print(f"Failed to backup {endpoint}: {str(e)}") print(f"Backup completed successfully! Files saved to {backup_dir}")
Quick notes for the script:
- Replace the placeholder credentials with your actual details.
- Adjust the
endpointslist to include other data you want (e.g.,/collections.json,/discounts.json). - For theme file backups (not just metadata), add a step to download the theme's
.zipfile using the theme's API ID. - Add logging to a file if you want to track failures over time.
Step 2: Set Up Automated Daily Execution
Now use your OS's built-in scheduler to run the script daily.
For Windows (Task Scheduler)
- Open Task Scheduler > Create Basic Task.
- Name it something like "Shopify Daily Backup" and set a daily trigger (pick a time when your store is least active).
- For the action, select "Start a program" and browse to your Python executable (e.g.,
C:\Python311\python.exe). - In "Add arguments", enter the full path to your backup script (e.g.,
"C:\Scripts\shopify_backup.py"). - Save the task and test it manually first to confirm it works.
For Mac/Linux (Cron Job)
- Open terminal and run
crontab -eto edit your cron jobs. - Add a line to run the script daily at, say, 2 AM. Example:
0 2 * * * /usr/bin/python3 /home/your-user/scripts/shopify_backup.py >> /home/your-user/scripts/shopify_backup_logs.txt 2>&10 2 * * *= Runs daily at 2:00 AM.- Use
which python3to get your system's Python path if unsure. - The
>> ... 2>&1part logs output/errors for troubleshooting.
- Save and exit the crontab editor.
Key Considerations
- API Rate Limits: Shopify allows 2 requests per second for private apps. If you have a large store, add small delays between requests if needed.
- Storage Management: Add a cleanup step to delete backups older than 30 days (or your preferred period) to save local space.
- Security: Store API credentials in environment variables instead of hardcoding them if you share the script.
- Testing: Always run the script manually first to confirm it pulls all your data correctly before enabling automation.
内容的提问来源于stack exchange,提问作者Muhammad Hamza Nisar

