I have the following bash script:
#!/bin/bash
mysqldump -u ******** -p******** --all-databases | gzip > /home/srvlinux01/MySQLBackups/database_$(date +\%Y-\%m-\%d).sql.gz
which is located in /home/srvlinux01/MySQLBackups/
as backup.sh
with the following permissions
-rwxr--r-- 1 root root 134 feb 27 12:48 backup.sh
I've set up a cronjob on sudo crontab -e
to run it every day, at night
#Automatic MySQL backup
30 3 * * * sh /home/srvlinux01/MySQLBackups/backup.sh
But I get emailed the following error:
sh: 0: Can't open /home/srvlinux01/MySQLBackups/backup.sh
I've been trying different setups, but can't figure out what is wrong. I can run the script manually and everything goes perfectly, so I guess there is something wrong with my cronjob entry, but can't really understand what. Could you please help me figure it out? Thanks!
chown user:user /home/srvlinux01/MySQLBackups/backup.sh
(replace user:user with your username and name of your user group). With that you can normaly run script as regular user. Also, if owner is root, do what @Guru saw, run script with sudo. – Mar 06 '13 at 08:25Still no luck though, wierdly enough it still can't find the file. If I just copy the command and paste it into the terminal it runs normally and makes the backup!
– Mateusz Kapusta Mar 06 '13 at 08:35SHELL=/bin/sh PATH=/bin:/sbin:/usr/bin:/usr/sbin
In your case, try putting:SHELL=/bin/sh PATH=/bin:/sbin:/usr/bin:/usr/sbin:/home/srvlinux01/MySQLBackups/
before your command. – don.joey Mar 06 '13 at 08:47#!/bin/sh
? – don.joey Mar 06 '13 at 08:48backup.sh
have execute permissions? Given that it is world readable, removing the x permissions for group and other is pointless as world could just docat backup.sh | sh
and achieve the same. Apart from, obviously, being able to simply read the username and password ... – zwets Mar 06 '13 at 08:52PATH
? – don.joey Mar 06 '13 at 09:03PATH=/bin:/sbin:/usr/bin:/usr/sbin
already does the trick? Now I'm wondering... I moved the comment to an answer. – don.joey Mar 06 '13 at 09:57