SHARE
TWEET

Untitled

a guest May 31st, 2017 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. TIMESTAMP=$(date)
  3. PATH=/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
  4.  
  5. DROPBOXLOG=a
  6. TRANSFERLOG=b
  7. TEMPDIR=c
  8. BACKUPDIR=d
  9. DROPDIR=e
  10.  
  11. SFTPSERVER="a"
  12. SFTPUSER="b"
  13. SFTPPASS="c"
  14.  
  15. # Write initial time stamp to both logs.
  16. echo "$TIMESTAMP ---Forms Instance Begin---" | tee -a "$TRANSFERLOG" >> "$DROPBOXLOG"
  17.  
  18. # Download files off sftp server.
  19. echo ">> Checking SFTP server for new files..." >> "$TRANSFERLOG"
  20. /usr/bin/expect <<EOF 2>&1 | tee -a "$TRANSFERLOG"
  21. spawn sftp $SFTPUSER@$SFTPSERVER
  22. expect  {
  23.         timeout {puts "Connection timed out."; exit}
  24.         "password:"
  25.         }
  26. send "$SFTPPASS\n"
  27. expect "\nsftp>"
  28. send "ls -l\n"
  29. expect  {
  30.         "*root*\nsftp>" {
  31.                 send "lcd '$TEMPDIR'\n"
  32.                 expect "\nsftp>"
  33.                 send "mget *\n"
  34.                 expect "\nsftp>"
  35.                 }
  36.         "\nsftp>"
  37.         }
  38. send "bye\n"
  39. expect "\n"
  40. expect eof
  41. EOF
  42.  
  43. echo ">> Done.  " | tr -d "\n" >> "$TRANSFERLOG"
  44.  
  45. # Did we download any files?
  46. if [ $(ls -F $TEMPDIR | grep -v \/ | wc -l) != "0" ]; then
  47.  
  48. # Yes we did, so delete only the files downloaded from the server...
  49. echo "Deleting downloaded files from server..." >> "$TRANSFERLOG"
  50.  
  51. find $TEMPDIR -maxdepth 1 -type f -printf "%f\n" | while read DELFILE; do
  52.  
  53. /usr/bin/expect <<EOF 2>&1 | tee -a "$TRANSFERLOG"
  54. spawn sftp $SFTPUSER@$SFTPSERVER
  55. expect {
  56.         timeout {puts "Connection timed out."; exit}
  57.         "password:"
  58.         }
  59. send "$SFTPPASS\n"
  60. expect "\nsftp>"
  61. send "rm '$DELFILE'\n"
  62. expect "\nsftp>"
  63. send "bye\n"
  64. expect "\n"
  65. expect eof
  66. EOF
  67.  
  68. done
  69.  
  70. # ...and do move and copy operations locally.
  71. ls -lh "$TEMPDIR" | grep -i "^-" >> "$DROPBOXLOG"
  72.  
  73. echo ">> Done.  Copying files to drop box..." >> "$TRANSFERLOG"
  74. find "$TEMPDIR" -maxdepth 1 -type f | while read CPFILE; do
  75. cp -v "$CPFILE" "/var/dropbox/files/$DROPDIR" >> "$TRANSFERLOG"
  76. done
  77.  
  78. echo ">> Done.  Moving files to backup directory..." >> "$TRANSFERLOG"
  79. find "$TEMPDIR" -maxdepth 1 -type f | while read MVFILE; do
  80. mv -v "$MVFILE" "$BACKUPDIR" >> "$TRANSFERLOG"
  81. done
  82.  
  83. echo ">> All operations complete." >> "$TRANSFERLOG"
  84.  
  85. # No we didn't, so don't do anything with the sftp server.
  86. else echo "No files transferred." >> "$TRANSFERLOG"
  87.  
  88. fi
  89.  
  90. # Write concluding time stamp to both logs.
  91. echo "$TIMESTAMP ---Forms Instance End---" | tee -a "$TRANSFERLOG" >> "$DROPBOXLOG"
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top