Jay Taylor's notes
back to listing indexsecurity - Running mysql dump in a cron job without exposing passwords - Super User
[web search]-
Teams
Ask questions, find answers and collaborate at work with Stack Overflow for Teams.
Try Teams for free Explore Teams -
Looking for
I want to run
mysqldump -u aUser -p P4SSw0rd --all-databases > backup.sql
in a cron job. How can I do so securely?
I know I could put the command right there, but anyone with access to the machine would see it straight away via crontab. Is there a better way to do it?
-
Your example is incorrect, it should be -pP4SSw0rd with no space. Commented Jul 7, 2016 at 2:58
4 Answers
As stated in man mysqldump
: see 6.1.2.1. End-User Guidelines for Password Security in the MySQL reference manual.
An option file is the safest bet, not least according to the above reference. Giving it in plaintext in crontab is not good, not least since the process command line by default is visible through ps
for other users. The same actually applies for environment variables as explained in the reference.
Relevant portion of the MySQL reference manual:
Store your password in an option file. For example, on Unix, you can list your password in the
[client]
section of the.my.cnf
file in your home directory:[client] password=your_pass
To keep the password safe, the file should not be accessible to anyone but yourself. To ensure this, set the file access mode to
400
or600
. For example:shell> chmod 600 .my.cnf
To name from the command line a specific option file containing the password, use the
--defaults-file=file_name
option, wherefile_name
is the full path name to the file. For example:shell> mysql --defaults-file=/home/francis/mysql-opts
Section 4.2.3.3, “Using Option Files”, discusses option files in more detail.
Also see https://stackoverflow.com/q/10725209.
-
It seems that ps command obfuscate the password with one x: ps: mysqldump -uroot -px xx mydb. I'm not saying that it's a good protection though (if you type jobs, then the password is revealed in plain text).– lingCommented Sep 21, 2015 at 5:41
Run the cronjob as a specific user and use some simple Bash logic to extract the password from a plaintext file stored somewhere on the system with permissions that only allow the user (or perhaps group) to access it.
PASS=`cat /path/to/pwdfile`
mysqldump -u aUser -p $PASS--all-databases > backup.sql
So if the cronjob runs as user 'example', the ownership of the file should be "example:example" and permissioned 0400.
You can also achieve a similar function using a user-level .my.cnf.
-
read PASS < /path/to/pwdfile
is a idiomatically cleaner to do the same thing (arguably I guess; superuser.com/q/323060/49184 applies). Commented Oct 21, 2012 at 10:52 -
-
Someone with even the most basic understanding of Bash should be able to see what's going on with that cat. :)– GarrettCommented Oct 21, 2012 at 16:38
-
True, I would even say it is the most common way to do it, but it still a bit of an eyesore :-) . If one makes UUOC a habit it will bite when the file is bigger than
ARGMAX
, it takes an extra process instead of using a shell built-in, it might tempt constructs such asfor i in `cat file`; do ...
with its own array of problems, etc. But of course, as with most things: if one knows what is happening one is free to do as one chooses. I'm not on a crusade here, regardless of how it might appear :-D . Commented Oct 21, 2012 at 17:35
For backup purposes, consider having a read-only user in mysql, like so
CREATE USER bUser IDENTIFIED BY 'p4ss';
GRANT SELECT ON *.* TO bUser@localhost;
GRANT LOCK TABLES ON *.* TO bUser@localhost;
mysqldump requires only SELECT
and LOCK TABLES
privileges to do its job.
Anyone with access to the machine has the same level of access to /var/spool/cron/crontabs/
as to /var/lib/mysql
you allow them to have. So, set the proper permissions on the directories and done. Anyone with root-access has direct access to the database files directly. Anyone you do not trust to have access to the machine should not have access at all to the machine.
Usually folks only see their own cronjobs via crontab -l
.
Your Answer
- The Overflow Blog
-
-
- Hot Meta Posts
-
6
-
8
Related
Hot Network Questions
- How is it possible that the sum of first n perfect nth powers is the current year?
- Book about the nature of death
- Help with a complicated AnyDice ability score calculation
- Is it okay to not like some team members in a team?
- Create a sequence of numbers in boxes
- What is the legal status of people from Unites States overseas territories?
- Color Selector Combobox Design in C#
- Why is Bilbo Baggins called Bilbo Beutlin in Der Hobbit?
- Wrong Passport Expiration Date on Canadian ETA
- Recursive auto-nesting of math decorations (e.g., hat bar x -> hat{bar{x}})
- Is 13 minutes enough time to change platforms in Brussels-Midi after arriving from London?
- How can I remove shower surround adhesive on ceramic tile?
- Can a Son of Kyuss regenerate a new Son from a severed limb?
- Rings where each left principal ideal is also a right principal ideal
- Linear version of std::bit_ceil that computes the smallest power of 2 that is no smaller than the input integer
- What are the use cases and challenges for a cubesat that would take pictures of other satellites?
- What's the most succinct way to say that someone feels the desire to do something but is unwilling to ever do so?
- What English expression or idiom is similar to the Aramaic "my heart revealed it"?
- Number of legal positions in 1D go
- Use of DeleteCases to Level Infinity
- Was the Tantive IV filming model bigger than the Star Destroyer model?
- Undefined consequent in logical implication
- Changing all strcpy to strscpy
- Implications of Goldbach's prime number conjecture