我一直在寻找,但我找不到我问题的答案。
我要做的是连接到openshift容器的远程shell并创建db转储,如果我手工放置用户名,密码和db名称(实际值),则可以工作。
我希望执行这个命令来访问env变量:(这个命令稍后将成为更大脚本的一部分)
oc rsh mon-rs-nr-0 mongodump --host=rs/mon-rs-nr-0.mon-rs-nr.xxx.svc.cluster.local,mon-rs-nr-1.xxx.svc.cluster.local,mon-rs-nr-2.mon-rs-nr.xxx.svc.cluster.local --username=$MONGODB_USER --password=$MONGODB_PASSWORD --authenticationDatabase=$MONGODB_DATABASE
但它不起作用,我也尝试了不同的版本echo等(env变量不被替换为它们的值)。环境变量存在于容器内。
当我尝试
oc rsh mon-rs-nr-0 echo "$MONGODB_PASSWORD"
我接待
$MONGODB_PASSWORD
但是当我首先连接到容器,然后执行命令:
C:UsersxxxxDesktop>oc rsh mon-rs-nr-0
$ echo "$MONGODB_PASSWORD"
mAYXXXXXXXXXXX
它的工作原理。但是我需要以我在顶部展示的方式使用它,有人知道变通方法吗?
感谢@msaw328评论,这里是解决方案:
C:UsersxxxDesktop>oc rsh mon-rs-nr-0 bash -c "mongodump --host=rs/mon-rs-nr-0.mon-rs-nr.xxx.svc.cluster.local,mon-rs-nr-1.mon-rs-nr.xxx.svc.cluster.local,mon-rs-nr-2.mon-rs-nr.xxx.svc.cluster.local --username=$MONGODB_USER --password=$MONGODB_PASSWORD --authenticationDatabase=$MONGODB_DATABASE"
输出:
Defaulted container "mongodb" out of: mongodb, mongodb-sidecar, mongodb-exporter
2021-08-20T11:01:12.268+0000 writing xxx.yyy to
2021-08-20T11:01:12.269+0000 writing xxx.ccc to
2021-08-20T11:01:12.269+0000 writing xxx.ddd to
2021-08-20T11:01:12.269+0000 writing xxx.eee to
2021-08-20T11:01:12.339+0000 done dumping xxx.eee (11 documents)
2021-08-20T11:01:12.339+0000 writing xxx.zzz to
2021-08-20T11:01:12.340+0000 done dumping xxx.ccc (24 documents)
2021-08-20T11:01:12.340+0000 writing xxx.bbb to
2021-08-20T11:01:12.340+0000 done dumping xxx.ddd (24 documents)
2021-08-20T11:01:12.340+0000 writing xxx.fff to
2021-08-20T11:01:12.436+0000 done dumping xxx.yyy (1000 documents)
2021-08-20T11:01:12.436+0000 writing xxx.ggg to
2021-08-20T11:01:12.436+0000 done dumping xxx.bbb (3 documents)
2021-08-20T11:01:12.437+0000 writing xxx.aaa to
2021-08-20T11:01:12.441+0000 done dumping xxx.fff (0 documents)
2021-08-20T11:01:12.441+0000 done dumping xxx.zzz (3 documents)
2021-08-20T11:01:12.447+0000 done dumping xxx.aaa(0 documents)
2021-08-20T11:01:12.449+0000 done dumping xxx.ggg (0 documents)