LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 10-07-2010, 09:40 AM   #1
bluethundr
Member
 
Registered: Jun 2003
Location: Summit, NJ
Distribution: CentOS 5.4
Posts: 122

Rep: Reputation: 15
Post 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...
 
Old 10-07-2010, 01:32 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,004
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
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

Last edited by Tinkster; 10-07-2010 at 01:33 PM.
 
  


Reply

Tags
bash scripting


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What happens with backup-manager.org (very useful backup script)? Murz Linux - Software 3 07-27-2010 07:46 AM
Newbie trying to write a simple backup script to backup a single folder Nd for school stryker759a Linux - Newbie 2 09-16-2009 09:52 AM
Need help with script to organise files into folders as part of DVD backup script jasybee2000 Linux - Newbie 5 06-15-2009 08:29 PM
LXer: Microsoft's latest open-source release catches a wrinkle LXer Syndicated Linux News 1 04-03-2009 02:06 PM
how to create backup MYSQL Script to backup my database for every 1hour RMLinux Linux - Newbie 3 11-20-2008 11:13 AM


All times are GMT -5. The time now is 09:53 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration