LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   ssh wrinkle in backup script (http://www.linuxquestions.org/questions/linux-newbie-8/ssh-wrinkle-in-backup-script-836746/)

bluethundr 10-07-2010 08:40 AM

ssh wrinkle in backup script
 
I came up with a shell script that I intend to use to shutdown my list of xen machines, back them up via rsync, then start them back up again. I have the virtual machines running on two physical machines.

The first part of the script turned out ok.. Tinkster gave an excellent tip to help this going yesterday. The script finds the session numbers for the running images, shuts them down and then restarts them. But the problem is in executing the same command on the remote host that holds the rest of the vm's.

Code:

#!/bin/bash
VMLIST1='vm01 vm02 vm03 vm04 vm05 vm06 vm07 vm08 vm09'
VMLIST2='vm10 vm11 vm12 vm13 vm14 vm15'

for i in `/usr/bin/virsh list | /bin/grep -v -e Id -e --- -e 'Domain-0' | /bin/awk '{print $1}'`; do echo ; echo ; echo "Shutting down session $i"; /usr/bin/virsh shutdown $i; echo; echo; echo "Session $i has been shut down." ; sleep 5; echo; echo; done

sleep 300

ssh sum2 "for i in `virsh list | /bin/grep -v -e Id -e --- -e 'Domain-0' | /bin/awk '{print $1}'`; do echo ; echo ; echo 'Shutting down session $i'; echo 'shutting down session $i'; /usr/bin/virsh shutdown $i;  echo; echo; echo 'Session $i has been shut down.'"; done

sleep 300

/usr/bin/rsync -avzp /mnt/store/xen/ /mnt/nas/xen/

sleep 300

for a in $VMLIST1; do /usr/sbin/xm create /mnt/store/xen/conf/$a; echo "starting $a"; sleep 5; done



ssh sum2 "for a in $VMLIST2; do /usr/sbin/xm create /mnt/store/xen/conf/$a; echo "starting $a; sleep 5; done"

This is an example of what I am attempting to parse:

Code:

[root@LCENT02:~]$:virsh list
 Id Name                State
----------------------------------
  0 Domain-0            running
  9 vm10                idle
 10 vm11                idle
 15 vm12                idle
 16 vm13                idle
 17 vm14                idle
 18 vm15                idle
 20 vm02                paused
 21 vm04                paused

The command that I am having a problem with is this one. It runs fine on the local machine:

Code:

[root@LCENT02:~]$:virsh list | grep -v -e Id -e --- -e Domain-0 | awk '{print $1}'
9
10
15
16
17
18
20
21

but not from the remote machine:

Code:

[root@LCENT01:~]#ssh sum2 "virsh list | grep -v -e Id -e --- -e Domain-0 | awk '{print $1}'"
  9 vm10                idle
 10 vm11                idle
 15 vm12                idle
 16 vm13                idle
 17 vm14                idle
 18 vm15                idle
 20 vm02                paused
 21 vm04                paused


I also tried to get a for loop similar to what I would ultimately like to do running on the remote machine via ssh:

from local:
Code:

[root@LCENT02:~]$:for i in `virsh list | grep -v -e Id -e --- -e Domain-0 | awk '{print $1}'`; do echo  $i; done
9
10
15
16
17
18
20
21

from remote:
Code:

[root@LCENT01:~]#ssh sum2 for i in `virsh list | grep -v -e Id -e --- -e Domain-0 | awk '{print $1}'`; do echo  $i; done
-bash: syntax error near unexpected token `do'
[root@LCENT01:~]#ssh sum2 "for i in `virsh list | grep -v -e Id -e --- -e Domain-0 | awk '{print $1}'`; do echo  $i; done"
[root@LCENT01:~]#ssh sum2 'for i in `virsh list | grep -v -e Id -e --- -e Domain-0 | awk '{print $1}'`; do echo  $i; done'
awk: cmd. line:1: {print
awk: cmd. line:1:      ^ unexpected newline or end of string

I know this is supposed to be tricky, but have tried everything I can think of to get this to work, and (hat in hand) would certainly appreciate any advice anyone may have...

Tinkster 10-07-2010 12:32 PM

Try
Code:

[root@LCENT01:~]#ssh sum2 "for i in `virsh list | grep -v -e Id -e --- -e Domain-0 | awk '{print \$1}'`; do echo  \$i; done"
And as grail pointed out in your other thread: you don't need
grep at all - awk will handle this just fine by itself.
Code:

[root@LCENT01:~]#ssh sum2 "for i in `virsh list | awk '$0 !~ /Id|---|Domain-0/{print \$1}'`; do echo  \$i; done"
For obvious reasons that's untested. I don't have an Xen VMs on
remote hosts :D


Cheers,
Tink


All times are GMT -5. The time now is 09:13 AM.