我制作了一个脚本来执行数据库备份。
#!/bin/bash
set -e
STORAGE_NAME=folder-backup
BACKUP_NAME=$(date +%y_%m_%d).gz
date
echo "Backing up MongoDB database to storage: $STORAGE_NAME"
echo "Dumping MongoDB $DB database to compressed archive"
mongodump -u user -p 1234 --authenticationDatabase admin --archive=/backup/tmp_dump.gz --gzip
echo "Copying compressed archive to storage: $STORAGE_NAME"
gsutil cp /backup/tmp_dump.gz gs://$STORAGE_NAME/$BACKUP_NAME
echo "Cleaning up compressed archive"
rm /backup/tmp_dump.gz
echo "Backup complete!"
当我手动运行它时,它可以工作并发送备份文件。
Backing up MongoDB database to storage: -backup
Dumping MongoDB database to compressed archive
2019-09-09T06:03:23.513-0400 writing admin.system.indexes to archive '/backup/tmp_dump.gz'
2019-09-09T06:03:23.557-0400 done dumping admin.system.indexes (3 documents)
2019-09-09T06:03:23.557-0400 writing config.system.indexes to archive '/backup/tmp_dump.gz'
2019-09-09T06:03:23.596-0400 done dumping config.system.indexes (2 documents)
2019-09-09T06:03:23.596-0400 writing admin.system.users to archive '/backup/tmp_dump.gz'
2019-09-09T06:03:23.654-0400 done dumping admin.system.users (2 documents)
2019-09-09T06:03:23.654-0400 writing admin.system.version to archive '/backup/tmp_dump.gz'
2019-09-09T06:03:23.712-0400 done dumping admin.system.version (2 documents)
2019-09-09T06:03:23.712-0400 writing test.AccountService to archive '/backup/tmp_dump.gz'
2019-09-09T06:03:23.722-0400 writing test.Settings to archive '/backup/tmp_dump.gz'
2019-09-09T06:03:23.732-0400 writing test._User to archive '/backup/tmp_dump.gz'
2019-09-09T06:03:23.747-0400 writing test.NotificationSettings to archive '/backup/tmp_dump.gz'
2019-09-09T06:03:24.112-0400 done dumping test._User (2506 documents)
2019-09-09T06:03:24.112-0400 writing test._Session to archive '/backup/tmp_dump.gz'
2019-09-09T06:03:24.131-0400 done dumping test.NotificationSettings (2240 documents)
2019-09-09T06:03:24.131-0400 writing test._PushStatus to archive '/backup/tmp_dump.gz'
2019-09-09T06:03:24.135-0400 done dumping test.Settings (2522 documents)
2019-09-09T06:03:24.135-0400 writing test.TaxEvent to archive '/backup/tmp_dump.gz'
2019-09-09T06:03:24.152-0400 done dumping test.AccountService (4887 documents)
2019-09-09T06:03:24.153-0400 writing test.fs.chunks to archive '/backup/tmp_dump.gz'
2019-09-09T06:03:24.392-0400 done dumping test._Session (2139 documents)
2019-09-09T06:03:24.392-0400 writing test.fs.files to archive '/backup/tmp_dump.gz'
2019-09-09T06:03:24.555-0400 [........................] test.fs.files 0/90 (0.0%)
2019-09-09T06:03:24.555-0400 [########################] test.fs.files 90/90 (100.0%)
2019-09-09T06:03:25.322-0400 done dumping test.TaxEvent (217 documents)
2019-09-09T06:03:25.322-0400 writing test.CalendarEvent to archive '/backup/tmp_dump.gz'
2019-09-09T06:03:25.333-0400 done dumping test._PushStatus (348 documents)
2019-09-09T06:03:25.333-0400 writing test.UserPurchase to archive '/backup/tmp_dump.gz'
2019-09-09T06:03:25.345-0400 done dumping test.fs.files (90 documents)
2019-09-09T06:03:25.345-0400 writing test.EventKnowledge to archive '/backup/tmp_dump.gz'
2019-09-09T06:03:25.356-0400 done dumping test.fs.chunks (122 documents)
2019-09-09T06:03:25.356-0400 writing test.KnowledgeWebsite to archive '/backup/tmp_dump.gz'
2019-09-09T06:03:25.478-0400 done dumping test.CalendarEvent (60 documents)
2019-09-09T06:03:25.478-0400 writing test._SCHEMA to archive '/backup/tmp_dump.gz'
2019-09-09T06:03:25.518-0400 done dumping test.EventKnowledge (43 documents)
2019-09-09T06:03:25.518-0400 writing test.News to archive '/backup/tmp_dump.gz'
2019-09-09T06:03:25.553-0400 done dumping test.UserPurchase (45 documents)
2019-09-09T06:03:25.553-0400 writing test.objectlabs-system.admin.collections to archive '/backup/tmp_dump.gz'
2019-09-09T06:03:25.565-0400 done dumping test.KnowledgeWebsite (26 documents)
2019-09-09T06:03:25.565-0400 writing test.objectlabs-system to archive '/backup/tmp_dump.gz'
2019-09-09T06:03:25.643-0400 done dumping test._SCHEMA (13 documents)
2019-09-09T06:03:25.643-0400 writing test._GlobalConfig to archive '/backup/tmp_dump.gz'
2019-09-09T06:03:25.711-0400 done dumping test.News (5 documents)
2019-09-09T06:03:25.711-0400 writing test._Installation to archive '/backup/tmp_dump.gz'
2019-09-09T06:03:25.753-0400 done dumping test.objectlabs-system.admin.collections (3 documents)
2019-09-09T06:03:25.753-0400 writing test._Role to archive '/backup/tmp_dump.gz'
2019-09-09T06:03:25.806-0400 done dumping test._GlobalConfig (1 document)
2019-09-09T06:03:25.824-0400 done dumping test.objectlabs-system (1 document)
2019-09-09T06:03:25.863-0400 done dumping test._Installation (0 documents)
2019-09-09T06:03:25.863-0400 done dumping test._Role (0 documents)
Copying compressed archive to storage: folder-backup
Copying file:///backup/tmp_dump.gz [Content-Type=application/octet-stream]...
| [1 files][ 13.4 MiB/ 13.4 MiB]
Operation completed over 1 objects/13.4 MiB.
Cleaning up compressed archive
Backup complete!
我决定做这个任务。
* * * * * /mongo_backup.sh >> /mongo_backup.log
文件mongo_backup.log
被写入
Mon Sep 9 05:57:01 EDT 2019
Backing up MongoDB database to storage: folder-backup
Dumping MongoDB database to compressed archive
Copying compressed archive to storage: folder-backup
Mon Sep 9 05:58:01 EDT 2019
Backing up MongoDB database to storage: folder-backup
Dumping MongoDB database to compressed archive
Copying compressed archive to storage: folder-backup
Mon Sep 9 05:59:01 EDT 2019
Backing up MongoDB database to storage: folder-backup
Dumping MongoDB database to compressed archive
Copying compressed archive to storage: folder-backup
Mon Sep 9 06:00:01 EDT 2019
Backing up MongoDB database to storage: folder-backup
Dumping MongoDB database to compressed archive
Copying compressed archive to storage: folder-backup
Mon Sep 9 06:01:01 EDT 2019
Backing up MongoDB database to storage: folder-backup
Dumping MongoDB database to compressed archive
Copying compressed archive to storage: folder-backup
Mon Sep 9 06:02:01 EDT 2019
Backing up MongoDB database to storage: folder-backup
Dumping MongoDB database to compressed archive
Copying compressed archive to storage: folder-backup
该文件mongo_backup.log
缺少行
Copying file:///backup/tmp_dump.gz [Content-Type=application/octet-stream]...
| [1 files][ 13.4 MiB/ 13.4 MiB]
Operation completed over 1 objects/13.4 MiB.
Cleaning up compressed archive
Backup complete!
它从我的购物车中丢失了。
为什么会这样?该脚本手动运行良好,发送了数据库备份文件,但crontab
发送失败。
Mon Sep 9 06:18:01 EDT 2019
Backing up MongoDB database to storage: folder-backup
Dumping MongoDB database to compressed archive
2019-09-09T06:18:03.950-0400 writing admin.system.indexes to archive '/backup/tmp_dump.gz'
2019-09-09T06:18:03.990-0400 done dumping admin.system.indexes (3 documents)
2019-09-09T06:18:03.990-0400 writing config.system.indexes to archive '/backup/tmp_dump.gz'
2019-09-09T06:18:04.030-0400 done dumping config.system.indexes (2 documents)
2019-09-09T06:18:04.030-0400 writing admin.system.users to archive '/backup/tmp_dump.gz'
2019-09-09T06:18:04.087-0400 done dumping admin.system.users (2 documents)
2019-09-09T06:18:04.087-0400 writing admin.system.version to archive '/backup/tmp_dump.gz'
2019-09-09T06:18:04.144-0400 done dumping admin.system.version (2 documents)
2019-09-09T06:18:04.144-0400 writing test.AccountService to archive '/backup/tmp_dump.gz'
2019-09-09T06:18:04.152-0400 writing test.Settings to archive '/backup/tmp_dump.gz'
2019-09-09T06:18:04.160-0400 writing test._User to archive '/backup/tmp_dump.gz'
2019-09-09T06:18:04.175-0400 writing test.NotificationSettings to archive '/backup/tmp_dump.gz'
2019-09-09T06:18:04.567-0400 done dumping test.AccountService (4887 documents)
2019-09-09T06:18:04.567-0400 writing test._Session to archive '/backup/tmp_dump.gz'
2019-09-09T06:18:04.574-0400 done dumping test.Settings (2522 documents)
2019-09-09T06:18:04.575-0400 writing test._PushStatus to archive '/backup/tmp_dump.gz'
2019-09-09T06:18:04.642-0400 done dumping test.NotificationSettings (2240 documents)
2019-09-09T06:18:04.642-0400 writing test.TaxEvent to archive '/backup/tmp_dump.gz'
2019-09-09T06:18:04.652-0400 done dumping test._User (2506 documents)
2019-09-09T06:18:04.652-0400 writing test.fs.chunks to archive '/backup/tmp_dump.gz'
2019-09-09T06:18:04.855-0400 done dumping test.TaxEvent (217 documents)
2019-09-09T06:18:04.855-0400 writing test.fs.files to archive '/backup/tmp_dump.gz'
2019-09-09T06:18:04.951-0400 done dumping test._PushStatus (348 documents)
2019-09-09T06:18:04.951-0400 writing test.CalendarEvent to archive '/backup/tmp_dump.gz'
2019-09-09T06:18:04.955-0400 [#######################.] test.fs.chunks 119/122 (97.5%)
2019-09-09T06:18:04.955-0400 done dumping test._Session (2139 documents)
2019-09-09T06:18:04.955-0400 writing test.UserPurchase to archive '/backup/tmp_dump.gz'
2019-09-09T06:18:04.982-0400 [########################] test.fs.chunks 122/122 (100.0%)
2019-09-09T06:18:05.958-0400 done dumping test.fs.files (90 documents)
2019-09-09T06:18:05.958-0400 writing test.EventKnowledge to archive '/backup/tmp_dump.gz'
2019-09-09T06:18:05.969-0400 done dumping test.fs.chunks (122 documents)
2019-09-09T06:18:05.969-0400 writing test.KnowledgeWebsite to archive '/backup/tmp_dump.gz'
2019-09-09T06:18:05.988-0400 done dumping test.UserPurchase (45 documents)
2019-09-09T06:18:05.988-0400 writing test._SCHEMA to archive '/backup/tmp_dump.gz'
2019-09-09T06:18:06.148-0400 done dumping test.CalendarEvent (60 documents)
2019-09-09T06:18:06.148-0400 writing test.News to archive '/backup/tmp_dump.gz'
2019-09-09T06:18:06.163-0400 done dumping test._SCHEMA (13 documents)
2019-09-09T06:18:06.163-0400 writing test.objectlabs-system.admin.collections to archive '/backup/tmp_dump.gz'
2019-09-09T06:18:06.175-0400 done dumping test.EventKnowledge (43 documents)
2019-09-09T06:18:06.175-0400 writing test.objectlabs-system to archive '/backup/tmp_dump.gz'
2019-09-09T06:18:06.188-0400 done dumping test.KnowledgeWebsite (26 documents)
2019-09-09T06:18:06.188-0400 writing test._GlobalConfig to archive '/backup/tmp_dump.gz'
2019-09-09T06:18:06.299-0400 done dumping test.News (5 documents)
2019-09-09T06:18:06.299-0400 writing test._Installation to archive '/backup/tmp_dump.gz'
2019-09-09T06:18:06.349-0400 done dumping test.objectlabs-system.admin.collections (3 documents)
2019-09-09T06:18:06.349-0400 writing test._Role to archive '/backup/tmp_dump.gz'
2019-09-09T06:18:06.372-0400 done dumping test.objectlabs-system (1 document)
2019-09-09T06:18:06.378-0400 done dumping test._GlobalConfig (1 document)
2019-09-09T06:18:06.449-0400 done dumping test._Role (0 documents)
2019-09-09T06:18:06.449-0400 done dumping test._Installation (0 documents)
Copying compressed archive to storage: folder-backup
/mongo_backup.sh: line 10: gsutil: command not found
我从评论中总结了答案:
要分析在 cron 中运行作业的问题,您需要截取 stderr 流并将其写入日志,您可以这样做:*
养成在 cron 作业中指定环境变量和 shell 的习惯*
题外话:在每分钟运行一次脚本时,建议通过
flock -w0 /var/run/1.lock /mongo_backup.sh
cron作业的命令来保护自己免于多次执行,以此类推:现在创建备份的过程不会花费太多时间,但是随着数据量的增加或设备负载的增加,归档可能不会在分配的分钟内结束,并且会发生重新启动。