Automating Server Backups Using "rsync/ssh/cron" on a Windows or Linux Machine

Everyone knows backups are important, not just for restoring from a system failure, but also for fixing user/admin/developer mistakes. Here's an efficient way to automatically backup files on your Linux/CentOS server using a spare Windows or Linux machine. You should already have a basic grasp of the Linux shell prompt for this guide.

Installing Cygwin
(You can skip this if you're backing up on a Linux machine.)
The first task is installing Cygwin on your Windows box, which provides a Linux-like environment for running our backup tools (rsync+ssh and cron). When running setup.exe, you should select as many packages as you feel comfortable installing by clicking install arrows. Be sure to install all of "Admin," "Base," "Devel," and "Net." You can always run setup.exe later to update your existing Cygwin installation.

Creating SSH Public/Private Keys
Since we want to copy our files securely over SSH without being prompted for a password each time we login, we need to generate a public key for our backup machine. At the shell prompt, enter:

BASH
ssh-keygen -t dsa

Ignore the questions by pressing "Enter" at any prompts, such as the file location or passphrase. Now open ~/.ssh/id_dsa.pub (e.g., c:/cgwin/home/(Windows Username)/.ssh/id_dsa.pub) file and copy the contents. Log in to your remote/Linux machine and paste this info into the file ~/.ssh/authorized_keys. If the file doesn't exist, just create a new one. You must also modify your SSH daemon to use allow Pubkey authentications. Edit /etc/ssh/sshd_config and uncomment:

    RSAAuthentication yes
    PubkeyAuthentication yes

On your backup machine, test your pubkey by typing (replacing username and remotehost):

BASH
ssh username@remotehost

You should now be able to login into your Linux server without typing in a password!

The rsync Backup Script

"rsync is a software application for Unix systems which synchronizes files and directories from one location to another while minimizing data transfer using delta encoding when appropriate. rsync can copy or display directory contents and copy files, optionally using compression and recursion." –rsync on Wikipedia

In other words, rsync is one of the best backup tools available.

Create your backup directory:

BASH
mkdir /rsync

And create your backup script inside it, named backup.sh, making sure to replace username@localhost:

BASH
#!/bin/bash
rsync -avz -e ssh username@remote:/var/www/ /rsync/www/ --log-file=/rsync/rsync.log

/var/www/ is the full path on the remote machine. /rsync/www/ is where to save on your backup machine. If you use a non-standard SSH port, you can use "ssh -p portNum" in quotes.

You can also backup your MySQL database files, log files, code repositories, or whatever else you desire. If you don't have access to the MySQL files directory, you can setup a mysqldump script on the remote machine to save to a file.

You may also want to create "snapshots" of your backups, so these files don't get overwritten by unwanted changes. Here's a snapshot script (snapshot.sh) that gzips MySQL database files:

BASH
#!/bin/bash
archivedate=$(date +%Y-%m-%d)
tar cvf - /rsync/mysql | gzip  > /rsync/archive/mysql_$archivedate.tar.gz

Cygwin: Installing the "cron" service
Crontabs are used for automating our backup scripts. On Cygwin, cron must be setup as a Windows service in order to run in the background:

BASH
chmod +x /bin/cygwin1.dll
mkdir /etc/cron.d
cygrunsrv --install cron -p /usr/sbin/cron -a -D
cygrunsrv --start cron

To see if the cron service is running, type:

BASH
ps -a | grep cron

Adding the Crontab:
On your backup server, type:

BASH
crontab -e

Press "i" and add:

BASH
0 0 * * * /rsync/backup.sh

Press "ESC" then "w" and enter to save.

This will run the backup script at midnight every day. You can also add a line for the snapshot.sh script... replace the first "0" with "30" to run the script 30 minutes later.

That's it! Now just make sure your backup server has your hard drives in a RAID 1 (mirroring) or better configuration for additional data redundancy!

Comments

many thanks

Many thanks for your help.

Covering this and cygwin helped me a lot.

Sometimes cygwin doesnt want to start as a service so i used this:

http://www.davidjnice.com/articles/cygwin_cron-service.html

Regards

What is worse, many developers

What is worse, many developers oakley minute take the golf course construction as by the massive circle place, then in the field peripheral development real estate project, lead the house price soil-rent value rise with the golf course, the creation multiple benefits. Therefore Local authority often no matter not only golf course's illegal construction question, instead treats as the achievements project, even invites the developer investment construction golf. In contrary construction golf course tide, but also has many experts to wave the flag beats a drum. The Beijing Forestry University golf education which and Research center director, international lawn academic society President Professor Han Liebao appears in many reports is. Professor Han once declared the approximately ten years ago that the accusation golf course pollution of the environment, the water consumption serious have not rested on, but the present actually starts in the interview to popularize the golf course water consumption to have how many. He also thought in the past Beijing's cheapest oakley sunglasses golf course are too few, needs 310 at least.

Divides in india while in the tresses

Divides in india while in the tresses ghd limited edition pink orchid (middle line), holds back again lightly using the hand bulb, nearby two holds back again small toward about, can make 23.Holds back again the scalp lightly using the hand bulb. From sends the pathline to begin first, gradually towards the best in the head, will be the back again in the head. Repeatedly can make six times.Makes the comb using the finger, passes sends, can make about ten times.The finger seem of some thing getting struck or slipping towards the soil ratatat knocks about the whole forehead lightly, brings about the scalp to relax.  The tresses which prior to resting, spreads exempts washes the tresses tonic which could the fix and upkeep endure injury dry, every single 1 week does two to 3 results is good.Before heading towards the swimming swimming pool or even the beach, wipes sunshine obstruct lotion about the tresses or consists of towards exposes towards the sunshine the ingredient the tresses tonic. this sort of does can allow defend sends the ingredient to penetrate properly to sends while in the silk.If you wish to stroke along yours organic volume, may perhaps mail the rubber to spurt, while in the denticle combs or about the cleanse toothbrush, from sent the border collection to begin to comb the tresses downward, the tresses may perhaps preserve ghd pink orchid australia is ideal straight.If the tresses suffers injury seriously, prior to producing indepth safeguards sends the evening to become feasible to mail the tree best location to smear many drops to possess the moisturizer purpose olive oil.Many shampoos consist of defend mail the ingredient, but these elements will allow the roots in the tresses cling the scalp, soon after the mind will turn into dry, will not possess the stereoscopic effect. might much better pour the shampoo while in the palm, then wipes in sends about the silk.Main stage one: can make “willowy” brideLet luxuriant just a little curl, not just romantic transferring also facilitates blows entire, this will be the most appropriate choice. However, remembers surely, prior to possessing a long-lasting ghd limited edition pink orchid wave the perform which, should carry on to perform initial safeguards sends, like this, features a long-lasting wave once the fluid medications will straight only then not corrode hair's wool lamella, will defend the tresses Health.Main stage two: should spend consideration to washes sends once the skill

Washes sends when with betroths youngsters

Washes sends when with betroths youngsters ghd colores prior to they are born massages the scalp, the massage therapy time is slightly extended a spot, such as this may perhaps stimulate the scalp blood vessels circulation, facilitates the roots in the tresses to absorb various moistens the ingredient;Skill 2: soon after washing sends, utilizes guarantees the humidity large the tresses tonic, special moistens the product to become capable to become connected towards the tresses surface, offers protective film, the wetness articles which as properly as the nutrient the tresses necessities cannot drain easily.Skill 3: absolutely soon after washing sends, should make utilization of the tresses tonic, the tresses tonic efficient element can bring about the tresses semblance activeness molecular orientation arrangement, is decreased hair's textile dietary fiber electric powered charge, the resistance reduces, types antistatic electrical power the protective movie to bring about the tresses to moisten, soft, along slides.Main stage three: prior to should spend amazing consideration the wedding ceremony ceremony, to nurseThe tresses can also be actively playing the quite key part while in the whole bridal modelling, how various mins charm can it improve for you, waits the fabulous quite small girls who marries might how to produce the tresses maintenance, can turn into the fabulous splendid 10 thousand human being of focal points ghd edicion limitada?

straight forward and perfect!

Thank you so much for this tutorial!

I needed to get a regular data update from a remote linux server to to local windows server.
I tried DeltaCopy but I couldn't make it work the way I needed to and since I usually work only with linux I felt pretty helpless suddenly being confronted with windows again.
End then I found your simple, yet perfect solution!

Many, many thanks!

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: [code], [blockcode], [bash], [css], [html], [ini], [javascript], [mysql], [php], [sql], [xml]. PHP source code can also be enclosed in <?php ... ?> or <% ... %>.

More information about formatting options