Permission denied when attempting to mv (sudo su userName)
Looks like the error is about having no permission to change directory.
- verify that the user has at least
r-x
on each folder leading down to the path from where the script is executed. - verify that the user has at least
rwx
on the folder + files where the backups are located. - verify whether the user has a login shell,
$HOME
and$PATH
. If yes, make the scriptcd
to some relevant location before starting operation. If not, define all commands and locations using absolute paths.
I cannot come up with any other possible causes.
Related videos on Youtube
Jeremy
Updated on September 18, 2022Comments
-
Jeremy almost 2 years
I am currently working on a backup script to log into my production server and pull the latest SQL dumps to my staging server. I have no issue pulling the SQL dumps but I do have an issue with moving the last backup files to a temporary folder.
My backup script user:
mysqlBackupUser
I am using this user's home directory as the location for this script and to store the backups. The folder structure is:
/home |- mysqlBackupUser |- .ssh |- backups |- bin
My script sits in
bin
and the backups are downloaded into thebackups
directoryThe
backups
directory also has other folders to organize the server and type of the backups. I.E.:/backups |-mysql |- production1 |- production2 |-misc |- production1 |- production2
So far everything is fine and works as expected. The issue is when I am in (not limited to):
/home/mysqlBackupUser/backups/mysql/production1/
when I create the tmp directory named
_lastBackUp
and try to move my previous backups into it I get:find: Failed to change directory: Permission denied
My code for this is:
local tmpBackUpDir="${LOCAL_LOCATION_DIR}/_lastBackUp/" # Ensure the directory exist mkdir -p "${tmpBackUpDir}" # move any previous files to bak find "${LOCAL_LOCATION_DIR}/" -type f -exec mv -t "${LOCAL_BAKUP_DIR}" {} \+;;
For clarity:
LOCAL_LOCATION_DIR = /home/mysqlBackupUser/backups/mysql/production1 tmpBackUpDir = /home/mysqlBackupUser/backups/mysql/production1/_lastBackUp/
To understand how the script is being used: Currently I am in as my user and I am
sudo su mysqlBackupUser
To be as the user. I am not sure if this is the issue and I cannot test by logging in as this user. The script will be run by cron and it will act as this user (keys are setup to ease the automation for this user) to do the process so I am not certain this is really the issue.
I have verified that the ownership and permission of all folders, even the
_lastBackUp
is owned bymysqlBackupUser
. I even set_lastBackUp
to 777 for testing and still got permissions denied...My complete error is:
find: Failed to change directory: Permission denied find: Failed to change directory: Permission denied find: Failed to change directory: Permission denied find: Failed to change directory: Permission denied find: Failed to change directory: Permission denied find: Failed to change directory: Permission denied find: Failed to change directory: Permission denied find: Failed to change directory: Permission denied find: Failed to change directory: Permission denied find: failed to restore initial working directory: Permission denied
Each
find: Failed to change directory: Permission denied
is for each file I am trying to move to_lastBackUp
Thanks for any insight into this.
-
Jeremy almost 10 yearsI get:
drwxrwxrwx 2 mysqlBackupUser mysqlBackupUser 4096 Jul 21 16:24 _lastBackUp
-
Jeremy almost 10 yearsYour last option worked (cd to relevant location)! I was in as my user initially and when I
sudo su mysqlBackupUser
it appears it kept the user in my original directory. It's weird that it would download the files where they belong and the only issue was with creating the tmp directory, especially since I am using absolute pathing. Either way it works now thanks to your last suggestion! thanks! -
woohooyeah almost 10 yearsCool, I'm glad it worked out.