Download zip file from s3 using s3cmd issue - bash

I have two issues I need help with on bash, linux and s3cmd.
First, I'm running into linux permission issue. I am trying to download zip files from a s3 bucket using s3cmd with following command in a bash script.sh:
/usr/bin/s3cmd get s3://<bucketname>/<folder>/zipfilename.tar.gz
I am seeing following error: permission denied.
If I try to run this command manually from command line on a linux machine, it works and downloads the file:
sudo /usr/bin/s3cmd get s3://<bucketname>/<folder>/zipfilename.tar.gz
I really don't want to use sudo in front of the command in the script. How do I get this command to work? Do I need to give chown permission to the script.sh which is actually sitting in a path i.e /foldername/script.sh or how do I get this get command to work?
Two: Once I get this command to work, How do I get it to download from s3 to the linux home dir: ~/ ? Do I have to specifically issue a command in the bash script: cd ~/ before the above download command?
I really appreciate any help and guidance.

First, determine what's failing and the reason, otherwise you won't find the answer.
You can specify the destination in order to avoid permission problems when the script is invoked using a directory that's not writeable by that process
/usr/bin/s3cmd get s3:////zipfilename.tar.gz /path/to/writeable/destination/zipfilename.tar.gz

Fist of all ask 1 question at a time.
For the first one you can simply change the permission with chown like :
chown “usertorunscript” filename
For the second :
If it is users home directory you can just specify it with
~user
as you said but I think writing the whole directory is safer so it will work for more users (if you need to)

Related

Visual Studio Code always asks for sudo permissions

Ok, so I've got a problem with VSCode where every time I try to save a file it asks me for sudo permission and it's really annoying and I can't make folders or files directly in the IDE. Can please someone help me?
Screenshot from VSCode
The running instance of VS Code doesn't seem to have write permission to the file you wish to save.
Since the file is in the directory of user uteu3, first make sure that it is user uteu3 that has started VS Code (it probably is, if uteu3 is the user logged into the system).
make sure uteu3 is the owner of the file and change the permissions by opening a terminal and executing:
sudo chown uteu3 filename && sudo chmod 664 filename
(Replace filename with actual path+name to the file.)
If this doesn't solve the problem, please execute the following two lines in a terminal and provide the output:
ps aux | grep visual-studio-code
stat -f '%A %a %N' filename
Since you indicate that this happens repeatedly also with other files, maybe several of the file permissions in your home directory are problematic. In that case, consider trying to fix them all. See for example: Need to fix file permissions in a user's home directory
.

How to compress root folders (/usr/) from user home directory in linux using script

Im writing shell script to take backup of nagios (/usr/local/nagios) directory. I'm unable to compress it from my home directory because its a root folder. i cannot use sudo as it asks for password. Is there any better way to zip /ussr/local/ files without comprimising security?
Someone please help.
Thank you!
Do it in a cron job owned by root.
Create this script and save it as /etc/cron.daily/nagios-backup:
#!/bin/sh
cd /usr/local
tarball="/home/kart/nagios-$(date +%Y-%m-%d).tgz"
tar -pzcf "$tarball" nagios
chmod 400 "$tarball"
Make sure that script is executable (sudo chmod 755 /etc/cron.daily/nagios-backup) and it'll run every night, dumping that log to a dated tarball file in /home/kart. The tarball will not be readable to you without using sudo (since your account can't normally see the contents and you requested preserving security).

Make shell script change script location

I have currently put together a script to move files from one directory to another.
This has gone ok however I was wondering if there was a way via a shell script to get it to run from anywhere on the server e.g I give the script for someone to use on their server and they can put the script anywhere and it will run.
I know a workaround is to put the script in /usr/local/bin or usr/bin and you can run it from anywhere but that is not what I want.
Is there a way that my script will auto run from usr/local/bin regardless of if it is in /scripts for instance?
Please see my script below:
#!/bin/sh -x
mkdir -p /var/Alitest
echo "This is a test that I have created. This is to
see if the output is successful I normally do this manually but a script is required" > /var/Alitest/action.txt
sed -i 's/This is a test that I have created/The test has been successful/g' /var/Alitest/action.txt
chmod 744 /var/Alitest/action.txt
chown root:root Alitest/action.txt
mv /var/Alitest/action.txt /script/action.txt
Any help would be greatly appreciated :)
Also in my log output for the script the following error is shown:
sed: 1: "/var/Alitest/action.txt": invalid command code A
Any ideas?
You can make a soft link in /usr/local/bin for your script. Then it will be in everyone's path to be executed.
e.g. ln -s /script/yourscript.sh /usr/local/bin/yourscript.sh
After reviewing the matter further I have decided the the best way to action this is to add the folder destination e.g /scripts to my path.
This can be done by vimming into the .bashrc file on the server and adding the below line:
export PATH=/dir_name:$PATH
remember to refresh the profile in order for the changes to take effect.
You can check if this has been successful by running the below command:
echo $PATH
There is no way to get your script to do this however this would be better then a softlink as if you add it to $PATH then you do not have to go through the task of adding softlinks each time.
Thank you all for your help.
Kind Regards
Ali

Jenkins: shell script - create directory access denied (Linux)

I try to execute a shell script during the build in Jenkins but i get an : "access denied" when i try to create a new folder.
I try to launch a php script in the shell script and i get access denied as well
I launched Jenkins from the .war file.
My question is how to give access to the shell script or user that execute to write in a specific folder?
I cannot put the chmod to 777 of this folder (too dangerous).
Easy fix/hack is to change the permissions on the folder Jenkins is trying to write to to 777.
chmod 777 foldername
Long term I would figure out what user Jenkins is running as and either change the folder ownership to that user or add Jenkins to whatever group the folder permissions are a part of.
I finally launched my script through an url with the curl command, and this worked fine.
It's not really a nice solution to the problem but i didn't find any other work around ;)

Cd in shell script not working

First off I am very very new to shell scripting. I am trying to write a script that takes in one parameter and then copies a folder in a different directory naming it using the parameter. This is the current code that I have:
#!/bin/sh
cd /var/www/html/fbplugin/chrome
sudo mkdir temp/$1
sudo cp -rf "/var/www/html/fbplugin/chrome/fbplugin" "/var/www/html/fbplugin/chrome/temp/$1"
When I run this code it says can't cd to /var/www/html/fbplugin/chrome. I'm not sure why it is saying this because I know the directory exists. I have copied the line directly and it works in terminal. If anyone could help me out that would be great.
If it matters in order to run the script I am typing "sh build.sh"
If that directory really exists, then you must have executed that script with a different user (cron, webserver, etc).
Check the rights for that directory.
I don't know why you're getting the error about cd, but it looks like you could just use absolute paths throughout. That would solve the larger problem of the script working correctly.

Resources