I have a Jenkins installation and running mysql server on the same box. One of my jenkins jobs is supposed to tar up the /var/lib/mysql folder but I get the following error

tar: /var/lib/mysql/mysql: Cannot open: Permission denied

I have added the jenkins user to the mysql group but this has not done anything for me.

Does anyone have any ideas?

Thank you for your help

  • Did you stop the database first? – Matt Mar 17 '14 at 8:07
  • I did not. I will try that and let you know how it goes. Thank you – Paul Sheldrake Mar 17 '14 at 8:08
  • I have tried the jenkins job with the database stopped and still received the same issue – Paul Sheldrake Mar 17 '14 at 8:25
  • Have you checked that the database files and /var/lib/mysql directory are group-readable? – Tero Kilkanen Mar 17 '14 at 8:36
  • @TeroKilkanen Thank you for your comment. Changing the permissions on the folders/files have fixed the issue. – Paul Sheldrake Mar 17 '14 at 8:55

From memory the mysql database directory is often set with user permissions only. Group permissions are 0. i.e. 0700 or similar.

Doing this backup the way you are doing it could lead to corruption if data is written while you are running tar. So rather than use tar directly, use mysqldump to back up the whole database. Add a jenkins user to mysql that has permissions to run the backup for all of the databases. Then gzip the resulting file. I'd create a script to do this and call it from Jenkins.

  • Thank you for your answer, we actually have a process to produce a more conventional sql dump. This is approach I'm working on here is about experimenting. Cheers – Paul Sheldrake Mar 17 '14 at 8:58

Running these commands has allowed my to tar the folder

find /var/lib/mysql -type d -exec sudo chmod 2775 {} +
find /var/lib/mysql -type f -exec sudo chmod 0664 {} +
  • But that opens up your database for potentially other users on the system to view more than necessary. Use 2770 & 0660 instead. And if you're going to just tar it up make sure you stop mysqld first. – Matt Mar 18 '14 at 0:31

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.