Spencer Mark
Spencer Mark

Reputation: 5311

ssh mysqldump from oscent to remote server

I'm trying to create automated backups of the mysql databases from my virtual host to my NAS storage.

I'm only just starting to learn shell commands so please bear with me - what I've found so far is:

mysqldump 
   -uusername 
   -ppassword 
   --opt database_name | 
   gzip -c | 
   ssh user@ipaddress 
   "cat > /path-to-the-directory-on-nas/$(date +%Y-%m-%d_%H.%I.%S).sql.gz"

but this seem to return the following error:

-bash: /path-to-the-directory-on-nas/$(date +%Y-%m-%d_%H.%I.%S).sql.gz: No such file or directory

Does anyone know how to overcome this problem and actually save it to the designated storage?

Upvotes: 1

Views: 873

Answers (4)

Nin
Nin

Reputation: 3020

You should debug further. First try

cat > /path-to-the-directory-on-nas/test.sql.gz. 

After that you should try if the date works:

echo $(date +%Y-%m-%d_%H.%I.%S)

Then you'll know if the path exists or if date... fails. From your error msg it seems like the date is the problem but you need to be sure first. Then you could try to assign the date to a variable:

#!/bin/bash
 filename=$(date +%Y-%m-%d_%H.%I.%S);
    mysqldump 
       -uusername 
       -ppassword 
       --opt database_name | 
       gzip -c | 
       ssh user@ipaddress 
     "cat > /path-to-the-directory-on-nas/$filename.sql.gz"

Upvotes: 1

dannyla
dannyla

Reputation: 1990

Check that the directory /path-to-the-directory-on-nas/ exists on the remote server.

If it is missing you can create it over ssh with the following command:

 ssh user@ipaddress mkdir -p /path-to-the-directory-on-nas/

( using the -p if there is multiple directories tree that need to be created )

If you wanted to create the directory with a time stamp you should do the following:

 ssh user@ipaddress mkdir -p /path-to-the-directory-on-nas/$(date '+%Y%M%D')/'

If you choose to include a timestamp in the directory path, you need to include it in the path that your mysqldump command uses.

Example:

Successfully create the file to a remote directory that exists on the remote system /var/tmp

 $ date | ssh user@ipaddress 'cat > /var/tmp/file.txt'
 $ ssh user@ipaddress cat /var/tmp/file.txt
 Fri Oct 12 19:39:16 EST 2012

Failing with the same error you are getting, trying to write to a directory that dosn't exist.

 $ date | ssh user@ipaddress 'cat > /var/Xtmp/file.txt'
 bash: /var/Xtmp/file.txt: No such file or directory

Upvotes: 1

raynix
raynix

Reputation: 101

Change

cat > /path-to-the-directory-on-nas/$(date +%Y-%m-%d_%H.%I.%S).sql.gz

to

cat > /path-to-the-directory-on-nas/`date +%Y-%m-%d_%H.%I.%S`.sql.gz

Make sure the folder already exists. At least worked on my Ubuntu :)

Upvotes: 2

RandomSeed
RandomSeed

Reputation: 29769

Replace

ssh user@ipaddress 
   "cat > /path-to-the-directory-on-nas/$(date +%Y-%m-%d_%H.%I.%S).sql.gz"

with

ssh user@ipaddress 
   "cat > /path-to-the-directory-on-nas/"$(date +%Y-%m-%d_%H.%I.%S)".sql.gz"

Upvotes: 0

Related Questions