Bash script to replace UUIDs (in OpenCms or Magnolia)

In content management systems like OpenCms or Magnolia, you often run into the problem of having to replace UUIDs.
I wrote a little Bash script for that.


import info.magnolia.importexport.DataTransporter
import javax.jcr.ImportUUIDBehavior
import groovy.io.FileType

def list = []
def dir = new File('/home/marc/c3/eos/EOS_01112018/')
dir.eachFileRecurse (FileType.FILES) { file ->
  list << file
}

list.each {
  path = it.path
  fileName = it.name
  print 'fileName = ' + fileName
  // currently path may not contain "(" and "."
  int indexOf = fileName.lastIndexOf(" (")
  if (indexOf < 0) indexOf = fileName.indexOf(".")
  workspace = fileName.substring(0, indexOf)
  println ' workspace = ' + workspace

hm = ctx.getJCRSession(workspace)
workspaceRoot = hm.getRootNode()
    
xmlFile = new File(path)
DataTransporter.importFile(xmlFile, workspace, '/', false,
ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW, true, true) 
}

I'd be happy to hear from you to support you in any Magnolia (CMS) or OpenCms project as a freelancer (Freiberufler).

#!/bin/bash
# example:
# ./replaceUUIDS.sh config.xml
# you can also use wildcards in the filename
# don't fogett to escape wildcards e.g. all files \*
# also be aware, that binaries may make the script fail, you may have to exlude them in the find expressions lieke jpg and png
#
# http://www.johnen.biz

FILE_NAME=$1

echo Finding all existing uuids 
OLDIFS=$IFS #make bash interpret escape sequences -> backslash n as a linebreak
IFS=$'\n'
UUIDS=$(find -type f -iname "$FILE_NAME" ! -iname "*.png" ! -iname "*.jpg" -print0| xargs -0 grep -hoER "[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}" | sort -u)
# for grep: h=supress output of filename, o=only show matching part E=use regex, R=only search file which match pattern, r=recursive search
# find all files matching FILE_NAME
IFS=$OLDIFS

echo Generating SED-Strings
#generating a SED-string for each UUID which will run in the loop for each file
b=0;
for i in $UUIDS;

do
  b=$(( $b + 1 ))
  NEW_UUID=$(uuidgen);
  NEW_PART=`echo "'s/$i/${NEW_UUID}/g'" | sed -e 's/-/\\\-/g'` #this replacement of - with \- may not be necessary
  echo $b = $i - $NEW_UUID;
  SED_STRING="${SED_STRING}-e${NEW_PART} ";
done

echo Finding files with UUIDS and replace them
#exchange all uuids with new ones
FILES=$(find . -type f -iname "$FILE_NAME" ! -iname "*.png" ! -iname "*.jpg")
for f in ${FILES[@]};
do
  eval sed $SED_STRING -i $f;
  echo Replacing UUIDS in file $f;
done

#exit;

Comments