Obsidian duplication contenu

Comment éviter la duplication de contenu entre deux vaults Obsidian ?

Typiquement lorsqu'on a un vault perso et un vault "repo documentation" par projet on est amené à dupliquer du contenu. Avec tous les risques de dé-synchronisation, de perte d'information et d'obsolescence du contenu d'un des repos.

Avec un plugin ?

A priori ce problème n'est adressé par aucun plugin.

Avec un script bash

Voici une solution qui repose sur les outils linux "grep" "find" et "rsync"... et un peu bash.

Tout d'abord il vous faut un marquage des documents à dupliquer, ce sera avec une property vaultsync: true et vaultsync_target:...:

---
vaultsync_origin: obsperso
vaultsync_target: omps-business-analytics
---

Puis définissez le script dans un fichier vaultsync.sh:

#!/bin/bash
# Copyright Nicolas BOSSARD 2023.

# Initialize verbose flag to false
verbose=false

# Check for options and set the verbose flag accordingly
while getopts ":v" opt; do
  case $opt in
    v)
      verbose=true
      ;;
    \?)
      echo "Invalid option: -$OPTARG" >&2
      exit 1
      ;;
  esac
done
# Remove the processed options from the command line arguments
shift "$((OPTIND-1))"

# Check that three arguments (source_folder, destination_folder, and SPECIAL_MARKER) are provided.
if [ $# -ne 3 ]; then
  echo "Usage: $0 source_folder destination_folder SPECIAL_MARKER"
  exit 1
fi

# Assign the arguments to the variables source_folder, destination_folder, and SPECIAL_MARKER.
source_folder="$1"
destination_folder="$2"
SPECIAL_MARKER="$3"


# Use the find command to locate all ".md" files with the SPECIAL_MARKER in the source directory if verbose is enabled.
if [ "$verbose" = true ]; then
	echo "Searching for files with the marker \"$SPECIAL_MARKER\" in folder \"$source_folder\" : "
  echo "--------------------------------------------------------------------"
  #
  # Use the find command to locate all ".md" files with the SPECIAL_MARKER in the source directory.
  # -type f: Find only files (not directories).
  # -name "*.md": Filter by files with the ".md" extension.
  # -exec grep -q "$SPECIAL_MARKER" {} \;: Execute a quiet (non-verbose) grep to search for SPECIAL_MARKER in each file.
  # -exec echo {} \;: Display the file paths that contain SPECIAL_MARKER.
  find "$source_folder" -type f  -name "*.md" -exec grep -q "$SPECIAL_MARKER" {} \; -exec echo {} \;
  echo "--------------------------------------------------------------------"
  echo ""
fi

# Utilisez la commande rsync pour synchroniser les fichiers trouvés dans le répertoire de destination.
# -av: Archive mode for recursive copying with verbose output.
# --files-from=...   Specifies the list of files to be synchronized.
echo "Synchronizing files..."
rsync -av --files-from=<(find "$source_folder" -type f   -name "*.md" -exec grep -q "$SPECIAL_MARKER" {} \; -exec echo {} \;) "$source_folder/" "$destination_folder/"

Fini, voici l'usage :

./vaultsync.sh -v source destination marker 

./vaultsync.sh -v ~/Perso/obsidian/obsperso ~/documentation "synchronisevaults: true"

intégration dans Obsidian

On peut peaufiner en utilisant le Plugin Obsidian obsidian-shellcommands