There are many ways to backup a site. There are remote storage solutions, WordPress plugins and more. They all have various advantages and disadvantages. Some of them incur a monthly fee, some of them are manual only and even fewer solutions offer any level of versioning.
So here’s a free, automated method to backup your site, including any databases. What you need is either a VPS hosting service or at least one that gives you SSH access. Your local computer can be Windows, Mac or Linux. I’ll write about Windows, since Linux users probably already know how to do this.
This method uses RSync for very efficient data transfer and SHH for secure connection.
Install CWRsync
- Download and install CWRSync. It’s a Windows port of the famous rsync program.
- Since you’ll need to call it from the command line, I suggest you add it to the Path (use Control Panel –> System –> Advanced System Settings –> Advanced tab –> Environment Variables, select the Path variable, Edit it and add
;C:\Program Files\cwrsync\bin
at the end).
Generate and install the RSA key pair
- Create a folder for your backup settings, could be anywhere, say
C:\backups
. - Shift-Click on this new folder and choose Open Command Window here.
- Type
ssh-keygen -t rsa -N '' -f mybackup.rsa
and hit enter. This will create a new private/public key RSA key pair that (for simplicity) will not be protected by a password; the private file will be calledmybackup.rsa
and the public one will bemybackup.rsa.pub
. - Now you must import the public key on your server. Put the file on the server using FTP/SFTP program of choice (I recommend WinSCP). Then you must copy the contents of the public key to
~/.ssh/authorized_keys.
The next script assumes you copied the key to your home dir.
mkdir ~/.ssh chmod 700 ~/.ssh cat mybackup.rsa.pub >> ~/.ssh/authorized_keys rm mybackup.rsa.pub chmod 600 ~/.ssh/*
Preparing the batch remote to local sync file
To automate the rync process, you will create a batch file. Make an empty text file called sync.cmd
in the same folder as the key pair.
It may look cryptic, but it’s not. I just created some variables (like this: %VAR%
) to make the code easier. This batch file does two things: First it does a mysqldump
on a database on a server and then copies the sql dump and site files locally.
For database dump, create a directory /var/sqldump
(or similar) on the server.
Put this into the batch file:
@ECHO OFF SETLOCAL SET CYGWIN=nodosfilewarning SET HOME=%~dp0 SET PATH=%HOME%\BIN;%PATH% REM the name of your private key, should be in the same folder as the batch file SET KEY=mybackup.rsa REM your username on the server, probably root and the domain name or ip address SET USER=[your username] SET DOMAIN=[your domain] REM remote folder to backup SET REMOTEPATH=/var/www REM remote location of the database dump SET REMOTESQLDUMP=/var/databases REM local path for backups - you must leave 'cygdrive' in place, replace '\' with '/', so "E:\Site\files" becomes "/cygrive/E/Site/files" SET LOCALPATH=/cygdrive/c/backups REM remote mysql username, password and database name SET MYSQLUSER=[your username] SET MYSQLPASS=[your password] SET MYSQLDB=[your db name] REM dump the database to file ssh.exe -i %HOME%\%KEY% %USER%@%DOMAIN% "mysqldump --skip-comments --user=%MYSQLUSER% --password=%MYSQLPASS% %MYSQLDB% > %REMOTESQLDUMP%/%MYSQLDB%.sql" REM copy the database dump rsync -rtze "ssh -i %HOME%\%KEY%" --progress --delete %USER%@%DOMAIN%:%REMOTESQLDUMP% %LOCALPATH% REM copy the files rsync -rtze "ssh -i %HOME%\%KEY%" --progress --delete %USER%@%DOMAIN%:%REMOTEPATH% %LOCALPATH%
If the output is 'ssh -i' is not recognized as an internal or external command, operable program or batch file
then you don’t have the Path set correctly.
You should see “RSA key fingerprint is [long hex string]. Are you sure you want to continue connecting (yes/no)?
“. Type “yes“.
Then you’ll get this message “Warning: Permanently added '[your domain ], xx.xxx.xxx.xxx' (RSA) to the list of known hosts.
” followed by the list of files being transferred.
At the end you’ll see something like
sent 129662 bytes received 3585741 bytes 63511.16 bytes/sec total size is 72572550 speedup is 19.53
Running the backup on a schedule
Although it’s beyond the scope of this article, at this point you can create a Task using Task Scheduler and make the batch script run every day at a given time to backup your server regularly.
Versioning
I won’t get in much detail, as this could be done as an entire article of its own, but I just want to point you in the right direction.
Versioning can be handled in different ways, depending on your needs. If all you need is daily archives but don’t need to always have direct access to the files, you could modify the batch file to zip all transferred files.
To archive, first install a commandline zip utility (my favorite is 7zip, free and very powerful). Then, set the computer date to be shown with dashes instead of slashes, preferrably something like YYYY-MM-DD. You could set the format from the batch, but since the date display is locale-dependent, it might not work. Then you add to the batch something like
pkzip c:\backups\*.* c:\tempzip.zip ren C:\tempzip.Zip c:\Archive_%date%.zip
The second method is to use the Volume Shadow Copy / Previous Version feature in the newer Windows versions (it’s especially well suited for Windows Server 2003/2008). If you set the Volume to create a snapshot every day, you’ll have a transparent versioning system at your fingertips (although you’ll still have to backup to external media).
One Response
Hello Armand,
I used the collapsible panels in a site “www.campus-trekking.com”, and they worked out well. Thanks for the code. I was wondering if you know how to back up a site made in google sites.
Best regards from Ecuador,
Javier
Comments are closed.