Linux: File Backup From Linux To Linux
by Victor | August 12, 2008 in Linux | No Comments
I was given the task of setting up a nightly back up of files located on one Linux Machine (Red Hat) to another Linux Machine (Red Hat). All the files requiring backup are located in one folder. This folder contains both files and folders (which can contain more files and folders). Each files is only very small (couple bytes each) and once a file is created, it is never changed or edited).
Having not a very clear idea on how to approach this task, I began to experiment and look for various other methods. My searches only gave way to many applications specializing in performing backups, none of which I really wanted to implement.
I realized that if I mounted the folder containing files from the host machine to the backup machine, and then performed a simple cp command. After running man cp, I came across the following command that would accomplish what I needed. Therefore, after mounting the folder as a directory, I do all these changes the Backup Machine.
cp -upR --reply=yes /mountedDirectory /backupDirectory
The following is a breakdown of what each command means.
-u
copy only when the SOURCE file is newer than the destination file or when the destination file is missing
-p (same as –preserve=mode, ownership, timestamp)
preserve the specified attributes (default:mode,ownership,timestamp) and security context, if possible attributes: links, all
-R
copy directories recursively
–reply={yes, no, query}
specify how to handle the prompt about an existing destination file.
During the testing phase, I found that the first execution took the longest (above six minutes), but each susequent execution was shorter (about one minute). I figured that most of the time spent during the first execution was due to many copy operations where most of the time spent during each subsequent execution was comparing.
The next step was to put my single line command into a script file. This is done by creating a *.sh file. I decided to put this file where the rest of these files live, which is mainly /bin. Create a new file with the following format and give it a name. In this example, we are using nightlyBackup.sh.
#!/bin/sh
cp -upR --reply=yes /mountedDirectory /backupDirectory
Next, navigate to the files and change the chmod. We want to chmod to 777 to give the owner read, write, and execute priveliges. The group and others will get read and execute preiveliges. This can be accomplished by:
chmod777 nightlyBackup.sh
The next step to get this simple singe line script to run nightly. This I found could be accomplished by running a crontab. A crontab is a linux application that will run scripts at specified times (similiar to schedulers in Windows)
crontab -l will list all scripts set for execution.
crontab -e will bring up a prompt to add scripts.
Since I want mine to run sometime during the night each night, I set mine to by adding this to the file as a new line:
0 0 * * * /bin/nightlyBackup.sh
Save and close the file and now the script is set to run nightly. What is obvious in this setup is that both the host machine and the backup machine need to be up and running each night for this to work. Otherwise, this is a perfectly great solution that requires little setup and maintenance.




