迁移jfrog到另外的jfrog中

发布于 2024年03月01日

将7.38.7版本的jfrog里的数据迁移到回退版本6.23.42版本中
#在官网下载jfrog-cli工具。
https://jfrog.com/getcli

这边使用了脚本来进行迁移,首先需要在jfrog-cli地址添加原先jfrog与新jfrog信息
使用jf c add添加:

jf c add 

Enter a unique server identifier: jfrog_8184 

JFrog Platform URL: http://10.30.4.56:8184 

JFrog Artifactory URL: http://10.30.4.56:8084/artifactory 

JFrog username: appuser 

JFrog password or API key: 

Is the Artifactory reverse proxy configured to accept a client certificate? (y/n) [n]? n 

Your JFrog URL uses an insecure HTTP connection, instead of HTTPS. Are you sure you want to continue? (y/n) [n]? y

这样就添加好了一个,需要两个都添加。
在服务器中运行下面的脚本,Jfrog_ID与Repo参数需要自行修改。

import subprocess, re

def get_artifact_count(Repo, path, Jfrog_ID):
    Jfrog_Use = "jf config use {}".format(Jfrog_ID)
    subprocess.run(Jfrog_Use, shell=True, check=True)
    command = 'jf rt search "{}/{}/*"'.format(Repo, path)
    try:
        output_bytes = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT)
        output = output_bytes.decode('utf-8')
        match = re.search(r'Found (\d+) artifact(s?)', output)
        if match:
            return int(match.group(1))
        else:
            return 0
    except subprocess.CalledProcessError as e:
        print("Error executing command: " + command)
        print(e.output.decode())
        return 0

def extract_paths(Repo, Jfrog_ID):
    Jfrog_Use = "jf config use {}".format(Jfrog_ID)
    subprocess.run(Jfrog_Use, shell=True, check=True)
    command = "jf rt search --recursive=false --include-dirs {}/ | grep path".format(Repo)
    output_bytes = subprocess.check_output(command, shell=True)
    output = output_bytes.decode('utf-8')
    output_lines = output.split('\n')
    output_lines = [line for line in output_lines if line.strip()]
    pattern = re.compile('"path": "{}/([^"]*)"'.format(Repo))
    matches = re.findall(pattern, '\n'.join(output_lines))
    integrated_paths = [path.replace(r'\u0026', '&') for path in matches]
    return integrated_paths

def compare_artifacts(Repo, Jfrog_ID_1, Jfrog_ID_2):
    paths = extract_paths(Repo, Jfrog_ID_1)
    for path in paths:
        count_8084 = get_artifact_count(Repo, path, Jfrog_ID_1)
        count_8184 = get_artifact_count(Repo, path, Jfrog_ID_2)
        if count_8084 != count_8184:
            Jfrog_Use1 = "jf config use {}".format(Jfrog_ID_1)
            Jfrog_Use2 = "jf config use {}".format(Jfrog_ID_2)
            subprocess.run(Jfrog_Use2, shell=True, check=True)
            Delete_Command = 'jf rt del "{}/{}" --quiet=true'.format(Repo, path)
            subprocess.run(Delete_Command, shell=True, check=True)
            subprocess.run(Jfrog_Use1, shell=True, check=True)
            Download_Command = 'jf rt dl "{}/{}/*" .'.format(Repo, path)
            subprocess.run(Download_Command, shell=True, check=True)
            subprocess.run(Jfrog_Use2, shell=True, check=True)
            Upload_Command = 'jf rt u "{}/*" "{}/"'.format(path, Repo)
            subprocess.run(Upload_Command, shell=True, check=True)
            #Delete_Command = 'rm -rf "{}"'.format(path)
            #subprocess.run(Delete_Command, shell=True, check=True)

if __name__ == '__main__':
    Repo = "Test"
    Jfrog_ID_1 = "jfrog_8084"
    Jfrog_ID_2 = "jfrog_8184"
    compare_artifacts(Repo, Jfrog_ID_1, Jfrog_ID_2)

这个脚本的大致意思是获取两个jfrog存储库各个目录的文件数量,如果不一致,那么就下载旧jfrog目录,然后在新jfrog中删除不一致的目录,进行上传。(不能直接全部下载上传,因为总共数据量太大,上传中断的话不好找寻失败的工件)
需要迁移哪个存储库只需要更改Repo。
Jfrog_ID_1为旧的jfrog,Jfrog_ID_2为新的jfrog。