Hi All,

Im having trouble getting a crob job to run.

What i have so far is

/home/ mysqldump -h -u root -ppassword database name | gzip > /home/echou/

any assistance would be appreciated

Dani AI

Generated

Testing the command interactively (as suggested) is the right first step — that proves the dump works outside cron. marked the original problem resolved, but the checklist and safe workflow below will help avoid intermittent failures and security issues when the job runs unattended.

Common gotchas to check first:

  • Run the exact command in a shell as the same user whose crontab will run it (root vs regular user).
  • Use absolute paths for every binary and file (cron runs with a minimal PATH). See crontab(5) for details (crontab manual).
  • Check cron logs and mail for error output (look in /var/log/cron or /var/log/syslog depending on distro; see the distro cron HOWTO for guidance: CronHowto).
  • Remember to escape percent signs in crontab entries (they are treated specially).

Safer dumps and security tips:

  • Never put the DB password on the command line; use an option file (e.g., ~/.my.cnf or a --defaults-extra-file) with permissions 600. MySQL documents option files and client behavior here: MySQL option files.
  • Prefer a dedicated, low-privilege MySQL user for backups. For InnoDB-heavy databases, use --single-transaction to avoid long locks (see the mysqldump docs: mysqldump).

Put the dump in a small script with absolute paths, log its output, and call that script from cron. Example skeleton:

#!/bin/sh
set -e
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
BACKUP_DIR=/root/backups
TIMESTAMP=$(date +%F-%H%M)
mkdir -p "$BACKUP_DIR"
/usr/bin/mysqldump --defaults-extra-file=/root/.my.cnf my_database | /bin/gzip > "$BACKUP_DIR/my_database-$TIMESTAMP.sql.gz"

Then call it from cron (example, daily at 02:00):

0 2 * * * /root/bin/mysql-backup.sh >> /var/log/mysql-backup.log 2>&1

If a scheduled job still fails, capture the script output, inspect cron logs, and confirm the cron service is running.

Recommended Answers

All 2 Replies

Does that command run in your terminal? If it does then all you need to do is to to edit crontab and add the format to schedule the backup. How often do you want to run the command?
See here for the format to use for scheduling tasks in crontab

sorted thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.