1

I have a shell script that takes the backup of the Mongo DB on a daily basis. It's working as expected. Now I need to remove the backups that are older than 2 weeks. Would that be achievable with the current naming convention. Can anyone shed some light? I'm fairly new to shell scripting

#!/bin/sh
DIR=`date +%m%d%y`
DEST=/dbBackups/$DIR
mkdir $DEST
mongodump --authenticationDatabase admin -h 127.0.0.1 -d pipe -u <username> -p <password>
Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
Sharan Mohandas
  • 861
  • 1
  • 9
  • 25

1 Answers1

1

Finally got it with the below script

#!/bin/sh
DIR=`date +%m%d%y`
DEST=/dbBackups/$DIR
mkdir $DEST
mongodump --authenticationDatabase admin -h 127.0.0.1 -d pipe -u <username> -p <password>
find /dbBackups/* -type d -ctime +14 -exec rm -rf {} +

Thanks to Shell script to delete directories older than n days

Sharan Mohandas
  • 861
  • 1
  • 9
  • 25
  • 1
    With GNU find, running `-exec rm -rf {} +` will be faster as it won't spawn a separate process for each file. – Todd A. Jacobs Jun 06 '17 at 07:00
  • Just a note for people using code in this answer, I think you'll likely want "-o $DEST" at the end of the mongodump line eg mongodump --authenticationDatabase admin -h 127.0.0.1 -d pipe -u -p -o $DEST – manihiki Nov 06 '19 at 17:10