-6

Hi automating some task to change the user from local to sudo su root and connect to ftp server and get the .gz file from ftp to local machine gunzip that file and restore into PostgreSQL BD, but i'm not able to change the user

some sample script that i used in script

1.spawn sudo -s <

expect "password for username:"

send -- "production123\r"

expect eof

error:-

-bash: ./dbbackup.sh: /usr/bin: bad interpreter: Permission denied

  1. sudo -kSs << EOF

production123

whoami

echo "Not a good idea to have a password encoded in plain text"

EOF

error :its also not switching the user

3.sudo -s <

expect "password for username:"

send -- "production123\r"

expect eof

error

[production@nsuat crons]$ ./dbbackup.sh ./dbbackup.sh: line 1: EOF: No such file or directory ./dbbackup.sh: line 3: expect: command not found ./dbbackup.sh: line 5: send: command not found ./dbbackup.sh: line 7: expect: command not found [production@nsuat crons]$

  • 1
    Could you please try to edit your question so that it is a bit more understandable? I mean, your question seems to boil down to how to be able to use sudo from a script, but there's plenty of other stuff also which blurs the context... – zagrimsan Dec 17 '15 at 8:24
0

The question is a bit hard to understand. But, if I may, I could offer an alternative approach. Make a specific user that runs the backup, and give it passwordless sudo rights. Now set up the cron job (or whatnot you use) to run as that user, and have the backup script elevate (sudo) it's rights where needed.

Alternatively, depending on how you Postgres is set up, you could just have the postgres user be the one running the restore.

  • Hi @Anders Olsson, after DBbackup server keep the .gz file with owner/group root on ftp server, i have a local machine with owner/group production,to take the backup and restore in Postgres we use local server when i open putty i give password production123 and then i changes to super user sudo su root which ask for password so while doing by script i cant able to changes the user .i use above script but its not working, if i'm doing same task with owner production while using restore command su postgres -c "psql -d Dhruva-test -f daily-backup-16-12" its ask for password – raj verma Dec 18 '15 at 10:32
  • Are you login in as a user production to through SSH, then using su to root, then using su again to user postgres? Are there any intermediate steps there, or? You can use sudo as any user you want, with the -u flag. – Anders Olsson Dec 18 '15 at 14:59
  • Hi @Anders Olsson Ya ,after login to SSH default in production then i changed to sudo su root to get the .gz file from FTP and Then i do gunzip after gunzip i restore it into DB. but i'm trying to do by script but i'm not able to changes the user. – raj verma Dec 22 '15 at 6:35
  • So, if you had passwordless sudo set up for the user, you could simply use sudo for the commands which need root access, and sudo -u postgres for the restore command. Please tell me if this is not an option, and you only want help with expect. – Anders Olsson Dec 22 '15 at 6:56
0

Try this; it works for me:

echo "[Password]" | su [UserName] -c [Command/Script to be executed]

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.