Jay Taylor's notes

back to listing index

security - Running mysql dump in a cron job without exposing passwords - Super User

[web search]
Original source (superuser.com)
Tags: mysql howto backups mysqldump superuser.com
Clipped on: 2025-01-23

    1. Home
    2. Questions
    3. Tags
    4. Saves
    5. Users
    6. Jobs
      New
    7. Companies
    8. Unanswered
  1. Teams

    Ask questions, find answers and collaborate at work with Stack Overflow for Teams.

    Try Teams for free Explore Teams
  2. Looking for

Asked 12 years, 3 months ago
Viewed 18k times
14

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?

akira
63k1818 gold badges140140 silver badges165165 bronze badges
asked Oct 21, 2012 at 7:06
user166707

4 Answers

Sorted by:
15

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 or 600. 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, where file_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.

answered Oct 21, 2012 at 11:02
Daniel Andersson
24.8k55 gold badges6060 silver badges6161 bronze badges
  • 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).
    – ling
    Commented Sep 21, 2015 at 5:41
4

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.

answered Oct 21, 2012 at 7:20
Garrett
4,20711 gold badge2424 silver badges3333 bronze badges
0

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.

answered Dec 7, 2016 at 23:04
Saptarshi Biswas
10344 bronze badges
0

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.

answered Oct 21, 2012 at 7:19
akira
63k1818 gold badges140140 silver badges165165 bronze badges

Your Answer

Community wiki

Hot Network Questions