Need help transfering Anki data from old computer

Hello,

I’m trying to move my Anki data from the HD of a dead (Linux) laptop to my new one. I have the HD mounted as a USB drive, and I’ve located the intact Anki data folder set there.

First, I’ve tried importing the latest .colpkg file from the backups subfolder. This is the tail end of the error message I get when I try that:

File "/media/_______/0389d154-4808-4711-b3f0-df052b36e6f6/usr/share/anki/aqt/importing.py", line 419, in _replaceWithApkg
    json.loads(z.read("media").decode("utf8")).items()):
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
<class 'UnicodeDecodeError'>: 'utf-8' codec can't decode byte 0xb5 in position 1: invalid start byte

Other people have asked about the same error here and gotten the answer that it’s because the file isn’t UTF-8 - but that answer seems beside the point since .colpkg files aren’t text in the first place and the import dialog has a dropdown to select between text/CSV files and Packaged Anki Decks.

The Anki version on the old HD is 2.1.15 according to the init.py file there, same version as on my new laptop. It’s apparently an old version but I run Ubuntu Linux and that’s the version it comes with. I got the Anki on the old HD to run by pointing its executable script at the old installation on the HD in the hopes that it can open the file since it’s what created it, but I get the same error.

Second, besides the backup folder with its .colpkg files there is the large collection.anki2 file.

“.anki2 files are not directly importable - please import the .akpg or .zip file you have received instead.”

There are no .akpg or zip files.

What would you suggest I try? Can I convert.colpkg or .anki2 files to something, or unpack them into text? Appreciate any advice.

Did you try to replace the Anki2 folder in your new HD with the Anki2 folder from your old HD? Just remember to make a backup of the folder before replacing it, in case you need the stuff inside it.

Just an idea but is the old version still compatible with AnkiWeb? In that case maybe syncing could be an easy solution.

Sorry, I forgot to mention that I did try swapping the old data folder for the one in my new installation, and it also resulted in an error.

Can the Ankiweb sync be used with files that are not in (ie. already imported to) Anki? The manual talks about my “collection”, but these files aren’t in a collection of course. I have not used the sync before.

It’s a zip file with other files inside it (you can rename the file extension to .zip and look).

FYI, the backups don’t really have the media files in them so preferably you need to get the whole Ankk2 folder, as Anon said.

Nope, you can only sync if you are able to import it. (Maybe you can try importing in other devices and then use sync? You can even try importing on your phone).

Was it the same error as above?

Yes, you can unpack the contents of your fields. You can access everything in your collection.anki2 database except for media which is stored separatly. If you wish to access the contents of your fields so that you can start from scratch (meaning you’re okay with loosing your review history, template styling, settings, ect.) then you can use the following python script.

Save it as main.py and run it with python3 main.py >> my_anki_notes.txt:

import sqlite3

# This assumes: script working directory and collection directory are identical
path_to_database = "collection.anki2"

connection = sqlite3.connect(path_to_database)
cursor = connection.cursor()

try:
    cursor.execute("SELECT ntid, ord, name FROM fields")
    records = cursor.fetchall()

    # create a dictionary with ord -> name pairs
    # Example value of ntid_map:
    # {1722571790755: [(0, 'ID'), (1, 'Text (Front)'), (2, 'Text (Back)')]
    ntid_map = {}
    for ntid, ord, name in records:
        if ntid not in ntid_map:
            ntid_map[ntid] = []
        ntid_map[ntid].append((ord, name))

    cursor.execute("SELECT flds, mid FROM notes")
    records = cursor.fetchall()

    for record in records:
        flds_value = record[0]
        mid_value = record[1]
        if not flds_value:
            print("Field is empty")

        fields = flds_value.split("\x1f")

        for idx, field in enumerate(fields):
            if mid_value in ntid_map:
                for ord, name in ntid_map[mid_value]:
                    if idx == ord:
                        print(f"{name}: {field}")

        print("\n")

except sqlite3.Error as error:
    print(f"Error: {error}")

connection.close()

I tested it on my own collection and it works fine. It also doesn’t need any anki specific code / hooks, as it’s accessing the database directly.

It should be save to use (meaning it should not be able to modify your database file) but test the script on a copy of your database regardless; I’m a novice programmer after all.

I think you are making this more complicated than it needs to be.

The most common ways to start using Anki on a new device are –

  1. Copy your Anki2 folder from the old device to the new device. If you have to pick and choose parts of the folder (for some reason) – the essentials are your (main) profile folder, and the collection.anki2 database file and collection.media subfolder it contains. Make sure the Anki install on your new device is pointing at the right location for your Anki2 data folder.
  2. On your old device, sync your collection with AnkiWeb [and make sure it is fully synced, including all of your media]. If you are syncing for the first time, choose upload to AnkiWeb. On your new device, setup syncing and download from AnkiWeb.

No, you can only sync an AnkiWeb account with what is within a single profile – your collection database and your media.

Thank you for the input, everyone! I think this may have been an issue with the Anki Ubuntu package. I was unable to get anything imported until I eventually tried removing it (version 2.1.15+dfsg-3ubuntu4.1, for posterity) and installing instead the official desktop version from the Ankiweb site. It was able to read the old data with no problems.

That version released almost 6 years ago. You shouldn’t use it. Linux Distro Packages - Anki Manual

1 Like