The Linux Page - backup files and directories

Back to the Linux Page

How to back up files and directories to another computer on the network ... and have it done automatically

In the course of my many Linux projects, the one I always seemed to put on the back burner was setting up an automated backup process that would copy my files and directories to another server. I paid the price when the hard drive crashed on my main file and print share server, and I spent over 30 hours trying to recover data and rebuild the server. That was enough. I researched and set up a system that would perform a daily backup to another server. And now, I present a step by step method to make a simple routine backup.

This tutorial was created on 9/4/07

Note: This instruction set is a guidline. It has been tested with Fedora Core 6 and will work, but the reader is encouraged to make changes to fit his/her tastes

In this tutorial, we will be setting up a backup from computerstore to computerbackup with a user named backupuser

Before starting, make sure you have these programs installed: ssh, cron, rsync

Create user named backupuser to all computers involved in the backup process

	# adduser -m backupuser

	# passwd backupuser

and enter password

Verify that the files that the bkuser group has permissions to access the directories and files being backed up. In my case, a few of the files and directories were accessible to user-myuser and group-myuser. So I made the following change:

	# usermod -G myuser backupuser

To see what groups backupuser is a memeber of:

	# groups backupuser

Then, login as backupuser to the computer where the files will be backed up to
Test the rsync by copying the directory foo and its contents from computerstore to computerbackup:

	$ rsync -aogvz -e ssh backupuser@computerstore:/path/to/foo /home/backupuser

If successful, continue by configuring computerbackup while logged in as backupuser

	$ mkdir /home/backupuser/backupstuff
	$ ssh-keygen -t dsa -b 1024 -f /home/backupuser/backupstuff/backupkey

When prompted for the passphrase, I just hit Enter to leave it blank

There are now two new files in /home/backupuser/backupstuff:
backupkey (the identification file) and (the public key file)
Verify/change permissions so that only authorized users have access to the identification file

The next step will be to copy the file to computerstore using the scp command (secure copy)

	$ scp /home/backupuser/backupstuff/ backupuser@computerstore:/home/backupuser

Now, either go directly to


or ssh into


From there, verify you are in /home/backupuser:

	$ pwd

If not, then change to that directory
Make an .ssh directory and an authorized_keys file, if they don't already exist.

	$ if [ ! -d .ssh ] then mkdir .ssh ; chmod 700 .ssh 
	$ mv .ssh
	$ cd .ssh/
	$ if [ ! -f authorized_keys ] then touch authorized_keys ; chmod 600 authorized_keys 
	$ cat >> authorized_keys

As a security measure, I've also disabled ssh access from root by editing /etc/ssh/sshd_config. It is not required, but I highly recommend it.

	#PermitRootLogin yes

Is changed to

	PermitRootLogin no

Now, exit from


and go back to


and test rsync with option of using key instead of password

	$ rsync -aogvz -e "ssh -i /home/backupuser/backupstuff/backupkey" backupuser@computerstore:/path/to/foo /home/backupuser

If successful, then it's time to create a script for this process to be automated

RUSER=backupuser  # Username being used on computerstore
RPATH=/path/to/foo # directory of what's being backed up 
LPATH=/home/backupuser # directory to store files to


Save the script to something obvious, like
In my own case, I also moved it to /home/backupuser/backupstuff so as not to lose it in all the files and directories that will be copied over
Then make it executable

	$ chmod a+x

Now, edit the crontab file

	$ crontab -e

and insert the following line:

0 2 * * 0 /home/backupuser/backupstuff/

This line tells the computer to run the at 2AM every Sunday.  The order of the line of numbers is as follows:

0=minute 2=hour (in a 24 hour clock) *=day *=month 0=day of week (where 0 is Sunday)

Save and close the script by hitting the Esc key, then type :wq
Your script is now set to back up files automatically

Please report any errors to

Credits go to the author of the tutorial "BACKUP: USING RSYNC AND SSH" as found at

Back to the Linux Page