LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   .sh script won't tar when run from cron (https://www.linuxquestions.org/questions/linux-newbie-8/sh-script-wont-tar-when-run-from-cron-879486/)

jonathansfl 05-08-2011 11:41 AM

.sh script won't tar when run from cron
 
Greetings
I'm having a script / cron problem. i'm trying to run a script that backs up a postgresql database using a pgdump command.
I've included a simplified version of my script here.
The pgdump works fine, and outputs an SQL file.
The problem is with the tar command. it fails to correctly tar, and then compress, the .sql file when the .sh script is run from cron. but when run manually from command line (with sudo) it works well. Note ls -l printout, where the top lines were generated by cron (and tar & tar.gz are minisule), whereas in the second group the tar and tar.gz are much larger (and correct).
Note that the .sh script is owned by root, who is also the user in the cron entry. I tried using postgres as the user for all those, but i think i then had password problems. should i be using some kind of "credential"?
THANK YOU!!!

---------------------------------------------------------------

-rwxr-xr-x 1 root root 400 2011-05-08 12:24 testbackup.sh
-rw-r--r-- 1 root root 3945771 2011-05-08 12:28 TEST__beta_cms_main_20110508122801.sql
-rw-r--r-- 1 root root 10240 2011-05-08 12:28 TEST__beta_cms_main_20110508122801.tar
-rw-r--r-- 1 root root 45 2011-05-08 12:28 TEST__beta_cms_main_20110508122801.tar.gz

postgres@dev-postgres-1:~/backups$ sudo ./testbackup.sh
-rw-r--r-- 1 root root 3945771 2011-05-08 12:29 TEST__beta_cms_main_20110508122910.sql
-rw-r--r-- 1 root root 3952640 2011-05-08 12:29 TEST__beta_cms_main_20110508122910.tar
-rw-r--r-- 1 root root 747801 2011-05-08 12:29 TEST__beta_cms_main_20110508122910.tar.gz

---------------------------------------------------------------

Here is the script and cron entry (cron in test mode to produce every 2 minutes):
---------------------------------------------------------------
Code:

#!/bin/sh
MYDATABASENAME="beta_cms_main"
MYDATABASENAME_DATETIME="TEST_"_"$MYDATABASENAME"_$(date +"%Y%m%d%H%M%S")
pg_dump -U postgres -F c -f /home/postgres/backups/$MYDATABASENAME_DATETIME.sql $MYDATABASENAME
tar -cf /home/postgres/backups/$MYDATABASENAME_DATETIME.tar $MYDATABASENAME_DATETIME.sql
tar -czf /home/postgres/backups/$MYDATABASENAME_DATETIME.tar.gz $MYDATABASENAME_DATETIME.tar

---------------------------------------------------------------

*/2 * * * * root /home/postgres/backups/testbackup.sh >> log_BST_postgres_backup_service_cron.log

carltm 05-08-2011 11:47 AM

If it works correctly with sudo, why not add sudo to the cronjob?

*/2 * * * * root sudo /home/postgres/backups/testbackup.sh >> log_BST_postgres_backup_service_cron.log

You many need to comment the "Defaults requiretty" line in /etc/sudoers.

After thinking about this, it might not work. Let me know if you try it.


All times are GMT -5. The time now is 12:14 PM.