Looking to create an API to automate backup data

josdmiller
Visitor
1 0 0

Hey! I'm looking to create an automated daily scheduled backup of my two Shopify stores. I'm looking to download using CSV files to back up store information.

  • products
  • customers
  • orders
  • gift card codes
  • discount codes
  • financial data
  • online theme

I would like this to automatically go into my Google Drive/Workspace account. Is this possible?

Replies 3 (3)

Cedcommerce
Shopify Expert
718 76 110

Hello @josdmiller

 

Yes, creating an automated daily scheduled backup of your Shopify stores and saving the backup files in your Google Drive or Workspace account is possible. 

 

To accomplish this, you can follow these steps:

 

Exporting Data from Shopify:

 

Shopify provides a built-in export feature that allows you to export various data types as CSV files. 

 

You can manually export the required data types (products, customers, orders, etc.) from your Shopify admin dashboard by selecting the respective sections and selecting the "Export" option. 

 

Alternatively, you can use the Shopify API to automate the export process. The API provides more flexibility and allows you to retrieve the data programmatically.

 

Automation:

 

You can write a script or use a third-party tool that interacts with the Shopify API to automate the backup process. The script/tool should be scheduled to run daily at a specific time.

 

The script/tool will connect to your Shopify stores using API credentials, retrieve the data types you want to back up and save them as CSV files locally.


Google Drive or Workspace Integration:

 

You can use the Google Drive API to upload the CSV files to your Google Drive/Workspace account. This requires setting up API credentials and writing code for file upload.

 

Various third-party services and tools are available to help you automate the backup process and handle the integration with Google Drive/Workspace. 

 

Some examples include Zapier, etc. These services often have pre-built integrations and workflows that allow you to easily connect your Shopify stores and Google Drive/Workspace accounts.


Setting up the Backup Schedule:

 

Depending on your chosen approach, you must set a daily schedule for running the backup script/tool or configuring the automation workflow. This can be done within the script itself (using a scheduler library like cron), or through the third-party service you're using (Zapier, Integromat, IFTTT, etc.).

 

By following these steps, you can automate the backup process for your Shopify stores.

Let us know if you need more help. 

 

Regards, 

CedCommerce

 

CedCommerce || Shopify Expert
- Let us know if our reply is helpful for you. Like it.
- Was your question answered? Mark it as an accepted solution.
- For further discussion contact: Email ID- apps@cedcommerce.com
- Whatsapp:- Join Here

StefanBoettjer
Shopify Partner
13 1 4

Hi, yes, that is possible through the Shopify API. In your Shopify account, go to APPs and create a DEVELOPER APP, granting it access to everything. Install Python on your system, including the modules to be imported in the code above. Here is some code that backs up the data, including the current live theme. The code creates a new and separate directory for each backup. Please note that the Shopify API can be sensitive if too many requests are sent, so I've added a 1-second delay between each request. The code is a quick guide, and you may need to customize it to fit your needs. I hope this helps you. Best regards, Stefan.

 

import requests
import json
import os
from datetime import datetime
import time  # 

# Shopify Store Informationen
API_KEY = 'YOUR API KEY'
PASSWORD = 'YOUR API PASSWORD'
SHOP_NAME = 'YOUR SHOPNAME (e.g. if your internal shopname is example.myshopify.com just enter example )'

# Create Backup Folder
backup_dir = datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
os.makedirs(backup_dir, exist_ok=True)

def save_data_to_file(data, filename):
    with open(os.path.join(backup_dir, filename), 'w') as f:
        json.dump(data, f, indent=4)

# Download
def download_and_save_file(url, filepath):
    response = requests.get(url, auth=(API_KEY, PASSWORD))
    if response.status_code == 200:
        # Create subfolders
        os.makedirs(os.path.dirname(filepath), exist_ok=True)
        
        with open(filepath, 'wb') as f:
            f.write(response.content)
        print(f'File {filepath} saved successfully.')
    else:
        print(f'Error downloading file from {url}: {response.status_code} - {response.text}')

# Shopify API Endpoints
endpoints = [
    'customers.json',
    'orders.json',
    'products.json',
    'pages.json',
    'custom_collections.json',
    'blogs.json',
    'themes.json',
    'smart_collections.json',
    'custom_collections.json',
]

for endpoint in endpoints:
    url = f'https://{SHOP_NAME}.myshopify.com/admin/api/2022-01/{endpoint}'
    response = requests.get(url, auth=(API_KEY, PASSWORD))

    if response.status_code == 200:
        data = response.json()
        filename = endpoint.replace('.json', '.json')
        save_data_to_file(data, filename)
        print(f'Data from {endpoint} saved successfully in {backup_dir}.')
    else:
        print(f'Error fetching data from {endpoint}: {response.status_code} - {response.text}')

# Download LIVE-Theme
themes_endpoint = f'https://{SHOP_NAME}.myshopify.com/admin/api/2023-10/themes.json'
themes_response = requests.get(themes_endpoint, auth=(API_KEY, PASSWORD))

if themes_response.status_code == 200:
    themes_data = themes_response.json()
    live_theme_id = None

    for theme in themes_data['themes']:
        if theme['role'] == 'main':  
            live_theme_id = theme['id']
            break

    if live_theme_id is not None:
        theme_dir = os.path.join(backup_dir, f'theme_{live_theme_id}')
        os.makedirs(theme_dir, exist_ok=True)

       
        theme_files_endpoint = f'https://{SHOP_NAME}.myshopify.com/admin/api/2023-10/themes/{live_theme_id}/assets.json'
        theme_files_response = requests.get(theme_files_endpoint, auth=(API_KEY, PASSWORD))

        if theme_files_response.status_code == 200:
            theme_files_data = theme_files_response.json()
            for asset in theme_files_data['assets']:
                asset_key = asset['key']
                asset_path = os.path.join(theme_dir, asset_key)
                asset_url = f'https://{SHOP_NAME}.myshopify.com/admin/api/2023-10/themes/{live_theme_id}/assets.json?asset[key]={asset_key}'
                download_and_save_file(asset_url, asset_path)
                print(f'File {asset_key} from Live Theme saved successfully in {backup_dir}.')
                time.sleep(1)  
        else:
            print(f'Error fetching files for Live Theme: {theme_files_response.status_code} - {theme_files_response.text}')
    else:
        print('No Live Theme found.')
else:
    print(f'Error fetching themes data: {themes_response.status_code} - {themes_response.text}')

print(f'Backup completed successfully in {backup_dir}.')

StefanBoettjer
Shopify Partner
13 1 4

You can do it by using the Shopify API. 1. Create an DEVELOPER APP with full access in your shopifyx store. 2. Install Python at your system and all packes you will find below. 3. You can use my example code dor backup. Feel free to change the code to your individual needs. Every time you will run the code a subfolder will be created.

 

import requests
import json
import os
from datetime import datetime
import time  # 

# Shopify Store Informationen
API_KEY = 'YOUR API KEY'
PASSWORD = 'YOUR API PASSWORD'
SHOP_NAME = 'YOUR SHOPNAME (e.g. if your internal shopname is example.myshopify.com just enter example )'

# Create Backup Folder
backup_dir = datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
os.makedirs(backup_dir, exist_ok=True)

def save_data_to_file(data, filename):
    with open(os.path.join(backup_dir, filename), 'w') as f:
        json.dump(data, f, indent=4)

# Download
def download_and_save_file(url, filepath):
    response = requests.get(url, auth=(API_KEY, PASSWORD))
    if response.status_code == 200:
        # Create subfolders
        os.makedirs(os.path.dirname(filepath), exist_ok=True)
        
        with open(filepath, 'wb') as f:
            f.write(response.content)
        print(f'File {filepath} saved successfully.')
    else:
        print(f'Error downloading file from {url}: {response.status_code} - {response.text}')

# Shopify API Endpoints
endpoints = [
    'customers.json',
    'orders.json',
    'products.json',
    'pages.json',
    'custom_collections.json',
    'blogs.json',
    'themes.json',
    'smart_collections.json',
    'custom_collections.json',
]

for endpoint in endpoints:
    url = f'https://{SHOP_NAME}.myshopify.com/admin/api/2022-01/{endpoint}'
    response = requests.get(url, auth=(API_KEY, PASSWORD))

    if response.status_code == 200:
        data = response.json()
        filename = endpoint.replace('.json', '.json')
        save_data_to_file(data, filename)
        print(f'Data from {endpoint} saved successfully in {backup_dir}.')
    else:
        print(f'Error fetching data from {endpoint}: {response.status_code} - {response.text}')

# Download LIVE-Theme
themes_endpoint = f'https://{SHOP_NAME}.myshopify.com/admin/api/2023-10/themes.json'
themes_response = requests.get(themes_endpoint, auth=(API_KEY, PASSWORD))

if themes_response.status_code == 200:
    themes_data = themes_response.json()
    live_theme_id = None

    for theme in themes_data['themes']:
        if theme['role'] == 'main':  
            live_theme_id = theme['id']
            break

    if live_theme_id is not None:
        theme_dir = os.path.join(backup_dir, f'theme_{live_theme_id}')
        os.makedirs(theme_dir, exist_ok=True)

       
        theme_files_endpoint = f'https://{SHOP_NAME}.myshopify.com/admin/api/2023-10/themes/{live_theme_id}/assets.json'
        theme_files_response = requests.get(theme_files_endpoint, auth=(API_KEY, PASSWORD))

        if theme_files_response.status_code == 200:
            theme_files_data = theme_files_response.json()
            for asset in theme_files_data['assets']:
                asset_key = asset['key']
                asset_path = os.path.join(theme_dir, asset_key)
                asset_url = f'https://{SHOP_NAME}.myshopify.com/admin/api/2023-10/themes/{live_theme_id}/assets.json?asset[key]={asset_key}'
                download_and_save_file(asset_url, asset_path)
                print(f'File {asset_key} from Live Theme saved successfully in {backup_dir}.')
                time.sleep(1)  
        else:
            print(f'Error fetching files for Live Theme: {theme_files_response.status_code} - {theme_files_response.text}')
    else:
        print('No Live Theme found.')
else:
    print(f'Error fetching themes data: {themes_response.status_code} - {themes_response.text}')

print(f'Backup completed successfully in {backup_dir}.')

 

Hope that helps, Best, Stefan 🙂