[英]bash: How do I write a shell script to sftp a mongodump from a MongoDB Docker container inside a DigitalOcean droplet to backup the MongoDB database?
I'm running a MeteorJS webapp deployed with meteor-up on a DO droplet. 我正在运行将Meteor-up部署在DO小滴上的MeteorJS Webapp。 I would like to have a .sh run from my local machine to get backups occasionally.
我希望从本地计算机运行.sh偶尔获取备份。 Folders being able to have timestamps would be a plus.
能够带有时间戳的文件夹将是一个加号。
This is what I am trying to achieve with a single .sh file: 这是我试图通过一个.sh文件实现的目标:
on local machine: 在本地计算机上:
ssh root@my.droplet.address
# <prompt for password>
inside droplet: 内滴:
docker exec -it mongodb bash
in mongodb docker: 在mongodb docker中:
rm -rf dump
mongodump -h 127.0.0.1 -d app
on remote droplet: 在远程液滴上:
rm -rf dump
docker cp mongodb:/dump dump
on local machine: 在本地计算机上:
sftp root@my.droplet.address
in remote droplet (sftp): 在远程Droplet(sftp)中:
DATE=`date +%Y-%m-%d_%H%M%S`
get -r dump $DATE
Is it possible to get all these in one .sh file? 是否可以将所有这些文件放在一个.sh文件中?
In the script you can run the commands in the following way: 在脚本中,您可以通过以下方式运行命令:
ssh root@my.droplet.address "docker exec mongodb rm -rf dump && docker exec mongodb mongodump -h 127.0.0.1 -d app && rm -rf dump && docker cp mongodb:/dump dump && docker cp mongodb:/dump dump"
ssh root@my.droplet.address:/dump $(date +%Y-%m-%d_%H%M%S) >/dev/null 2
This is how I managed to do it with one .sh file. 这就是我设法用一个.sh文件做到这一点的方法。 Without ssh keys, this will prompt you twice for the root password.
如果没有ssh键,这将提示您两次输入root密码。 the dump folder will be copied to ./$DATE.
转储文件夹将被复制到./$DATE。 where $DATE = current datetime on the local machine.
其中$ DATE =本地计算机上的当前日期时间。
#!/bin/bash
DATE=`date +%Y-%m-%d_%H%M%S`
ssh root@cleanr.ivanho.me "docker exec mongodb rm -rf dump && docker exec mongodb mongodump -h 127.0.0.1 -d app && rm -rf dump && docker cp mongodb:/dump dump"
scp -r root@cleanr.ivanho.me:/root/dump $DATE
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.