Reputation: 173
I'm a programming apprentice and have recently been given a job to complete on the terminal and I need some help or just some pointers in the right direction as to what I need to do.
I need to copy a certain file from a remote server and back it up every hour with a time stamp included. I know how to connect to servers using SSH and I also know how to transfer files using SCP - it's just writing the script I need some help with. I know that I will need to use CRON to schedule it I just don't know how I can put everything together. I'm using the latest version of Ubuntu.
Any help would be much appreciated.
Thanks.
Upvotes: 0
Views: 8485
Reputation: 272247
Just create a shell script thus:
#!/bin/bash
scp username@host:file.log file.log.`date +%H%M%S`
(error checking elided - don't forget to make it executable via chmod u+x
)
The date invocation takes the current date/time, creates a timestamp and appends it to the destination log file name. So you'll get something like:
file.log.131504
That's time-based, but you'll likely want a date instead. For more info see here and the date man page for formatting options.
Note that jobs under cron run with a cut down environment (reduced PATH etc.). So be sure to set all appropriate environment variables in your script, cd to the correct directory etc., and log the results via something like:
* * * * * /home/user/mycronscript.sh 2>&1 >/tmp/test.log
Upvotes: 3