Linux – Why does the backup script work when I run it, but not when it’s run via cron

backupcronlinuxmongodbunix

This is my script, backup_mongo.sh

#!/bin/bash
suffix=$(date +%w)
rm /home/myuser/backup/mongo-$suffix -rf
mkdir /home/myuser/backup/mongo-$suffix
mongodump -h$1 -u$2 -p$3 -dmydb -o/home/myuser/backup/mongo-$suffix

In my crontab -e:

0   3  *   *   *    /bin/bash  /home/myuser/myproject/production/backup_mongo.sh  localhost user1 pass1

When I run the script normally (executing the command above), the script works and the directory "mydb" gets created in /mongo-$suffix.

However, when I put this in crontab, /mongo-$suffix gets created but the "mydb" directory does not exist. There's nothing in /mongo-$suffix.

Best Answer

You may not have mongodump in your PATH. Try using the absolute path to the mongodump command.

Related Topic