-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmongo-backup.sh
More file actions
executable file
·91 lines (74 loc) · 2.02 KB
/
Copy pathmongo-backup.sh
File metadata and controls
executable file
·91 lines (74 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/bin/bash
# Shell Script To Backup Mongo Databases
# Author: V. Alex Brennen <vab@cryptnet.net>
# Copyright: None
# License: Public Domain
# Created: 2017.02.08
# Dependencies: Mongodump (MongoDB) and bzip2 (for optional compression)
# Description: This script to used to create a bson dump of all dbs on a
# MonogoDB installation.
# Programs that will be used
MONGODUMP=/usr/bin/mongodump
BZIP2=/usr/bin/bzip2
NICE=/bin/nice
# File locations
BACKDIR=/mnt/backups/database_exports/mongo
# Other settings
## Shell User
SHUSER=mongoback
## Compression level
CLVL="-9"
## Use nice
BE_NICE=1
## Nice level
NLVL=19
# Get the date information that we'll use for the backup directories and
# to construct the backup file names
YEAR="$(date +"%Y")"
MONTH="$(date +"%m")"
DAY="$(date +"%d")"
# Make Sure the back-up directory for the current year exists and is
# writable by the back-up script.
BACKDIR="$BACKDIR/$YEAR"
if [ ! -d "$BACKDIR" ]; then
eval "/bin/mkdir $BACKDIR"
fi
if [ ! -w "$BACKDIR" ]; then
eval "/bin/chown $SHUSER:$SHUSER $BACKDIR"
eval "/bin/chmod 775 $BACKDIR"
fi
# Make sure the back-up directory for the current month exists and is
# writable by the back-up script.
BACKDIR="$BACKDIR/$MONTH"
if [ ! -d "$BACKDIR" ]; then
eval "/bin/mkdir $BACKDIR"
fi
if [ ! -w "$BACKDIR" ]; then
eval "/bin/chown $SHUSER:$SHUSER $BACKDIR"
eval "/bin/chmod 775 $BACKDIR"
fi
# Dump The databases and un/pw metadata
## note need date code here
eval "$MONGODUMP --out $BACKDIR/mongodump-$YEAR-$MONTH-$DAY"
# This is the function that does the actual compression and
# directory transversal.
function compress_files
{
for i in $("ls *");
do
if [ -f "$i" ]; then
if [ $BE_NICE -eq 1 ]; then
eval "$NICE -$NLVL $BZIP2 $CLVL $i"
else
eval "$BZIP2 $CLVL $i"
fi
elif [ -d "$i" ]; then
cd "$i" || exit 1
compress_files
cd .. | exit 1
fi
done
}
# Compress the bson export files
eval "cd $BACKDIR/mongodump-$YEAR-$MONTH-$DAY"
compress_files