?
Solved

Copying and renaming file using SCP

Posted on 2012-08-30
8
Medium Priority
?
3,577 Views
Last Modified: 2013-11-15
I wish to be able to copy a backup file from a hosted linux server to our windows server running Cygwin in the office. I've set this up and can copy manually, i now wish to create a script to automate this.

This is how far i have got:

scp test.dat ftpuser@IPADDRESS:/home/ftpuser/Backup

What i wish to do is for the script to rename the file once it reaches the destination with the date and time in the file name, so the file name in this example would be test-30082012-1500.dat

i've set up the keys and am able to connect without using a password. it's just the file rename i'm struggling with.

Thanks in advance.
0
Comment
Question by:afflik1923
  • 5
  • 3
8 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 2000 total points
ID: 38350558
FILE="/path/to/test.dat"
FILEBASE=$(basename $FILE)
FILENAME=${FILEBASE%%.*}
FILEEXT=${FILEBASE##*.}
DATESTRING=$(date "+%d%m%Y-%H%M")
scp $FILE ftpuser@IPADDRESS:/home/ftpuser/Backup/${FILENAME}-${DATESTRING}.${FILEEXT}

Open in new window

Works with extensions other than ".dat" as well, and works whether a full path to the file is given or not. A ".something" extension is required, however.
0
 

Author Comment

by:afflik1923
ID: 38350825
Thanks woolmilkporc thats exactly what i wanted and has worked just great.

Just to add to that is there an easy way to have an email sent advising of successful or failed transfer?
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 38350890
You can add just following the line containing the scp command:

if [[ $? -ne 0 ]]; then
   TEXT="Transfer of $FILE on $(hostname) failed: $(date)"
  else
     TEXT="Transfer of $FILE on $(hostname) succeeded: $(date)"
fi
echo "$TEXT" | mailx -s "Transfer Results from $(hostname)" recipient@domain.tld
0
Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

 

Author Comment

by:afflik1923
ID: 38350927
ok thanks, however i get the following:

You must specify direct recipients with -s, -c, or -b
0
 

Author Comment

by:afflik1923
ID: 38351023
No ignore that i was missing a space before the email address.

Thanks again this has saved me a lot of googling ans is exactly what i was looking for.
0
 

Author Comment

by:afflik1923
ID: 38351033
Ah one last thing, would be great to have the failed or success message in the subject?
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 38351204
Instead of

echo "$TEXT" | mailx -s "Transfer Results from $(hostname)" recipient@domain.tld

use

mailx -s "$TEXT" recipient@domain.tld </dev/null

The mail message will have an empty body this way.

Thx for the points!
0
 

Author Comment

by:afflik1923
ID: 38351932
Thats perfect, thanks again
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you discover the power of the R programming language, you are going to wonder how you ever lived without it! Learn why the language merits a place in your programming arsenal.
Eseutil Hard Recovery is part of exchange tool and ensures Exchange mailbox data recovery when mailbox gets corrupt due to some problem on Exchange server.
The viewer will learn how to successfully download and install the SARDU utility on Windows 7, without downloading adware.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question