8

I have a script called backup.sh:

#!/bin/bash
sudo zip -r /home/jazuly/backup.zip /var/lib/automysqlbackup/
cd /home/jazuly/backupscript/cp2google/
php cp2google.php /home/jazuly/backup.zip
cd ~
rm -f /home/jazuly/backup.zip

I made it executable:

sudo chmod +x backup.sh

Then I tried to edit cronwith crontab -e.

I add:

# m h dom mon dow command
0 0 * * * /home/jazuly/backup.sh

but when cron sends the backup.zip to my email, I download and open it, and the file is corrupt.

If I run the script manually with:

$ sudo chmod +x backup.sh 
$ ./backup.sh

I can open the zip file.

Jazuly
  • 203
  • what message you get when fie corrupted ? – John Joe Jun 15 '17 at 08:30
  • 2
    Are you sure the script works? You create /home/jazuly/backup.zip as root (sudo) and then try to delete it as a regular user (rm -f /home/jazuly/backup.zip), that doesn't make sense. – terdon Jun 15 '17 at 08:46
  • if i run manual without cron is work... then, what should i do? – Jazuly Jun 15 '17 at 08:51
  • @JohnJoe unkown format or demaged and size is 0 – Jazuly Jun 15 '17 at 08:52
  • Show whereis -b zip – zombic Jun 15 '17 at 08:56
  • zip: /usr/bin/zip – Jazuly Jun 15 '17 at 09:05
  • in your bash script try adding a zip file test zip -T before sending to php script. and again echo zip file size from php script. cron should report output. Debug your problem in this way. Figure out at what point it breaks.My guess is it's your php script or way its called from bash. Or not giving php binary a full path when called from cron. Use full paths – B. Shea Jun 16 '17 at 04:07

3 Answers3

6

You will need to use the full path in any cron executed script. So, don't do cd ~, give instead cd /home/jazuly

For further debugging, you can also redirect the output of the cron script to a file, with /home/jazuly/backup.sh 1> /home/jazuly/log.txt 2> /home/jazuly/err.txt

So the whole command:

# m h dom mon dow command
0 0 * * * /bin/bash /home/jazuly/backup.sh 1> /home/jazuly/log.txt 2> /home/jazuly/err.txt
seanlano
  • 2,986
  • 3
  • 28
  • 44
  • i try it but crone not send me anything. im already delete that path change with rm -f /home/jazuly/backup.zip – Jazuly Jun 15 '17 at 07:24
  • Just updated the answer about the path - don't use relative paths, try cd /home/jazuly in your script – seanlano Jun 15 '17 at 07:25
  • Also the php line, use the full path there too. – seanlano Jun 15 '17 at 07:26
  • how to run php wit full path? i try php /home/jazuly/backupscript/cp2google/cp2google.php /home/jazuly/backup.zip i got error – Jazuly Jun 15 '17 at 07:30
  • Still no luck? Does your cp2google.php give any output? You can capture the output with a line like /home/jazuly/backup.sh 1> /home/jazuly/log.txt 2> /home/jazuly/err.txt. This will send the output to log.txt and any stderr messages to err.txt. After you run the cron job, you should be able to see if any errors are printed. – seanlano Jun 15 '17 at 07:38
  • here is the error i get https://gist.github.com/jazuly/8bf87fe34d74737bc340730c0089c534 – Jazuly Jun 15 '17 at 07:41
  • Looks like there are some missing files there - backupdatabaseterbaru-c771cd4f4fcf.p12 seems to be not found. I'm not familiar with this PHP script, where is the location of this file? – seanlano Jun 15 '17 at 07:45
  • /home/jazuly/backupscript/cp2google – Jazuly Jun 15 '17 at 07:47
  • Location of this file /home/jazuly/backupscript/cp2google/cp2google.php is correct? – zombic Jun 15 '17 at 07:50
  • And try chown jazuly.jazuly /home/jazuly/backup.zip Why do you use sudo zip? – zombic Jun 15 '17 at 07:52
  • Sorry, I'm not sure if that's correct, I'm not familiar with the PHP program to be able to know how it works. This question has some more info on Cron errors: https://askubuntu.com/questions/350861 Maybe it will help. – seanlano Jun 15 '17 at 07:53
  • @zombic then? run php /home/jazuly/backupscript/cp2google/cp2google.php /home/jazuly/backup.zip ? – Jazuly Jun 15 '17 at 07:55
  • Yes, then run cp2google.php. – zombic Jun 15 '17 at 08:02
  • @zombic still get same error – Jazuly Jun 15 '17 at 08:04
  • i run sudo php /home/jazuly/backupscript/cp2google/cp2google.php /home/jazuly/backup.zip still get same error – Jazuly Jun 15 '17 at 08:05
  • 3
    The extensions are irrelevant. The point you make in the comments about using the absolute path is helpful, but your answer is just wrong: cron doesn't care about extensions at all. – terdon Jun 15 '17 at 08:47
  • Fair enough, I had thought that through testing previously I'd found the extension made cron go bad - but that may have been multiple factors during that testing and I'd erroneously blamed it on the extension. I'll edit the answer to avoid if being confusing for future readers. – seanlano Jun 16 '17 at 00:11
4

Try to use full paths

#!/bin/bash
/usr/bin/zip -r /home/jazuly/backup.zip /var/lib/automysqlbackup/
/usr/bin/php /home/jazuly/backupscript/cp2google/cp2google.php /home/jazuly/backup.zip
rm -f /home/jazuly/backup.zip

And add /bin/bash in cron

# m h dom mon dow command
0 0 * * * /bin/bash /home/jazuly/backup.sh

And check permissions for files backup.zip, backupdatabaseterbaru-c771cd4f4fcf.p12

zombic
  • 181
1

Here are the steps of how I fixed it:

  1. Change Permission & Owner of var/lib/automysqlbackup to 777 & jazuly.jazuly.
  2. Move all the folders & files from backupscript/cp2google/ to home/jazuly.
  3. Use wait for every statement.
  4. My final code:

    #!/bin/bash
    zip -r backup.zip /var/lib/automysqlbackup/
    wait
    php cp2google.php backup.zip
    wait
    rm -f backup.zip
    

    And my cron:

    0 0 * * * /home/jazuly/backup.sh
    

    to backup every midnight/day.

    I don't think there is a need to write the full path if the .sh file is in the same path with what you want to execute.

Jazuly
  • 203