Task:
Create a Cronjob that executes a bash script once every night. The bash script should do a mongoDB Backup and push it to a Git Repo.
So I created a cronjob as the root user via the crontab -e command:
#!/bin/bash
SHELL=/bin/bash
USER=root
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PWD=/root
HOME=/root
0 2 * * * /path/script.sh > /path/script.log
This is the script that should be executed:
BAK="/path"
#MONGO="/usr/bin/mongo"
#MONGODUMP="/usr/bin/mongodump"
#GIT="/usr/bin/git"
echo "starting backup"
echo $BAK
/usr/bin/mongodump --username=myusername --password=mypassword --db=mydatabase --out=$BAK$
echo "backup created"
/usr/bin/git add .
/usr/bin/git commit -m "backup from `date +'%Y_%m_%d'`"
echo "committed"
/usr/bin/git push -u origin master
echo "pushed backup. done"
Outcome:
Backups are being created but not pushed to the repository. Executing the script manually pushes the backups as desired. The Log only includes all the echo's from the script.
* 2 * * *is set to run “At every minute of 2nd hour.” If you want to run it “At 02:00 every day.” you should set it like0 2 * * *