Customize the User Environment
The environment is comprised of a series of settings that provide a look and feel that the user is comfortable with or that the corporation deems necessary to create a productive work setting. Bash provides systematic ways to setup an environment that will meet your needs to help you function more effectively. One of those settings should be some kind of automatic backup when you logout for the day.
There are three files that are read when a user logs in and out. These files are typically in the user directory by default when using the bash shell. The files are hidden files so they are preceded by a “.” and may be viewed by using the command:
The files are .bash_profile, .bash_logout, and .bashrc. If these three files do not exist in your home directory they can be created by using a text editor like vi.
You may also see .bash_history which keeps a list of the commands that the user has executed.
Any changes you make to this script must be saved and then you must logout and login again. However it is possible to use the source command which will run all of the commands that are in the script:
Now there is a catch, bash provides a way for a system to use two alternative files other than .bash_profile, these files are .bash_login or .profile. If either of these files is listed and .bash_profile is not then they will run, but if .bash_profile is listed it will run.
The .bash_profile is read by the system and executes any command located there only when a user logs into the system, and not when a user starts a new shell. When the user starts a new shell .bashrc file is read. This setup allows the user to separate the commands needed at startup and those that may be important when starting a subshell.
The .bash_logout provides a way to execute commands when the user logs out of the system. One useful application for this in a user’s home directory is that an administrator can provide a way to kill all of the user’s applications when logging out just in case an application was not closed correctly or that hangs.
Here is an example of a simple command that copies all OpenOffice Writer files to a USB device in a directory called MyDocs when the user logs out.
Create a file named .bash_logout in the users home directory if it has not been created already. In that file use this command in a line and save the file:
cp Documents/*.odt /media/usb/MyDocs
Note the case and that since the command will be issued from the user’s home directory the path is simply the Documents directory. Of course this command will require the user to save all the Writer files they want to copy into the Documents directory or they will not be copied. You will also need a directory called MyDocs in the USB device.
This script will rsync the root user’s home directory to a local backup.
Open .bash_logout or create it with vi .bash_logout in the root user’s home directory. Create a backup directory with:
Of course this location should be on a separate drive and you will need permissions to access that drive. It could be a USB drive that is mounted as /bk.
Enter this line of code in the .bash_logout:
rsync -avz –exclude=*.rpm /root/ /bk/root_bk
chmod -R 700 /var/root_bk
Test the script by logging out and then login in again.
Simple script but you get the idea. Placing an automatic backup in that logout script will be worth gold if you have problems with your drive.
The rsync command will create an exact copy of your information in a new location based on what has changed. It takes a look a timestamps to determine what file have changed. This synchronization process is a reliable way to keep two directories exactly the same.
The Trailing Slash
It is important to understand the difference in use with or without the trailing slash.
rsync -av /home/office/ /bk
This example does not move the office directory folder only the contents.
rsync -av /home/office /bk
This example will result in the office directory located under the /bk = /bk/office.
Note the differences and choose the method you want to use for your backups.
If you want to make your transfer smaller then use the -z option to compress your transfer.
rsync -avz /home/office/ /bk
Archive Mode -a
This will ensure that all symbolic links, devices, attributes,permissions, ownerships,etc. are preservered and transferred. This is the same as rlptgoD
This will provide you with information.
Get Stats on Transfer –stats
rsync -avz –stats /home/office/ /var/bk
Here is an example of the stats you will have available.
Number of files: 324352
Number of files transferred: 70613
Total file size: 20610660916 bytes
Total transferred file size: 11504455981 bytes
Literal data: 11504455981 bytes
Matched data: 0 bytes
File list size: 17670110
File list generation time: 37.718 seconds
File list transfer time: 0.000 seconds
Total bytes sent: 11527010557
Total bytes received: 1887490
sent 11527010557 bytes received 1887490 bytes 21212323.91 bytes/sec
total size is 20610660916 speedup is 1.79
File size Transferred
Exclude a Pattern
rsync -avz –exclude=*.rpm –stats /home/office/ /var/bk
Linux Training Packages CLICK HERE
Create an exclude file.
If you have a number of extensions you want to exclude, create a file and then tell rsync where the file is so that these extensions or patterns will not be transferred.
rsync -avz –exclude-from=/home/office/exclude –stats /home/office/ /bk
Here the file containing the patterns is listed by using –exclude-from= followed by a path to the file. As you can see the patterns in the file are not transferred.
home/office/exclude file contents, one pattern on each line.
This script will sync the /home where user mail is kept to a directory on a separate drive. Once the file is synced then it will be backed up with a tar command in the example above.
/usr/bin/rsync -a – -verbose – -stats /home /bk/home
if [ $? -ne 0 ]; then
Crontab By using crontab -e you will be able to setup cron jobs that will run the two scripts above as often as you need.
17 8-17 * * * /root/rsync.sh
47 8-17 * * * /root/bk.sh
If you created a script like this, below is the output.
/usr/bin/rsync -a –verbose –stats /home /bk/home
Number of files: 8195
Number of files transferred: 7376
Total file size: 6677491116 bytes
Total transferred file size: 6677490762 bytes
Literal data: 6677490762 bytes
Matched data: 0 bytes
File list size: 218205
Total bytes sent: 6678839831
Total bytes received: 147540
sent 6678839831 bytes received 147540 bytes 12045062.89 bytes/sec
total size is 6677491116 speedup is 1.00