Savior Faire

Memos - Moving attachements from database to local

I want to move the attachements stored in memos database back to local storage. Here are the steps:

Install mysql on local machine:

pip install mysql-connector-python

Configure the MariaDB host, so that it can be accessed from local machine:

# /mnt/user/appdata/mariadb/config/mysqld.cnf

[mysqld]
bind-address = 0.0.0.0

Then use this code:

import mysql.connector
from mysql.connector import Error
import os
from datetime import datetime

Database connection parameters

config = {
    'host': '192.168.1.100',
    'database': 'memosdb',
    'user': 'user',
    'password': 'password'
}

def export_blobs_to_files():
    try:
        # Establish a connection to the database
        connection = mysql.connector.connect(**config)
        cursor = connection.cursor()

        # Query to fetch blob data
        query = """
            SELECT `id`, `filename`, `blob`, `created_ts`
            FROM `resource`
            WHERE `blob` IS NOT NULL
        """
        cursor.execute(query)
        records = cursor.fetchall()

        for record in records:
            id, filename, blob, created_ts = record
            print(f"Processing record ID: {id}")

            # If created_ts is already a datetime object, no need to convert it
            if isinstance(created_ts, datetime):
                created_date = created_ts
            else:
                # If it's a string, convert it to datetime
                created_date = datetime.strptime(created_ts, '%Y-%m-%d %H:%M:%S')

            # Extract year and month from created timestamp
            year = created_date.year
            month = created_date.month

            # Create directory structure based on created timestamp
            dir_path = f"{year}/{month:02d}"
            os.makedirs(dir_path, exist_ok=True)

            # Construct the full file path
            file_path = f"{dir_path}/{int(created_date.timestamp())}_{filename}"

            # Write blob content to file
            with open(file_path, 'wb') as file:
                file.write(blob)

            print(f"Blob exported to: {file_path}")

    except Error as e:
        print(f"Error occurred during blob export: {e}")
    except Exception as e:
        print(f"Unexpected error during blob export: {e}")
    finally:
        if 'connection' in locals():
            connection.close()

def update_database_references():
    try:
        # Establish a connection to the database
        connection = mysql.connector.connect(**config)
        cursor = connection.cursor()

        # Query to fetch records with blob data
        query = """
            SELECT `id`, `filename`, `created_ts`
            FROM `resource`
            WHERE `blob` IS NOT NULL
        """
        cursor.execute(query)
        records = cursor.fetchall()

        for record in records:
            id, filename, created_ts = record
            print(f"Processing record ID: {id}")

            # If created_ts is already a datetime object, no need to convert it
            if isinstance(created_ts, datetime):
                created_date = created_ts
            else:
                # If it's a string, convert it to datetime
                created_date = datetime.strptime(created_ts, '%Y-%m-%d %H:%M:%S')

            # Extract year and month from created timestamp
            year = created_date.year
            month = created_date.month

            # Construct the file path
            file_path = f"assets/{year}/{month:02d}/{int(created_date.timestamp())}_{filename}"

            # Update the database to reference the local file
            update_query = """
                UPDATE `resource`
                SET `storage_type` = 'LOCAL', `reference` = %s, `blob` = NULL
                WHERE `id` = %s
            """
            cursor.execute(update_query, (file_path, id))
            connection.commit()

            print(f"Updated record ID: {id} with file path: {file_path}")

    except Error as e:
        print(f"Error occurred during database update: {e}")
    except Exception as e:
        print(f"Unexpected error during database update: {e}")
    finally:
        if 'connection' in locals():
            connection.close()

if __name__ == "__main__":
    export_blobs_to_files()
    update_database_references()

Then move the folders to /mnt/user/appdata/memos/assets. And it should be done.

#Tutorials