Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- TIMESTAMP=$(date)
- PATH=/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
- DROPBOXLOG=a
- TRANSFERLOG=b
- TEMPDIR=c
- BACKUPDIR=d
- DROPDIR=e
- SFTPSERVER="a"
- SFTPUSER="b"
- SFTPPASS="c"
- # Write initial time stamp to both logs.
- echo "$TIMESTAMP ---Forms Instance Begin---" | tee -a "$TRANSFERLOG" >> "$DROPBOXLOG"
- # Download files off sftp server.
- echo ">> Checking SFTP server for new files..." >> "$TRANSFERLOG"
- /usr/bin/expect <<EOF 2>&1 | tee -a "$TRANSFERLOG"
- spawn sftp $SFTPUSER@$SFTPSERVER
- expect {
- timeout {puts "Connection timed out."; exit}
- "password:"
- }
- send "$SFTPPASS\n"
- expect "\nsftp>"
- send "ls -l\n"
- expect {
- "*root*\nsftp>" {
- send "lcd '$TEMPDIR'\n"
- expect "\nsftp>"
- send "mget *\n"
- expect "\nsftp>"
- }
- "\nsftp>"
- }
- send "bye\n"
- expect "\n"
- expect eof
- EOF
- echo ">> Done. " | tr -d "\n" >> "$TRANSFERLOG"
- # Did we download any files?
- if [ $(ls -F $TEMPDIR | grep -v \/ | wc -l) != "0" ]; then
- # Yes we did, so delete only the files downloaded from the server...
- echo "Deleting downloaded files from server..." >> "$TRANSFERLOG"
- find $TEMPDIR -maxdepth 1 -type f -printf "%f\n" | while read DELFILE; do
- /usr/bin/expect <<EOF 2>&1 | tee -a "$TRANSFERLOG"
- spawn sftp $SFTPUSER@$SFTPSERVER
- expect {
- timeout {puts "Connection timed out."; exit}
- "password:"
- }
- send "$SFTPPASS\n"
- expect "\nsftp>"
- send "rm '$DELFILE'\n"
- expect "\nsftp>"
- send "bye\n"
- expect "\n"
- expect eof
- EOF
- done
- # ...and do move and copy operations locally.
- ls -lh "$TEMPDIR" | grep -i "^-" >> "$DROPBOXLOG"
- echo ">> Done. Copying files to drop box..." >> "$TRANSFERLOG"
- find "$TEMPDIR" -maxdepth 1 -type f | while read CPFILE; do
- cp -v "$CPFILE" "/var/dropbox/files/$DROPDIR" >> "$TRANSFERLOG"
- done
- echo ">> Done. Moving files to backup directory..." >> "$TRANSFERLOG"
- find "$TEMPDIR" -maxdepth 1 -type f | while read MVFILE; do
- mv -v "$MVFILE" "$BACKUPDIR" >> "$TRANSFERLOG"
- done
- echo ">> All operations complete." >> "$TRANSFERLOG"
- # No we didn't, so don't do anything with the sftp server.
- else echo "No files transferred." >> "$TRANSFERLOG"
- fi
- # Write concluding time stamp to both logs.
- echo "$TIMESTAMP ---Forms Instance End---" | tee -a "$TRANSFERLOG" >> "$DROPBOXLOG"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement