cron job not working - what am i doing wrong?

Joined
12 Feb 2006
Posts
17,225
Location
Surrey
i've never used cron before so unsure what i'm doing wrong, but unfortunately the script i've got from online doesn't work.

below is the script i have which is save as 'mysql_autobackup.sh'

Code:
#!/bin/sh
#change directory to your backup directory
cd /home/websiteNameFolder/db_backup/;
#get backup of database of applications
mysqldump --user='[removed username]' --password='[removed password]' mydb >databaseName.sql;
#compress it in zip file
zip app_database-$(date +%Y-%m-%d-%H:%M).sql.zip tmp_db.sql;
#remove  sql file
rm -rf tmp_db.sql;
#delete backups older than 20 days
find /home/websiteNameFolder/db_backup/app* -mtime +30 -type f -delete;


i've edited out some bits.

then on cronjobs i have the following to run sunday at 4am using the following.

Code:
0    4    *    *    0

Code:
/home/websiteNameFolder/db_backup/mysql_autobackup.sh

but nothing happens. i checked the folder and no backup there. is there a way to see why it failed?

one thing i'm not sure on if where it says "mydb >" i assumed this was my database name so put that there, but then it has the .sql so wasn't convinced i was correct.

thanks
 
Associate
Joined
19 Nov 2021
Posts
954
Location
Portsmouth
Why do you have semicolons after your lines in the shell script?
Have you set the script to be executable?
Which user's cron is it in?

mydb must be your database name
the bit after the > is the output file name - where the backup gets dumped to.
in the zip command, replace tmp_db.sql with the file name from the command above.
 
Last edited:
Soldato
Joined
3 Jun 2005
Posts
3,067
Location
The South
Using CLI/SSH'ing into the server, make sure you're making the bash script executable, as @Sinbad2000 says, and that you can execute it -
Code:
chmod +x {FILENAME.sh}
./{FILENAME.sh}

And then test the cron job by setting it to run every 5 minutes or so, 5 * * * * (Cron tab guru website) and check your directory. Once it works, then set it to the correct schedule.

And to save a line, you can pipe straight to Zip/GZip or similar by doing -
Code:
mysqldump --no-tablespaces --user={USERNAME} --password={PASSWORD} --databases {SPACE DELIMITED DB NAMES} | gzip -9 > {GZipped DB.sql.gz}
 
Soldato
Joined
18 May 2010
Posts
22,376
Location
London
You could also try running it manually to see if it works.

sudo ./mysql_autobackup.sh

Try running the individual commands individually as well to see if they work.

Also maybe you will find logs in /var/log/*.

Another thing I would change is #!/bin/sh to this #!/bin/bash.
 
Last edited:
Soldato
Joined
21 Jun 2004
Posts
2,789
Location
Berkshire
Did you get this working? As mentioned you should have all the cronjob executions being logged in /var/log/messages or /var/log/syslog depending on the os/distro.

Errors should get logged there too, otherwise you may want to do something like this:
/home/websiteNameFolder/db_backup/mysql_autobackup.sh &> /path/to/log.txt

This ensures standard out and standard error is saved into that log.txt file to check later.

Another thing that's tripped me up is the default value for $PATH, which defines where binaries are located. This can often be defined in your shell when you login, but has no effect on cronjobs as they are not logging in interactively. Instead you can specify PATH in your script, or use the full path to the binaries you are using e.g. /usr/bin/mysqldump instead of just mysqldump.
 
Back
Top Bottom