Expect is a handy scripting tool for task automation. You may have never heard of it. I heard about many scripting things before. But one day I needed something simple but didn’t know how to proceed, what would be a good tool for my purpose. The task was simple. Exporting a website content from a managed hosting to a VPS (virtual private server) onto another company in another continent without human intervention. The human intervention part also meant, and this was one of the goals, not having an ssh connection on enduring several long hours, not only to save power on my devices but also just in case my connection went down for whatever the reason. I looked around for a few hours and expect convinced me.
What is expect after all? On my own words it is a tool that allows you to execute tasks a human being will be required to perform them but removing the human from the equation. This is called automation. Is it good or is it bad? Both. I tend to look at it as good since automation releases human attention from tedious tasks and allows us to focus on other things we consider more important and convenient to accomplish.
If you find the articles in Adminbyaccident.com useful to you, please consider making a donation.
Use this link to get $200 credit at DigitalOcean and support Adminbyaccident.com costs.
Get $100 credit for free at Vultr using this link and support Adminbyaccident.com costs.
Mind Vultr supports FreeBSD on their VPS offer.
How did I use it then? Well… I wrote the expect script so then at a convenient time a cron job would trigger it and perform the task (export all my website data). I had already dumped the databases. All I needed was my content. It was more than one site, since I was hosting for more things than mine. I did some tests with several expect scripts until I nailed it. Editing cron was also something I was familiar with but once you play with it you discover a nice way to arrange tasks and everything becomes logic in your head.
What this script depended on? Basically ssh access which I had.
How was the expect script structured?
Asking to the remote server for an sftp connection
Send the password
Once in give the right command to get the content from the right target
Keep the connection open enough time for the transfer to finish.
Close the connection.
This script will only work if you have previously established an ssh connection to the remote server. Otherwise it will fail since there will be some questions the expect script will not be able to answer. Adapt it to your specific needs. For example in this case the hosting company allowed ssh connections by using passwords. But your use case for a similar scenario may slightly vary. Imagine your hosting company may be using ssh keys only. That’s different and you have to adapt the script to it.
Once you have the script you have to set the cronjob. The easiest way is to do this from a web based administration panel such as Webmin. But of course you can also set this from the terminal. Obviously you will have to install expect into one of the machines involved. In my case it was the destination server since this was the one calling (with the script) to the remote one asking for the data. I was claiming for my beloved articles, pictures, and hours of work. And I did it while I slept, ate and went for a drink. It was automatic.