Create an Automatic Backup When You Logout

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:

ls -la

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:

source .bash_profile

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.

Logout Script
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:

mkdir /bk/root_bk

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: