Backing Up Using Expect and Rsync

Share this article

Rsync alone is a powerful tool for moving data within and among local and remote servers. When combined with Expect, interactive sessions can be automated on the command line for very useful purposes.

I find the most powerful application being the process of hot backups to remote servers each evening. While optical disk backups are run for offline archiving and data preservation, I find most the most useful backups being those held on secured remote servers managed via Expect and Rsync.

Expect – found at http://expect.nist.gov/ – simply enables automated responses to prompts at the command line when user intervention is not necessary or reasonable. Thus, overnight backups can be run, enabling remote authentication and a password entry when prompted (when ‘expected’ – hence the name). More information can be found by running man expect on the command line.

These files are appended with the .exp extension and as you will see in the scripts below, use a shebang at the top to call the Expect program. I set permissions on these scripts as executable by root only.

Rsync – found at http://samba.anu.edu.au/rsync/ is a tool for data synchronization, copying of files and so on. More information can be found by running man rsync on the command line.

Rsync can be advantageous as it will perform differential backups once a master copy is completed, backing up files that have changed by reviewing the datestamp and file size, minimizing bandwidth and time frames in which backups need to run. Running a tape or optical backup drive can be configured with the remote backup server to further free up overnight resources for your production web server.

When used in conjunction, a straight forward backup scheme can be implemented for web servers (among other systems).

Here are the scripts in use. They are called from the backup server to the target server and data is stored in ‘hot backup’ for easy access:

Script 1 – MySQL Database backup

#!/usr/bin/expect set timeout 19900 spawn /bin/bash expect -re "]# " send "rsync -avzb -e ssh root@domain.com:/var/db/mysql/dumps/ /backup/servers/comain.com/mysqlr" expect -re "password:" sleep 2 send "rootpasswordr" expect -re "total size is" expect -re "]# " send "exitr"

Now for backing up domain data (web sites), a second script is run:

Script 2 – Domain backups

#!/usr/bin/expect set timeout 19900 spawn /bin/bash expect -re "]# " send "rsync -avzb -e ssh root@domain.com:/home/sites /backup/servers/domain.com/homer" expect -re "password:" sleep 2 send "rootpasswordr" expect -re "total size is" expect -re "]# " send "exitr"

To deconstruct this:

1) Expect is called (your path may be different – reveal it by issuing ‘whereis expect’ on the command line)

2) send the command to the remote server over ssh:

rsync options are -a (archive which preserves permissions and links among other items), v (verbose), z (compress) and b (backup – adds a ~ to preexisting destination files, a sort of versioning of backups)

3) an ssh password is ‘expected’, or in other cases, authorized keys can be copied over to the remote server to bypass a password prompt for ssh.

4) a password is expected – which is sent via Expect to the server for authentication and the operation is carried out.

5) The script completes and closes.

I have named these files mysql.backup.exp and domains.backup.exp and entered them into cron to run on a daily schedule.

While this should not be the only backup procedure – it can be very useful to have a live server available for quick restores or review of backup data.

Additional scripts can also be written to backup critical configuration files (i.e. the /etc directory) and other areas of the server(s).

Frequently Asked Questions (FAQs) on Backing Up Using Expect and Rsync

What is the significance of using Expect with Rsync for backups?

Expect is a tool for automating interactive applications such as telnet, ftp, passwd, fsck, rlogin, tip, etc. When paired with Rsync, a utility that provides fast incremental file transfer, it can automate the process of backing up files. This combination is particularly useful when dealing with large amounts of data or when needing to perform regular backups. It reduces the risk of human error, saves time, and ensures that your data is safely stored and up-to-date.

How can I handle errors during the backup process?

When using Expect and Rsync, errors may occur due to network issues, incorrect commands, or problems with the files themselves. To handle these, you can use the “expect” command to anticipate certain responses or errors. If an error occurs, you can program Expect to take a specific action, such as retrying the command, logging the error, or even sending an alert.

Can I use Expect and Rsync for backing up remote servers?

Yes, Expect and Rsync can be used to backup remote servers. Rsync allows for the transfer of files between different servers, while Expect can automate the login process. This means you can set up a script to automatically log into a remote server, initiate a backup with Rsync, and then log out, all without manual intervention.

How can I ensure the security of my data when using Expect and Rsync?

Rsync uses the SSH protocol for data transfer, which provides a high level of security. However, to further enhance security, you can use Expect to automate the process of entering passwords, reducing the risk of exposure. Additionally, you can use Rsync’s built-in encryption to protect your data during transfer.

How can I use Expect and Rsync to backup specific directories?

You can specify the directories you want to backup in the Rsync command. For example, if you want to backup the /home and /etc directories, you would use the command “rsync -av /home /etc /destination”. Expect can then be used to automate this command, running it at specified intervals or in response to certain triggers.

Can I use Expect and Rsync to backup to an external hard drive?

Yes, you can use Expect and Rsync to backup to an external hard drive. You would simply specify the path to the external drive as the destination in the Rsync command. Expect can then automate this process, ensuring that your backups are regularly updated.

How can I schedule backups using Expect and Rsync?

You can schedule backups by using a cron job in conjunction with Expect and Rsync. A cron job is a time-based job scheduler in Unix-like operating systems. You can set up a cron job to run your Expect script at specific times or on specific days, automating your backup process.

Can I use Expect and Rsync for incremental backups?

Yes, one of the key features of Rsync is its ability to perform incremental backups. This means that after the initial backup, subsequent backups only transfer the changes made since the last backup. This makes the backup process faster and more efficient, especially for large amounts of data.

How can I restore my data from a backup made with Expect and Rsync?

To restore your data, you would use the Rsync command to transfer the files from the backup location to the desired restore location. If you have used Rsync’s incremental backup feature, you can restore to any point in time that a backup was made.

Can I use Expect and Rsync to backup multiple servers?

Yes, you can use Expect and Rsync to backup multiple servers. You would need to set up an Expect script for each server, specifying the login details and the Rsync command for the backup. These scripts can then be run manually or automated using a cron job.

Blane WarreneBlane Warrene
View Author
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week