Last modified: 17 April 2007 Backing up office and lab Linux computers
There are many ways to do this with Linux. This simple example will show how to maintain a mirror copy of local files (such as a /home directory) on userbackup using ssh and rsync with scheduling handled by cron. This example will also require that you have administrative access to your machine (though, very similar steps can be used by a normal user).
Configuring SSH for Automatic Logins
First, we need to make sure that ssh is configured to login to the Physics machines without requiring a password. If we do not do this, then each time the script to perform the backup is run, it will ask for your Physics account password. (If you plan to do your backups by hand, then this step is unnecessary.)
Follow these steps to allow the root user to connect to the Physics machines. Note: The comments in bold do not need to be typed.
[user@localhost ~] su - (First, let's become root)
Password: (enter root's password)
[root@localhost ~]# ssh-keygen -t rsa (start to generate ssh keys)
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): (just press enter)
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): (just press enter)
Enter same passphrase again: (just press enter)
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
b2:cb:67:96:e0:8c:b7:ea:16:12:b4:7b:fe:28:5e:1b root@localhost.localdomain
[root@localhost ~]#
Now the ssh keys have been created for root. Next thing to do is to copy the public key over onto our account on the Physics machine. I like to do this by opening two xterms and cutting and pasting with the mouse, but it can also be done by copying the public key file over. We'll cover both techniques.
If you still have your terminal open with the root account active, change into root's .ssh directory (/root/.ssh)
[root@localhost ~]# cd .ssh
[root@localhost .ssh]# ls
id_rsa id_rsa.pub
[root@localhost .ssh]#Using your favorite text editor (I like vim), open the id_rsa.pub.
[root@localhost .ssh]# vim id_rsa.pub
Just leave that window open for now. Now open up another terminal and ssh using your Physics username/password to one of the physics machines (let's use vminteractive). Once on vminteractive, move into your .ssh directory. If you do not have one, then create it.
physuser@vminteractive:~$ cd .ssh
-bash: cd: .ssh: No such file or directory (if this happens, create it)
physuser@vminteractive:~$ mkdir .ssh
physuser@vminteractive:~$ cd .ssh
physuser@vminteractive:~$ vim authorized_keys2 (we'll copy the public key here)
Now just copy the key from the root window over to the file on vminteractive (highlight with mouse then paste with middle mouse button). Now, if all goes well, the authorized_keys2 file will have one long line containing your public key. If it is broken up into several lines, then they will have to be joined (just remove the carriage returns, or use Shift-J in vim to join.)
Another way is to copy the file over to vminteractive (let's say it's just in your directory). Make sure the .ssh directory exists, and run this command: Note that there are 2 ">" signs together, or >> and that the switch for head is the number 1
physuser@vminteractive:~$ head -1 id_rsa.pub >> ~/.ssh/authorized_keys2
physuser@vminteractive:~$ chmod 600 ~/.ssh/authorized_keys2
If all went well, you should be able to log into vminteractive as physuser (or whatever your Physics account name is) from the root account on your local machine and not be prompted for a password. For example,
[root@localhost ~]# ssh physuser@vminteractive.physics.tamu.edu
Linux vminteractive 2.6.13 #1 Fri Sep 16 09:46:15 CDT 2005 i686
vminteractive.physics.tamu.edu
*************
* IMPORTANT *
*************
If your home directory is not mounted properly when you login (i.e., you
can't see your files), before contacting Computer Support, please run:
'smbldap-passwd' and update your password. It's alright if you use the
same password as before.
If this does not fix your problem, please visit:
https://www.physics.tamu.edu/tasks
and enter a task detailing your problem. If for some reason you are
unable to access this page, you may call 979-845-1379 (but be warned-
this phone is not always manned)
Last login: Tue Apr 3 11:17:52 2007 from somewhere.physics.tamu.edu
physuser@vminteractive:~$
Using rsync to Synchronize files
rsync is useful for syncing file systems, directories, etc. since it is so easy to use and very fast.
Make sure that you have rsync installed. It is commonly installed with major distributions. This example will make a copy of /home on userbackup. Using your favorite text editor, create a script to run rsync. Here is a simple one that will copy all of /home. (Note, ArchiveDirName will be provided by the Computer Support Group when you request access to the backup machine)[root@localhost .ssh]# vim my_backup.sh
#!/bin/sh
/usr/bin/rsync -aqz -e ssh /home physuser@userbackup.physics.tamu.edu:/mass/ArchiveDirName/
Save that file, and make it executable (chmod 744 my_backup.sh). Now that file can be run, and it should make a copy of /home over onto userbackup. If you wish to test the script first, to see if it will be successful, change the switches on the rsync command from -aqz to -avzn. This will tell you what rsync will do without actually doing it. (Just be sure to change it back once you are happy.) There are many options for rsync, please consult the man page, rsync(1), to see what is available.
To have your machine sync files automatically, copy the script (as root) to your /etc/cron.weekly directory (the location of this directory may be different your Linux distribution). cron will then run this script once a week as defined in /etc/crontab.