Ubuntu: Backup files regularly using expect, rsync and Cron

Harry Lee

May 24, 2016

Want to backup your remote files at regular intervals? This post is for you.

This post focuses on: rsync, expect and cron. To access the remote server, I’m assuming that you are using ssh (with password authentication) for that. If you use ftp or other protocols, you will have to tweak the instructions a bit. If you are using SSH with public key (well done!), you will have to tweak the rsync command to use public key.

Background

The idea is to use rsync to copy files from the remote server to your local repository. Since remote servers are normally protected by passwords (or private-public key pairs), we need a way for the terminal to enter the password for us. This is where expect comes in.

Expect is a natural and intuitive automation scripting language that operates in much the same way humans do when interacting with a system.

So expect removes the need for human interaction when a password is needed. For a detailed way to use expect see Using Expect Scripts to Automate Tasks.

We then use cron to schedule automation of this task. See How To Cron.

Method

  1. Create a new file in ~/Documents/, backup.exp, with the contents below:

    #!/usr/bin/expect -f
        
        set timeout 19900
        set pass "<mypassword>"
        spawn rsync -av -e ssh <user>@<ip address>:</home/user/remote> <"/home/user/backup/remote">
        expect "assword"
        sleep 2
        send "$pass\r"
        expect -re "total size is"
        expect -re "$ "
        puts "Successfully backed up!"
        
    • replace all the necessary parameters with your own credentials. (Basically everything encased between < >).
  2. Change the permission of the file to be executable:

        $ sudo chmod +x ~/Documents/backup.exp
    
  3. Run cron:

        $ sudo crontab -e
    
  4. Place the following line at the end of the file.

        00 06,12,18,00 * * * /home/user/Documents/backup.exp
    

    This basically tells the server to execute the backup.exp script every 6 hours, every day at 6:00 AM, 12:00 PM, 18:00 PM and 00:00 AM. You can change the intervals however you see fit.

Conclusion

This is really useful if you have your own file server with files that are created/modified regularly. Backing up these files consistently and frequently can give you peace of mind that your files are “safer” than when you do not have this in place.