Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# This script is to take daily backup of mongodb
#**** Created by Manish Shrivastava ****
#=================================================
# Instead of backing up on set days this will check the time differences
# between the last backup and the time the script running.
# Useful if you're backing up irregularly on a laptop
#!/usr/local/ruby/bin/ruby -w
require 'time'
Backpath = "" # backup directory
Host = "" # mongo host to connect,empty is for local
Database = "" # database to use. empty is for all
Collection = "" # collection to use
User = ""
Passwd = ""
Port = ""
Now = Time.now
DATE = Now.strftime("%Y-%m-%d")
Lastweek = Now - (60*60*24*7)
Lastmonth = Now - (60*60*24*30)
# Form request options
opt = ""
opt = opt << " --host #{Host}" unless Host.empty?
opt = opt << " --port #{Port}" unless Port.empty?
opt = opt << " --username #{User} --password #{Passwd}" unless User.empty?
opt = opt << " --db #{Database}" unless Database.empty?
opt = opt << " --collection #{Collection}" unless Collection.empty?
Opt = opt
def dbdump(bkpath)
puts "mongodump --out #{bkpath} #{Opt}"
`/usr/local/bin/mongodump --out #{bkpath} #{Opt}`
# sleep(60*1)
end
# Removes old backups
def remove_old(folder, time_from)
filenames = Dir.entries("#{Backpath}/#{folder}/").select {|filename| /^(\d+)-(\d+)-(\d+)$/.match(filename)}
# Remove old backup
filenames.each do |filename|
`rm -rf "#{Backpath}/#{folder}/#{filename}/"` if Time.parse(filename) < time_from
end
end
# Check if backup is required
def entries(folder)
filenames = Dir.entries("#{Backpath}/#{folder}/").select {|filename| /^(\d+)-(\d+)-(\d+)$/.match(filename)}
end
def last_time(entries)
entries.empty? ? nil : Time.parse(entries[entries.length-1])
end
#Create required directory
#first check backup directory exist?
if Dir[Backpath] == []
`mkdir -p #{Backpath}`
end
#check daily exist?
if Dir["#{Backpath}/daily"] == []
`mkdir -p '#{Backpath}/daily'`
end
#check weekly exist?
if Dir["#{Backpath}/weekly"] == []
`mkdir -p '#{Backpath}/weekly'`
end
#check monthly exist?
if Dir["#{Backpath}/monthly"] == []
`mkdir -p '#{Backpath}/monthly'`
end
#make monthly backup
monthly_entries = entries('monthly')
if monthly_entries.empty? || last_time(monthly_entries) < Lastmonth
puts "Monthly backup begin.."
bkpath=Backpath+"/monthly/"+DATE
dbdump bkpath
end
#make daily backup
weekly_entries = entries('weekly')
if weekly_entries.empty? || last_time(weekly_entries) < Lastweek
# Delete backups older than a month"
remove_old("weekly", Lastmonth)
puts "Weekly backup begin.."
bkpath=Backpath+"/weekly/"+DATE
dbdump bkpath
end
daily_entries = entries('daily')
if daily_entries.empty? || !(last_time(daily_entries) == DATE)
puts "Delete backup 7 days ago"
remove_old("daily", Lastweek)
bkpath=Backpath+"/daily/"+DATE
puts "Daily backup begin..."
dbdump bkpath
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.