Our customer asked if they can automatically upload latest RMAN backups via FTP to another server. Because they keep latest 3 backups in same directory, I wrote a small bash script which calls an SQL script to queries RMAN backups and then uploads only the backup pieces belong to the latest backup job. I wanted to share this script because it demonstrates some interesting methods such as handling arrays returned from SQLPLus.
Here’s the SQL script (rmanfind.sql) to query RMAN backups. Our customer takes archive log backups during the day, so I needed to state full backup jobs while querying.
SET HEA OFF SET FEED OFF SET LINES 500 SET PAGES 500 SET TRIM ON SELECT handle from V$BACKUP_PIECE where rman_Status_stamp IN ( select stamp from V$RMAN_STATUS where parent_stamp = (SELECT session_stamp from V$RMAN_BACKUP_JOB_DETAILS where start_time = (select max(start_time) from V$RMAN_BACKUP_JOB_DETAILS where input_type = 'DB FULL')));
The bash script (rmanftp.sh) can divided to 3 parts. In first part, the required credentials and environment variables are defined. On second part, rmanfind.sql is called. On last part, the resultset is processed and files are uploaded via FTP:
#!/bin/bash #--------------------------------------------------------------------- # rmanftp (C) 2013 Gokhan Atil http://www.gokhanatil.com #--------------------------------------------------------------------- # Required Info to Connect Oracle . oraenv <<EOF ORCL EOF ORACLE_CREDENTIALS=gokhan/atil # Required Info to Connect FTP Server TARGETSERVER=192.168.0.10 FTPUSER=oracle FTPPASS=oracle TARGETDIR=DAILY_BACKUP # Query Latest Backup SENDFILES=(`$ORACLE_HOME/bin/sqlplus -s $ORACLE_CREDENTIALS @rmanfind.sql`) COUNT=${#SENDFILES[*]} IDX=0 SENDTHEMALL='' while (( $IDX < $COUNT )) do LOCALPATH=`echo ${SENDFILES[$IDX]} | sed 's|\(.*\)/.*|\1|'` FILENAME=`echo ${SENDFILES[$IDX]} | sed 's|.*/\(.*\)|\1|'` SENDTHEMALL="${SENDTHEMALL}lcd $LOCALPATH\n" SENDTHEMALL="${SENDTHEMALL}put $FILENAME\n" IDX=$(($IDX+1)) done # Convert Newlines to Enters? FTPCMD=`echo -e $SENDTHEMALL` # Upload Files ftp -d -n $TARGETSERVER <<EOF user $FTPUSER $FTPPASS cd $TARGETDIR bin $FTPCMD bye EOF
Here are the important parts of the script:
Line 20: The resultset returned from SQL Plus is put into an array variable. Be careful about the brackets.
Line 22: How many rows we have?
Line 26: A while loop starts here to processes all rows in the result set.
Line 30-31: I extract the path and file name because ftp client does not support full path names. So I create “cd” and “put” commands for each backup piece.
Line 36: SENDTHEMALL variable has newline (\n) characters but when I put it directly to EOF block, these newline characters are not honored, so I call “echo -e” and assign the result to FTPCMD variable.
Line 43: FTPCMD contains ftp commands such as “lcd XXXXX put YYYYY”.