#!/usr/bin/expect -- # $Id: upload.sh,v 1.3 2007/09/29 19:21:28 sms Exp $ # www.pccl.demon.co.uk # # Script to upload a website. # The idea is that I make a "Ghost" filestore that has the same filenames as # the WWW filestore but empty. The action is then: # # 1. Workout how to make .WWW look like WWW and create an update array. # 2. Connect to the ISP and obey the update array. As each change is successful, # make the same change to .WWW. # # WRITEME: Look for files to delete # WRITEME: Remove ghost directory if it can't be made. # WRITEME: Do ftp equivalent of mkdir -p # Uncomment the next line to turn on logging. #log_file -noappend log # Set the 0 to 1 to see what's happening log_user 0 # Program definitions set HOME $env(HOME) set PROMPT "ftp> " set timeout -1 # These might be parameters one day # NB: on the website all files are in docroot. set HOST "homepages.demon.co.uk" set ROOT "/srv/www/htdocs" append GHOST $HOME "/.Image" # Look for changes to $ROOT and form the update array. (FTP commands) if [file isdirectory $ROOT] { cd $ROOT set actions 0 foreach filename [exec find . \( -type f -o -type l \) -print] { set ghost "" append ghost $GHOST [string trimleft $filename "."] # We have determined a filename and its ghost. # Check if the ghost is newer. if [file isfile $ghost] { set fileTime [file mtime $filename] set ghostTime [file mtime $ghost] if {$fileTime >= $ghostTime} { # Schedule the put to the host set update($actions,action) put set update($actions,filename) "$filename docroot/$filename" set update($actions,ghost) $ghost incr actions } } else { set ghostdir [file dirname $ghost] if [file isdirectory $ghostdir] { } else { # Make the ghost directory exec mkdir -p $ghostdir # Schedule the mkdir on the host set dir [file dirname $filename] set update($actions,action) mkdir set update($actions,filename) docroot/$dir set update($actions,ghost) - incr actions } # Schedule the put to the host set update($actions,action) put set update($actions,filename) "$filename docroot/$filename" set update($actions,ghost) $ghost incr actions } } } else { puts "$ROOT is not a directory" exit } # If nothing has changed then exit if {$actions == 0} { puts "No work to do" exit } # Connect puts "Connecting" spawn ftp $HOST expect { "\\n\[45]?*" { puts "Failed to connect" exit } "Unknown host" { puts "$expect_out(buffer)" puts "Failed to connect" exit } $PROMPT } puts "Connected - OK" # Update the ISP by obeying the update list. for {set i 0} {$i < $actions} {incr i} { set command "$update($i,action) $update($i,filename)" puts "$command" send "$command\r" expect { "\\n\[45]?*" { puts "111 " puts "$expect_out(buffer)" puts "$command - Failed" exit } "Not connected" { puts "$expect_out(buffer)" puts "$command - Failed" exit } "No such file or directory" { puts "$expect_out(buffer)" puts "$command - Failed" exit } "\\n\\??*" { puts "$expect_out(buffer)" puts "$command - Failed" exit } $PROMPT } puts "$command - OK" set ghost $update($i,ghost) if {$ghost == "-"} {} else { exec touch $ghost } }