Saturday, September 3, 2022

[SOLVED] Permissions for a Bash Script

Issue

I have a script, located in /home/domain/cron/backup.sh

I basically want it to be CRON job that backs up the mysql database using mysqldump. The command works find when I am logged in to the SSH, however when I want to run the file from the cron job it comes back permission denied.

The folder I want to dump to is called 'dbbackup'. If I run ls -al on this folder I get:

drwxrwxrwx  2 root root 4096 Mar 16 14:07 ./
drwx--x--x 14 gf gf 4096 Mar 13 14:47 ../

gf is another user on the system, and is the user that the CRON script runs as.

When I run the CRON job, I get the following response:

database2015-16-03-2015.sql: Permission denied

I can only log in as a user called graphite to the SSH, the user gf does not have shell access so I cannot log in as them.

The mysqldump command is:

mysqldump db_2015 > /home/gf/dbbackup/database2015-$(date +%d-%m-%Y)$

I dont know what to do :(

EDIT For future reference and anyone coming across the same frustration i was having, I found the solution, sort of.

Originally I was trying to set the cron job through the control panel, under the user gf (which is not one that has SSH access and therefore I cannot log into the command prompt with). But of course this user does not have permissions to run this backup.sh file.

So I started thinking well the user that I created the backup.sh file with, graphite, maybe I can run the cron job as him. So I started looking in to crontab, and still getting sudo errors (tty not found) and things. Then I went back to my mysqldump command in the backup.sh file, and instead of starting that command with sudo, I ran it as normal, and placed the -u <username> and --password=<password> In there, and it works perfectly. I've learnt a bit more about permissions and my database now backs up on a daily basis. Huzzah!


Solution

the problem is this :

drwx--x--x 14 gf gf 4096 Mar 13 14:47 ../

as you see gf can read, write and excecute, however other users can only excecute.

Note this is a folder. thus if its not readable the files inside can not be accessed.

what you need to do is to ensure this folder and every other parent folder is readable. something like :

chmod 755 <folder name>

so you'll get:

drwxr-xr-x 14 gf gf 4096 Mar 13 14:47 ../

NOTE if you are not root or gf users. you won't be able to do any of those things.



Answered By - nafas
Answer Checked By - Pedro (WPSolving Volunteer)