Backup and restore your library data
- In this example, you will:
Backup a local library to a JSON file
Restore tags for tracks from a JSON file backup
Backup a remote library to a JSON file
Restore remote playlists from a JSON file backup
Set up logging
Set up logging to ensure you can see all info reported by the later operations.
Libraries log info about loaded objects to the custom STAT
level.
import logging
import sys
from musify.logger import STAT
logging.basicConfig(format="%(message)s", level=STAT, stream=sys.stdout)
Backup and restore a local library
Load a local library. For more information on how to do this see Load a local library and objects
from musify.libraries.local.library import LocalLibrary library = LocalLibrary( library_folders=["<PATH TO YOUR LIBRARY FOLDER>", ...], playlist_folder="<PATH TO YOUR PLAYLIST FOLDER>", )
from musify.libraries.local.library import MusicBee library = MusicBee(musicbee_folder="<PATH TO YOUR MUSICBEE FOLDER>")
Backup your library to JSON:
import json path = "local_backup.json" with open(path, "w") as file: json.dump(library.json(), file, indent=2)
Restore the tags for all tracks in your library from a JSON file:
with open(path, "r") as file: backup = json.load(file) library.restore_tracks(backup["tracks"])
… or restore only a specific set of tags:
from musify.libraries.local.track.field import LocalTrackField tags = [ LocalTrackField.TITLE, LocalTrackField.GENRES, LocalTrackField.KEY, LocalTrackField.BPM, LocalTrackField.DATE, LocalTrackField.COMPILATION, LocalTrackField.IMAGES ] library.restore_tracks(backup, tags=tags)
Save the tags to the track files:
import asyncio results = asyncio.run(library.save_tracks(replace=True, dry_run=False)) # ... or if tags were specified results = asyncio.run(library.save_tracks(tags=tags, replace=True, dry_run=False)) library.log_save_tracks_result(results)
Backup and restore a remote library
Load a remote library. For more information on how to do this see any of the relevant guides for loading remote libraries.
Note
This step uses the
SpotifyLibrary
, but any supported music streaming service can be used in generally the same way. Just modify the imports and classes as required.from musify.libraries.remote.spotify.api import SpotifyAPI api = SpotifyAPI( client_id="<YOUR CLIENT ID>", client_secret="<YOUR CLIENT SECRET>", scope=[ "user-library-read", "user-follow-read", "playlist-read-collaborative", "playlist-read-private", "playlist-modify-public", "playlist-modify-private" ], # providing a `token_file_path` will save the generated token to your system # for quicker authorisations in future token_file_path="<PATH TO JSON TOKEN>" )
Backup your library to JSON:
import json path = "remote_backup.json" with open(path, "w") as file: json.dump(library.json(), file, indent=2)
Restore the playlists in your library from a JSON file and sync the playlists:
from p1 import * import asyncio from musify.libraries.remote.core.library import RemoteLibrary with open(path, "r") as file: backup = json.load(file) async def restore_remote_library(library: RemoteLibrary, backup: dict[str, Any]) -> None: """Restore the playlists in a remote ``library`` from the given ``backup``""" async with library: await library.restore_playlists(backup["playlists"]) results = await library.sync(kind="refresh", reload=False, dry_run=False) library.log_sync(results) asyncio.run(restore_remote_library(library, backup))