Copying and renaming file using SCP

Posted on 2012-08-30
Medium Priority
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.
Question by:afflik1923
  • 5
  • 3
LVL 68

Accepted Solution

woolmilkporc earned 2000 total points
ID: 38350558
FILEBASE=$(basename $FILE)
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.

Author Comment

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?
LVL 68

Expert Comment

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)"
     TEXT="Transfer of $FILE on $(hostname) succeeded: $(date)"
echo "$TEXT" | mailx -s "Transfer Results from $(hostname)" recipient@domain.tld
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

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

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

Author Comment

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.

Author Comment

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

Expert Comment

ID: 38351204
Instead of

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


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

The mail message will have an empty body this way.

Thx for the points!

Author Comment

ID: 38351932
Thats perfect, thanks again

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