You are here

How to backup a web site


To backup a web site on a periodic basis (say once per night), onto my own computer, running Linux. Web site computer runs Linux as well.

Domain name of the web site computer is supposed to be Web site files are supposed to be in /var/www/website directory.

Backup directory on my own computer is supposed to be /home/myself/backup.



We will use rsync over ssh to copy files from the web site to the local computer. ssh will be configured to use the public/private keys authentication mechanism, so that no password will be used.

The rsync command will be run every night thanks to cron.

This method is largely inspired by this article (additionnally, this article describes how to replicate a mySQL database...).

Detailed steps

  • on the computer running the web site, create the unprivileged user backup. Add it to www-data group (if the web server uses this group).
  • test that rsync works:
rsync -avz -e ssh /home/myself/backup/website/
  • on local computer, create an ssh key pair. Do not set a passphrase (think twice at security consequences for your local computer).
sudo mkdir /root/rsync
sudo ssh-keygen -t dsa -b 1024 -f /root/rsync/mirror-rsync-key
  • move the public key to the web site computer:
sudo scp /root/rsync/
  • on the web site computer, create the file authorized_keys, under backup username:
mkdir .ssh
chmod 700 .ssh
mv .ssh/.
cd .ssh/
touch authorized_keys
chmod 600 authorized_keys   
cat >> authorized_keys
  • add following clauses at start of authorized_keys file contents, so that only rsync connections will be accepted. Separate those clauses from what is already in the file, using a space character:
  • create file ~/rsync/checkrsync, with following contents:
                echo "Rejected"
                echo "Rejected"
                echo "Rejected"
                echo "Rejected"
                echo "Rejected"
                echo "Rejected"
        rsync\ --server*)
                echo "Rejected"
  • set up its protection:
chmod 700 ~/rsync/checkrsync  
  • on local machine, add following line into super user crontab  (sudo crontab -e):
00 05 * * * /usr/bin/rsync -avz --delete -e "ssh -i /root/rsync/mirror-rsync-key" \ \

This will start the rsync copy every night, at 5 AM.