Skip to main content

Thread: su as backup user inside of bash script


hello,

attempting write backup script following:

1) lock , flush tables on mysql db
2) dump db file
3) unlock tables
4) rsync file offsite storage

seems going well. however, don't want setup ssh storage server on network root user without password.

attempting su backup user inside of script
when try run script happens should until try so.. jumps out of script .. akss me login backup user.. proceeds rsync offsite storage

, resumes execiting script.

not going setup cron job. executed manually. assuming case, how can script run without prompting password?

here i've come far...

assuming script run root , identity of backup user need assumed inside script without perstering user enter backup user's password.

code:
  echo ' ' echo "database backup script beginning" echo ' ' /usr/bin/mysql -uroot -psecretpass -e 'use the_db; flush tables read lock;' echo "attempting flush tables on test_db read lock" echo ' ' sleep 5 if [ $? == 0 ];   echo "flushing tables on the_db read lock - success" else   echo "error on flush tables read lock on the_db" fi echo ' '  echo "attempting dump database the_db file: backup-`date +%m-%d-%y`.sql" echo ' ' /usr/bin/mysqldump -uroot -pseccretpass testdb > /home/test-bak/backupdb/backup-`date +%m-%d-%y`.sql sleep 5 if [ $? == 0 ];    echo "mysql dump of the_db backup-`date +%m-%d-%y`.sql success" else    echo "mysql dump of the_db fail" fi echo ' '  echo "attempting unlock tables on the_db" echo ' ' /usr/bin/mysql -uroot -psecretpass -e 'unlock tables' if [ $? == 0 ];  echo "unlock tables on the_db success" else   echo "unlock tables on the_db fail" fi echo ' '  echo "attempting su backup user" /bin/su test-bak if [ $? == 0 ];   echo "su backup user go!" else   echo "su backup user fail" fi echo ' '   echo "attempting rsync backup offsite storage" /usr/bin/rsync -avz -e ssh -i /home/test-bak/.ssh/id_rsa.pub /home/test-bak/backupdb/*  test-bak@backup:/home/test-bak/backupdb if [ $? == 0 ];   echo "rsync test_db backup file: backup-`date +%m-%d-%y`.sql offsite storage success" else   echo "rsync test+d backup file: backup-`date +%m-%d-%y`.sql offsite storage fail" fi echo ' '   echo "*******" echo ' ' echo "done , done"

the problem you're executing "su" without special parameters, *thinks* want execute shell user.

need doing using "-c" option of "su" tell run specific command user. using info script, think want:
code:
# switching user "test-bak", , executing 'rsync' command  /bin/su -c "/usr/bin/rsync -avz -e ssh -i /home/test-bak/.ssh/id_rsa.pub /home/test-bak/backupdb/*  test-bak@backup:/home/test-bak/backupdb" test-bak
note: return code return code 'su', not return code 'rsync' command.

lloyd b.


Forum The Ubuntu Forum Community Ubuntu Specialised Support Ubuntu Servers, Cloud and Juju Server Platforms [ubuntu] su as backup user inside of bash script


Ubuntu

Comments

Popular posts from this blog

Flip address is out of range arduino uno r3

Arduino Uno not uploading

Indesign and MathType fonts