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:
- the status of copy process is reported, whether successful, or not
- The remove process will only start if the copy process is successful
- 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* email@example.com:/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
- If STAT=0, the copy process is successful, the script will proceed to remove the dump file on the local server
- if STAT=1, the script did not find a dump file to transfer to remote server
- 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:.
Then put this line:
00 16 * * * /backup/movedump.sh
With this, the script will be executed every day at 4 pm sharp.