LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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-13-2012, 04:47 PM   #1
wh33t
Member
 
Registered: Oct 2003
Location: Canada
Posts: 695

Rep: Reputation: 47
Is it possible to ssh just one or two commands from a script?


I have a back up machine that regularly rsyncs to a webserver. So far this has been working pretty good but now I'd also like the back up machine to back up the databases as well as the files. I was hoping there was a way to to have my back up script trigger a mysql dump through a single ssh command, then the rsync would back up the files as well as the mysql_dump.sql and then after the rsync is complete the script would once again ssh a single command to the webserver to delete the mysql_dump.sql.

Is this kind of thing possible?

I'm hoping for something like "ssh admin@webserver.com; mysqldump -u root -ppassword -all-databases > /sqldir/sql.sql"
 
Old 10-13-2012, 05:06 PM   #2
JaseP
Senior Member
 
Registered: Jun 2002
Location: Eastern PA, USA
Distribution: K/Ubuntu 12.04/14.04, Scientific Linux 6.3/6.4, Android-x86, Pretty much all distros at one point...
Posts: 1,799

Rep: Reputation: 157Reputation: 157
Let me stop you for a second... K-I-S-S philosophy...

Why don't you do this as a cron job,... Wouldn't that be better???

Or, if you are looking to force backups,... create a script that does the job you want with a single command on the host, and execute that script after you SSH in... ???

You can make your script as complex as you want, with conditional steps, etc... You could even make it backup certain things based on the the phases of the moon if you incorporate pom.
 
Old 10-13-2012, 05:08 PM   #3
wh33t
Member
 
Registered: Oct 2003
Location: Canada
Posts: 695

Original Poster
Rep: Reputation: 47
Quote:
Originally Posted by JaseP View Post
Let me stop you for a second... K-I-S-S philosophy...

Why don't you do this as a cron job,... Wouldn't that be better???

Or, if you are looking to force backups,... create a script that does the job you want with a single command on the host, and execute that script after you SSH in... ???

You can make your script as complex as you want, with conditional steps, etc... You could even make it backup certain things based on the the phases of the moon if you incorporate pom.
This will run from a backup script. But the question remains the same, I still need to know if it's possible to SSH a single command to the webserver from the backup script. The reason I don't want to tell the webserver to run the command itself is that I then would have to create another rsa key pair for the webserver to be able to passwordless ssh into the back up machine. There is already one key pair for the backup machine to ssh passwordless into the webserver. Believe it or not I am trying to K-I-S-S!
 
Old 10-13-2012, 05:42 PM   #4
JaseP
Senior Member
 
Registered: Jun 2002
Location: Eastern PA, USA
Distribution: K/Ubuntu 12.04/14.04, Scientific Linux 6.3/6.4, Android-x86, Pretty much all distros at one point...
Posts: 1,799

Rep: Reputation: 157Reputation: 157
Well don't KISS me...

J-King...

So, ... Let me get this straight,... You have a server,... then you have a separate Web-server. And,... you want to backup both sets of files... Is this right?!?!

I would suggest that the better course of action might be to have the web-server run its backup local to a shared directory, and then (after sufficient time delay), have the server backup its stuff and the shared directory... You don't have to enable the webserver to have access to anything on the server, just the other way around...

Just a thought...
 
Old 10-13-2012, 06:02 PM   #5
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,133
Blog Entries: 2

Rep: Reputation: 4839Reputation: 4839Reputation: 4839Reputation: 4839Reputation: 4839Reputation: 4839Reputation: 4839Reputation: 4839Reputation: 4839Reputation: 4839Reputation: 4839
But that actually does not at all answer the original question.
Yes, it is possible to launch single commands with ssh and you almost guessed the correct syntax:
Code:
ssh admin@webserver.com "mysqldump -u root -ppassword -all-databases > /sqldir/sql.sql"
If you add a command after the sh command it will be launched on the machine you connect to. For further information have a look at
Code:
man ssh
 
Old 10-13-2012, 07:19 PM   #6
wh33t
Member
 
Registered: Oct 2003
Location: Canada
Posts: 695

Original Poster
Rep: Reputation: 47
Talking

Quote:
Originally Posted by TobiSGD View Post
But that actually does not at all answer the original question.
Yes, it is possible to launch single commands with ssh and you almost guessed the correct syntax:
Code:
ssh admin@webserver.com "mysqldump -u root -ppassword -all-databases > /sqldir/sql.sql"
If you add a command after the sh command it will be launched on the machine you connect to. For further information have a look at
Code:
man ssh
YES! I almost sound like I know what I'm doing! Linux has been a real struggle for me. So if I do the above command it will kill the SSH connection after it's done the mysqldump?

Thank you for your help
 
Old 10-13-2012, 07:21 PM   #7
wh33t
Member
 
Registered: Oct 2003
Location: Canada
Posts: 695

Original Poster
Rep: Reputation: 47
Wink

Quote:
Originally Posted by JaseP View Post
Well don't KISS me...

J-King...

So, ... Let me get this straight,... You have a server,... then you have a separate Web-server. And,... you want to backup both sets of files... Is this right?!?!

I would suggest that the better course of action might be to have the web-server run its backup local to a shared directory, and then (after sufficient time delay), have the server backup its stuff and the shared directory... You don't have to enable the webserver to have access to anything on the server, just the other way around...

Just a thought...
I have a webserver, and a backup machine which sits in my house. I don't have a port forward set up in my home router or anything which is something I'd have to set up if I wanted the webserver to be able to push files onto the back up machine. I do not want that because of the extra configuration and complexity involved and because in the future I see myself having multiple back up machines in multiple places and I would love to be able to just copy the system I have on the back up machine versus configuring a network and an RSA key pair every time I expand the number of back up machines... there is some method to my madness
 
Old 10-13-2012, 07:39 PM   #8
JaseP
Senior Member
 
Registered: Jun 2002
Location: Eastern PA, USA
Distribution: K/Ubuntu 12.04/14.04, Scientific Linux 6.3/6.4, Android-x86, Pretty much all distros at one point...
Posts: 1,799

Rep: Reputation: 157Reputation: 157
OK,... But question...

What limitations are there on the remote system, and what limitations are there on the @home backup system??? You might not want to be downloading business backups over a home connection, if that connection has got upload/download limits on it...
 
Old 10-13-2012, 08:42 PM   #9
wh33t
Member
 
Registered: Oct 2003
Location: Canada
Posts: 695

Original Poster
Rep: Reputation: 47
Quote:
Originally Posted by JaseP View Post
OK,... But question...

What limitations are there on the remote system, and what limitations are there on the @home backup system??? You might not want to be downloading business backups over a home connection, if that connection has got upload/download limits on it...
It's a high speed ADSL connection. I think I'm entitled to a 500gb a month of traffic and I'm using Rsync, so I'm downloading the changes every day.
 
Old 10-13-2012, 09:07 PM   #10
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,133
Blog Entries: 2

Rep: Reputation: 4839Reputation: 4839Reputation: 4839Reputation: 4839Reputation: 4839Reputation: 4839Reputation: 4839Reputation: 4839Reputation: 4839Reputation: 4839Reputation: 4839
Quote:
Originally Posted by wh33t View Post
So if I do the above command it will kill the SSH connection after it's done the mysqldump?
That is how it works.
 
Old 10-13-2012, 09:14 PM   #11
wh33t
Member
 
Registered: Oct 2003
Location: Canada
Posts: 695

Original Poster
Rep: Reputation: 47
Quote:
Originally Posted by TobiSGD View Post
That is how it works.
So it just always implied that if you combine statements or commands they exit at the end?

Normally an SSH login like that would keep you logged in.
 
Old 10-13-2012, 09:23 PM   #12
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,133
Blog Entries: 2

Rep: Reputation: 4839Reputation: 4839Reputation: 4839Reputation: 4839Reputation: 4839Reputation: 4839Reputation: 4839Reputation: 4839Reputation: 4839Reputation: 4839Reputation: 4839
A normal SSH command without appended command will log you in and wait for your input. A SSH command with appended command will log you in, execute the given command and log you out. Provided that the command you launch does not wait for user input.
You can do some some amazing things with that, like piping commands through SSH. For example:
Code:
ssh tobi@demon "tar -cz /home/tobi" | cat >home-backup.tgz
will tar and compress my home folder on the remote machine and pipe it over SSH to the cat command on my local machine which writes it to the file home-backup.tgz.
Sounds weird, but once you can wrap your mind around that concept it becomes quite handy.
 
Old 10-13-2012, 11:04 PM   #13
wh33t
Member
 
Registered: Oct 2003
Location: Canada
Posts: 695

Original Poster
Rep: Reputation: 47
Quote:
Originally Posted by TobiSGD View Post
A normal SSH command without appended command will log you in and wait for your input. A SSH command with appended command will log you in, execute the given command and log you out. Provided that the command you launch does not wait for user input.
You can do some some amazing things with that, like piping commands through SSH. For example:
Code:
ssh tobi@demon "tar -cz /home/tobi" | cat >home-backup.tgz
will tar and compress my home folder on the remote machine and pipe it over SSH to the cat command on my local machine which writes it to the file home-backup.tgz.
Sounds weird, but once you can wrap your mind around that concept it becomes quite handy.
That's incredible! So easy to do so much. Linux is like the geeks swiss army knife.
 
  


Reply

Tags
ssh single command


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
How to execute multiple commands via SSH in a script angrymachine Linux - General 4 10-12-2011 10:05 AM
Root commands in SSH script japhyr Programming 5 03-01-2011 11:22 AM
[SOLVED] Passing commands through an SSH shell in a bash script buee Linux - Newbie 12 07-01-2010 01:28 AM
script which logs into devices over ssh and executes commands m4rtin Programming 1 03-23-2010 12:51 PM
Running multiple commands remotely via SSH in a script gimpy530 Linux - General 4 12-19-2009 10:22 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:44 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration