0

I have a remote .sh script to take backup of mySQL file. Cron is running by .sh file not executing. Content of Crontab are:

#SHELL=/bin/bash PATH=/bin:/sbin:/usr/bin:/usr/sbin 
* * * * * /home/ubuntu/dscript/db_backup.sh > /tmp/db.output

File Contents are:

#!/bin/bash

DATE=$(date +%Y%m%d_%H%M)

zipname="db_prod_$DATE.sql"
mysqldump db_prod > $zipname
aws s3 mv $zipname s3://mybackups/$zipname
rm -rf $zipname

zipname="db_staging_$DATE.sql"
mysqldump db_staging > $zipname
aws s3 mv $zipname s3://mybackups/$zipname
rm -rf $zipname

Bash path

bash: /bin/bash /etc/bash.bashrc /usr/share/man/man1/bash.1.gz

I am on Ubuntu.

File is is in +x mode.

2 Answers 2

1
#!/usr/bin/bash

DATE=$(date +%Y%m%d_%H%M)
zipname="db_prod_$DATE.sql"
SCRIPT_LOG_FILE="/tmp/backup.log"

echo "[${DATE}] [INFO] Starting personnal backup" > ${SCRIPT_LOG_FILE}
$(which mysqldump) db_prod > $zipname
if [ "$?" -eq "0" ] && [ -f ${zipname} ];then
    echo "[$(date +%Y%m%d_%H%M)] [INFO] Mysqldump save file ${zipname}" > ${SCRIPT_LOG_FILE}
    # file is created continue job with this file ...
    $(which aws) s3 mv $zipname s3://mybackups/$zipname
    rm -rf $zipname
else
    echo "[$(date +%Y%m%d_%H%M)] [ERROR] Mysqldump can't save file ${zipname}" > ${SCRIPT_LOG_FILE} 
fi
exit 0

And crontab -l must be:

* * * * * /home/ubuntu/dscript/db_backup.sh &> /tmp/error.and.result.db.output 
Sign up to request clarification or add additional context in comments.

Comments

0

check cron error: try to see error with:

~$ sudo cat /var/log/syslog|grep CRON|grep db_backup.sh
~$ sudo cat /var/log/syslog|grep CRON|grep MY_USERNAME

(Permission denied or other)

where MY_USERNAME is the user execute : crontab -e

check script error:

* * * * * /home/ubuntu/dscript/db_backup.sh > /tmp/result.db.output
* * * * * /home/ubuntu/dscript/db_backup.sh 2> /tmp/error.db.output

or simply

* * * * * /home/ubuntu/dscript/db_backup.sh &> /tmp/error.and.result.db.output 

The question is whether the error comes from the execution of cron or is in the script, the above methods will help you find the source of the problem.

4 Comments

output file is created but blank. The above command produced following: CRON[29315]: (ubuntu) CMD (/home/ubuntu/deployment_scripts/db_backup.sh > /tmp/db.output)
excuse me , have you really try: &> /tmp/error.and.result.db.output
empty output is normal in your case, put an echo in your file: echo "script start"
Yes.. now I got it.. it says: /home/ubuntu/dscript/db_backup.sh: line 7: aws: command not found /home/ubuntu/dscript/db_backup.sh: line 12: aws: command not found

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.