Last active
July 15, 2024 11:31
-
-
Save sebge2emasphere/497cb264b32ac39a80864c864d522906 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
I created an npm package for this script (and gave you credit of course)
(repo)
Iam happy that it helped you ;)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
if Iam write it will merge (I tested on an empty target)