我每周和每天都有两个mongodb转储。每日转储最近30天。我尝试做的是从每周基于最后一次ObjectId的每日备份转储,并将每日转储恢复到每周。
我有这个bash脚本:
#/bin/bash
echo "Choose date from list:"
s3cmd ls s3://backups/weekly/
read -p "Date (YY-MM-DD):" WEEKLY_DATE
read -p "Insert db name (e.g. staging.example.com):" WEEKLY_DB
s3cmd get s3://backups/weekly/$WEEKLY_DATE/$WEEKLY_DB.tar.gz
echo "Extracting archive ..."
tar -xvf $WEEKLY_DB.tar.gz
echo "Restore weekly backup ..."
mongorestore data/weekly/$WEEKLY_DATE/$WEEKLY_DB/
#get latest ObjectId from current weekly dump
echo "Get latest ObjectId from current weekly dump"
for h in localhost; do
dbs=`mongo --eval "db.getMongo().getDBNames()" --host "$h" | grep '"' | tr -d '",' `
for db in $dbs; do
cols=`mongo "$db" --host "$h" --quiet --eval "db.getCollectionNames()" | tr -d ',"[]' `
for collection in $cols; do
oid=$(mongo "$db" --eval "db.getCollection(\"$collection\").find().sort({\$natural:-1}).limit(1).map(function(o) {return o._id})[0];" | grep -e "ObjectId")
#dump from daily backup based on latest weekly ObjectId
mongodump --host 192.168.0.1 -q "{_id:{\$gte:$oid}}" -d "$db" -c "$collection" --out daily
#restore from daily to weekly db
for dir in `ls daily`; do
mongorestore --nsInclude "$db" daily/${dir}
done
done
done
done
我得到了这个错误:
2017-09-20T13:12:57.218+0000 Failed: error parsing query as json: invalid character '}' looking for beginning of value
2017-09-20T13:12:57.228+0000 preparing collections to restore from
2017-09-20T13:12:57.228+0000 don't know what to do with file "daily/dbname/file.bson", skipping...
2017-09-20T13:12:57.228+0000 don't know what to do with file "daily/dbname/file.metadata.json", skipping...
2017-09-20T13:12:57.228+0000 don't know what to do with file "daily/dbname/file_data.bson", skipping...
2017-09-20T13:12:57.228+0000 don't know what to do with file "daily/dbname/file_data.metadata.json", skipping...
2017-09-20T13:12:57.228+0000 done