The Palos Publishing Company

Follow Us On The X Platform @PalosPublishing
Categories We Write About

Backup browser bookmarks with Python

Backing up browser bookmarks using Python is a practical task that can be automated for major browsers like Google Chrome, Mozilla Firefox, and Microsoft Edge. Each browser stores bookmarks in specific file formats and directories, which a Python script can read and copy to a backup location. Below is a detailed guide on how to back up bookmarks for these browsers using Python.


Backup Browser Bookmarks with Python

Prerequisites

Install necessary Python libraries:

bash
pip install shutil json pathlib

Also, ensure you run the script with permissions to access user data directories.


Understanding Bookmark File Locations

Google Chrome / Microsoft Edge (Chromium-based)

Bookmarks are stored in a JSON file:

  • Windows: C:Users<Username>AppDataLocalGoogleChromeUser DataDefaultBookmarks

  • macOS: ~/Library/Application Support/Google/Chrome/Default/Bookmarks

  • Linux: ~/.config/google-chrome/Default/Bookmarks

For Microsoft Edge, the path is similar but replace GoogleChrome with MicrosoftEdge.

Mozilla Firefox

Bookmarks are stored in a places.sqlite database file:

  • Windows: C:Users<Username>AppDataRoamingMozillaFirefoxProfiles<profile>places.sqlite

  • macOS: ~/Library/Application Support/Firefox/Profiles/<profile>/places.sqlite

  • Linux: ~/.mozilla/firefox/<profile>/places.sqlite

The <profile> is a randomly named folder ending with .default-release or similar.


Python Script for Backing Up Bookmarks

python
import os import shutil import platform from pathlib import Path from datetime import datetime def get_chrome_bookmark_path(): home = Path.home() system = platform.system() if system == 'Windows': return home / "AppData/Local/Google/Chrome/User Data/Default/Bookmarks" elif system == 'Darwin': return home / "Library/Application Support/Google/Chrome/Default/Bookmarks" elif system == 'Linux': return home / ".config/google-chrome/Default/Bookmarks" return None def get_edge_bookmark_path(): home = Path.home() system = platform.system() if system == 'Windows': return home / "AppData/Local/Microsoft/Edge/User Data/Default/Bookmarks" elif system == 'Darwin': return home / "Library/Application Support/Microsoft Edge/Default/Bookmarks" elif system == 'Linux': return home / ".config/microsoft-edge/Default/Bookmarks" return None def get_firefox_bookmark_path(): home = Path.home() system = platform.system() if system == 'Windows': base_path = home / "AppData/Roaming/Mozilla/Firefox/Profiles" elif system == 'Darwin': base_path = home / "Library/Application Support/Firefox/Profiles" elif system == 'Linux': base_path = home / ".mozilla/firefox" else: return None if not base_path.exists(): return None for profile in base_path.iterdir(): if profile.is_dir() and profile.name.endswith('.default-release'): return profile / "places.sqlite" return None def backup_file(file_path: Path, browser_name: str, backup_dir: Path): if not file_path.exists(): print(f"{browser_name} bookmarks not found.") return timestamp = datetime.now().strftime('%Y%m%d_%H%M%S') backup_file_name = f"{browser_name}_bookmarks_backup_{timestamp}{file_path.suffix}" backup_path = backup_dir / backup_file_name try: shutil.copy2(file_path, backup_path) print(f"{browser_name} bookmarks backed up to {backup_path}") except Exception as e: print(f"Failed to backup {browser_name} bookmarks: {e}") def main(): backup_dir = Path.home() / "BrowserBookmarksBackup" backup_dir.mkdir(exist_ok=True) chrome_path = get_chrome_bookmark_path() edge_path = get_edge_bookmark_path() firefox_path = get_firefox_bookmark_path() if chrome_path: backup_file(chrome_path, "chrome", backup_dir) if edge_path: backup_file(edge_path, "edge", backup_dir) if firefox_path: backup_file(firefox_path, "firefox", backup_dir) if __name__ == "__main__": main()

Features of the Script

  • Cross-Platform Support: Automatically detects Windows, macOS, or Linux.

  • Timestamped Backups: Each backup file includes a timestamp to prevent overwriting.

  • Backup Directory: All backups are stored in ~/BrowserBookmarksBackup.


Optional: Automating Backups with Task Scheduler or Cron

Windows Task Scheduler

  1. Save the script as backup_bookmarks.py.

  2. Create a .bat file to run it using Python.

  3. Schedule the .bat file using Task Scheduler.

macOS / Linux Cron Job

Add a cron entry:

bash
0 12 * * * /usr/bin/python3 /path/to/backup_bookmarks.py

This runs the script daily at noon.


Conclusion

Automating the backup of browser bookmarks ensures your important links are safe and easily restorable. This Python script provides a robust foundation for backing up bookmarks across Chrome, Firefox, and Edge browsers. You can extend it to upload backups to the cloud, compress files, or integrate with version control for historical tracking.

Share this Page your favorite way: Click any app below to share.

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Categories We Write About