Pages

Thursday, June 1, 2023

sftp from server to server with crontab task

===============
Send files from server A to server B from crontab
===============
Step A - Generate a commands file
Step B - Execute this commands file from bash script


generate_sftp_script.sh

#!/bin/bash
REMOTE_SERVER=back_server_01
REMOTE_PATH=/starhome/ES/ora_online
WORK_DIR=/home/shdaemon/ftp_to_mng
BACKUP_DIR=/backup/ora_online/for_backup
SFTP_SCRIPT=ftp_commands.txt
LOG_FILE=sftp_to_mng.log
RUN_DATE=`date +"%Y%m%d"_"%H%M%S"`

backup_dir_last=`ls -ltr ${BACKUP_DIR} | grep -v log | grep 202 | tail -1 | awk '{print $9}'`

cd ${WORK_DIR}
echo "echo Begin sftp to $REMOTE_SERVER at $RUN_DATE" >>$LOG_FILE
rm -f ${SFTP_SCRIPT} 2>/dev/null
echo "lcd ${BACKUP_DIR}/${backup_dir_last}" >> ${SFTP_SCRIPT}
echo "mkdir  ${backup_dir_last}" >>${SFTP_SCRIPT}
echo "cd ${backup_dir_last}" >> ${SFTP_SCRIPT}
for f in `ls -1 $BACKUP_DIR/$backup_dir_last/`
do
 echo "put $f" >> ${SFTP_SCRIPT}
done
echo "bye" >> ${SFTP_SCRIPT}

main_sftp.sh
#!/bin/bash
sftp -b ftp_commands.txt someuser@back_server_01

generated ftp_commands.txt
lcd /backup/ora_online/for_backup/20230601_0300
cd /starhome/ES/ora_online
mkdir  20230601_0300
cd 20230601_0300
put file_1
put file_2
put file_3
put file_4
bye


No comments:

Post a Comment