php - mysqldump - cron job -
am trying auto backup of our mysql database via cron job runs daily.
we have:
$database_user = 'value'; $database_pass = 'value'; $database_server = 'localhost'; // name of database backup $database_target = 'value'; // database dump $sql_backup_file = $temp_dir . '/sql_backup.sql'; $backup_command = "mysqldump -u" . $database_user . " -p" . $database_pass . " -h " . $database_server . " " . $database_target . " > " . $sql_backup_file; system($backup_command); // end database dump problem message cron daemon with:
usage: mysqldump [options] database [tables]
or mysqldump [options] --databases [options] db1 [db2 db3...]
or mysqldump [options] --all-databases [options]
for more options, use mysqldump --help
sh: -h: command not found
so looks has -h
~~~
anyone have thoughts on how fix?
first, recommend upgrade mysql 5.6+ can keep database passwords more secure. first, follow instructions this stackoverflow answer set more-secure mysql login method command line scripts.
you should write bash script instead of using php. here's full backup script, simple. db_name name of database, , /path/to/backup_folder/ want store backups. --login-path=local switch in home directory of whoever running bash script , see if there login file there (must readable current user , accessible no 1 else).
#!/bin/bash #$now provide timestamp in filename backup now=$(date +"%y%m%d-%h%m%s") db=/path/to/backup_folder/"$now"_db_name.sql.gz mysqldump --login-path=local db_name | gzip > "$db" #you change permissions on created file if want chmod 640 "$db" i save file db_backup.sh inside of /usr/local/bin/ folder , make sure readable/executable user going doing db backups. can run # db_backup.sh anywhere on system , work.
to make cron, put file called 'db_backup' (the name doesn't matter) inside /etc/crond.d/ folder looks (user_name user supposed run backup script)
shell=/bin/bash path=/sbin:/bin:/usr/sbin:/usr/bin:usr/local/bin mailto=root home=/ # "user_name" try run "db-backup.sh" every day @ 3am 0 3 * * * user_name db-backup.sh
Comments
Post a Comment