I have a script called backup.sh:

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.

  • what message you get when fie corrupted ? – John Joe Jun 15 '17 at 8: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 8:46
  • if i run manual without cron is work... then, what should i do? – Jazuly Jun 15 '17 at 8:51
  • @JohnJoe unkown format or demaged and size is 0 – Jazuly Jun 15 '17 at 8:52
  • Show whereis -b zip – zombic Jun 15 '17 at 8:56

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
  • 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 7: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 7:25
  • Also the php line, use the full path there too. – seanlano Jun 15 '17 at 7: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 7:30
  • 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 8:47

Try to use full paths

/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

  • from backupscript/sub directory all 777 – Jazuly Jun 15 '17 at 9:42
  • Command ls -l will show you files owner. – zombic Jun 15 '17 at 10:12
  • root root, root root – Jazuly Jun 15 '17 at 10:27
  • Run chown jazuly.jazuly file_name or chown -R jazuly.jazuly directory for all files and directories with owner root. – zombic Jun 15 '17 at 10:32

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:

    zip -r backup.zip /var/lib/automysqlbackup/
    php cp2google.php backup.zip
    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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.