You are not logged in.

#1 2013-12-14 02:18:20

wolfdogg
Member
From: Portland, OR, USA
Registered: 2011-05-21
Posts: 545

cron.daily script not working

i have a script to run mysqldump that i found somewhere, i thought it was found under either the LAMP wiki, or the mysql wiki, but im not finding it when looking again.

i am not well versed in this one so i threw a script inside cron.daily so i didnt hvae to yet edit crontab. 

#!/bin/bash

#THISDIR=$(dirname $(readlink -f "$0"))
NOW=$(date +"%Y-%m%d")
PATH="/srv/backup/"$NOW"_db_backup.gz"

mysqldump --single-transaction --flush-logs --master-data=2 --all-databases \
 | gzip > $PATH

echo 'purge master logs before date_sub(now(), interval 7 day);' | mysql

i was getting

command not found

for gzip, mysql, and mysqldump.

i added the full paths to the commands to squelch some errors

#!/bin/bash

#THISDIR=$(dirname $(readlink -f "$0"))
NOW=$(date +"%Y-%m%d")
PATH="/srv/backup/"$NOW"_db_backup.gz"

/usr/bin/mysqldump --single-transaction --flush-logs --master-data=2 --all-databases \
 | /usr/bin/gzip > $PATH

echo 'purge master logs before date_sub(now(), interval 7 day);' | /usr/bin/mysql

, and now im getting more errors.  i am running the command directly from the directory its in, will this even work?

# ll
total 24
drwxr-xr-x  2 root root 4096 Dec 13 12:24 .
drwxr-xr-x 53 root root 4096 Dec 13 07:39 ..
-rwxr--r--  1 root root  668 Oct 10 05:57 logrotate
-rwxr--r--  1 root root  806 Jun 27 06:22 man-db
-rwxr-xr--  1 root root  311 Dec 13 12:18 mysqldump
-rwxr--r--  1 root root   94 Oct 21 07:33 shadow
# ./mysqldump
mysqldump: Got error: 1045: "Access denied for user 'root'@'localhost' (using password: NO)" when trying to connect
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

furthermore, there is a couple empty non working .gz files inside my backup directory, so its doing something when ran as an actual cron job, its just not dumping the data,

permissions on the /srv/backup directory are as follows

drwxrwx---  2 root dev 4096 Dec 13 10:03 backup

Edit:
crap on me, it looks like now that i added the full paths, its overcoming the command not found error, OBVIOUSLY, but what i didnt realize is the error coming back is not a file system permission error, its a mysql error, DOH.  so looks like i might just have to correct the command.  what can do i about not having to add the password in this bash script?  or do i need to.  if so, on what part of the command?

Last edited by wolfdogg (2013-12-14 02:25:15)


Node.js, PHP Software Architect and Engineer (Full-Stack/DevOps)
GitHub  | LinkedIn

Offline

#2 2013-12-14 02:58:02

wolfdogg
Member
From: Portland, OR, USA
Registered: 2011-05-21
Posts: 545

Re: cron.daily script not working

Ok, im open to better suggestions, i.e. if theres something more secure, but i made a solution
i made a new user called cron in mysql, and gave it basic privileges (LOCK TABLES, RELOAD, SELECT, SUPER) only....   then in my script i had to add --password and --user in both places
see below

#!/bin/bash

#THISDIR=$(dirname $(readlink -f "$0"))
NOW=$(date +"%Y-%m%d")
PATH="/srv/backup/"$NOW"_db_backup.gz"

/usr/bin/mysqldump --user=cron --password=mypass --single-transaction --flush-logs --master-data=2 --all-databases | /usr/bin/gzip > $PATH

echo 'purge master logs before date_sub(now(), interval 7 day);' | /usr/bin/mysql --user=cron --password=mypass

does this seem fair?  or does privs like SUPER or any others mentioned open a bunch of holes?


Node.js, PHP Software Architect and Engineer (Full-Stack/DevOps)
GitHub  | LinkedIn

Offline

#3 2013-12-14 14:24:49

ralvez
Member
From: Canada
Registered: 2005-12-06
Posts: 1,730
Website

Re: cron.daily script not working

I think it may be a bit more secure if you use visudo.
You set the script to be run by the cron user you created but with no password, eg:
cron ALL=NOPASSWD: /path/to/your/script/cron

That way you do not have to embed the password in the script as you did.

R.

Offline

Board footer

Powered by FluxBB