Skip to content

Instantly share code, notes, and snippets.

@sebge2emasphere
Last active July 18, 2023 09:21
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save sebge2emasphere/497cb264b32ac39a80864c864d522906 to your computer and use it in GitHub Desktop.
Save sebge2emasphere/497cb264b32ac39a80864c864d522906 to your computer and use it in GitHub Desktop.
#!/bin/bash
function usage()
{
echo "Export Nexus Repositories."
echo ""
echo "./nexus-export"
echo -e "\t--help"
echo -e "\t--localNexusUrl"
echo -e "\t--localNexusUser"
echo -e "\t--localNexusPwd"
echo -e "\t--targetNexusUrl"
echo -e "\t--targetNexusUser"
echo -e "\t--targetNexusPwd"
echo -e "\t--localBlobsDir"
echo -e "\t--ignoreRepositories"
echo ""
}
TARGET_NEXUS_URL=""
TARGET_NEXUS_USER=""
TARGET_NEXUS_PWD=""
LOCAL_BLOBS_DIR=""
LOCAL_NEXUS_URL=""
LOCAL_NEXUS_USER=""
LOCAL_NEXUS_PWD=""
IGNORED_REPOSITORIES=()
IGNORED_SUFFIXES=()
TEMP_DIR="/tmp"
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
--help)
usage
exit
;;
--targetNexusUrl)
TARGET_NEXUS_URL="$2"
shift
shift
;;
--targetNexusUser)
TARGET_NEXUS_USER="$2"
shift
shift
;;
--targetNexusPwd)
TARGET_NEXUS_PWD="$2"
shift
shift
;;
--localBlobsDir)
LOCAL_BLOBS_DIR="$2"
shift
shift
;;
--localNexusUrl)
LOCAL_NEXUS_URL="$2"
shift
shift
;;
--localNexusUser)
LOCAL_NEXUS_USER="$2"
shift
shift
;;
--localNexusPwd)
LOCAL_NEXUS_PWD="$2"
shift
shift
;;
--ignoreRepositories)
IFS=, read -r -a IGNORE_REPOSITORIES <<< "$2"
shift
shift
;;
*)
usage
exit
;;
esac
done
function findValueInPropertyFile {
while IFS='=' read -r key value
do
if [[ "${key}" == $2 ]]; then
echo "${value}"
return
fi
done < "$1"
echo ""
return
}
function trim(){
echo $@
}
function containsElement () {
local e match="$1"
shift
for e; do [[ "$e" == "$match" ]] && return 0; done
return 1
}
function isIgnoredSuffix() {
for e in "${IGNORED_SUFFIXES[@]}"; do [[ $1 == *$e ]] && return 0; done
return 1
}
rm $TEMP_DIR/nexus-* &> /dev/null
success=true
for file in $(find $LOCAL_BLOBS_DIR -type f -name "*.properties"); do
repoName=$(findValueInPropertyFile $file "@Bucket.repo-name")
repoName=$(trim $repoName)
blobName=$(findValueInPropertyFile $file "@BlobStore.blob-name")
blobName=$(trim $blobName)
if [[ ! -z "$repoName" && ! -z "$blobName" ]]; then
if containsElement "$repoName" "${IGNORE_REPOSITORIES[@]}" || isIgnoredSuffix "$blobName"; then
echo "$repoName $blobName" >> "$TEMP_DIR/nexus-not-exported.txt"
else
echo "Migrating $repoName $blobName"
echo "$repoName $blobName" >> "$TEMP_DIR/nexus-exported.txt"
curl -s -o "$TEMP_DIR/nexus-temp-file" -u "$LOCAL_NEXUS_USER:$LOCAL_NEXUS_PWD" "$LOCAL_NEXUS_URL/repository/$repoName/$blobName"
if [ $? -ne 0 ]; then
success=false
echo "Error while downloading $repoName/$blobName" >> "$TEMP_DIR/nexus-failures.txt"
fi
curl -s -u "$TARGET_NEXUS_USER:$TARGET_NEXUS_PWD" --upload-file "$TEMP_DIR/nexus-temp-file" "$TARGET_NEXUS_URL/repository/$repoName/$blobName"
if [ $? -ne 0 ]; then
success=false
echo "Error while uploading $repoName/$blobName" >> "$TEMP_DIR/nexus-failures.txt"
fi
fi
fi
done
if $success; then
exit 0
else
exit 1
fi
@rluvaton
Copy link

Is this will merge the 2 registries or overwrite?

@sebge2emasphere
Copy link
Author

if Iam write it will merge (I tested on an empty target)

@rluvaton
Copy link

rluvaton commented Jul 20, 2020

I created an npm package for this script (and gave you credit of course)

(repo)

@sebge2emasphere
Copy link
Author

sebge2emasphere commented Jul 20, 2020

Iam happy that it helped you ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment