So I need to automate a process where an oracle dump file is moved to a remote server, and the existing loacl file is then permanently removed. I have to make sure that:

  1. the status of copy process is reported, whether successful, or not
  2. The remove process will only start if the copy process is successful
  3. The copy process is unattended, so a public key authentication must be available between the local and remote server. I have guide regarding a public key authentication for ssh/scp somewhere around here

This is how the script looked like:


 #!/sbin/sh
 date >> /backup/movedump.log
 tgl=$(date +"%y-%m-%d-%M")
 cd /backup
 if [ -f /backup/dumpf* ];
 then
 scp dumpf* kirim@172.16.2.15:/opt/data/dump/backup/
 STAT=$?
 echo "$tgl-S1:dump file succesfuly moved" >> /backup/movedump.log
 else
 STAT=1
 fi
 if [ $STAT -eq 0 ];
 then
 rm dumpf*
 echo "$tgl-S2:local dump file deleted" >> /backup/movedump.log
 else
 if [ $STAT -eq 1 ];
 then
 echo "$tgl-E1:no dump file on source directory, no action taken" >> /backup/movedump.log
 else
 echo "$tgl-E2:dump file move process exit with code $STAT, remove process not executed" >> /backup/movedump.log
 fi
 fi
 

The code above will check whether the dumpfile exists or not. If the file doesn’t exist, the variable STAT will be given a value of 1. If the file exist, it will begin scp process to the remote server. Scp process provide us with several report codes that is stored on “$?”. If the copy process is successful, the value of “$?” is 0, other if it’s not. That value is then stored on the variable STAT

The process then proceed based on the value inserted to the variable STAT

  1. If STAT=0, the copy process is successful, the script will proceed to remove the dump file on the local server
  2. if STAT=1, the script did not find a dump file to transfer to remote server
  3. if STAT value is other than 1 and 0, the transfer process has failed

All of the step taken by the script is logged on file movedump.log

The next step is to make sure the script is executed everyday, automatically. For me, it is scheduled to run 4 pm, everyday:.

$crontab -e

Then put this line:

00 16 * * * /backup/movedump.sh

With this, the script will be executed every day at 4 pm sharp.

Done!

By ikhsan

6 thoughts on “Scheduling a move file using cron and scp”
  1. How did you set up the scp to work without entering your password and/or RSA/DSA passphrase? Do you have ssh-agent automatically running on startup? Did you do that part manually and leave the existing spawned ssh-agent shell running thereby keeping your credentials alive? Other? I appreciate your insights. Thx.

    1. You can use public key authentication method to check the credential between host. This way, you don’t have to type in the password.
      There are several security measurement that you have to consider when using this method, such as the user from the client machine will be able to gain access to the other host
      Please check out the guide that i have written regarding how to do this.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.