martes, 21 de junio de 2011

Archivos de Configuracion

Que onda!, regreso por aquí con otra cosilla que se me ocurrio gracias al proyecto que estoy haciendo en el jale. Resulta que pronto voy a tener que formatear mi server y ponerle todo de nuevo. Todo porque no me preocupe en el principio por particionar bien el disco, ahora para hacer que trabaje bien el sistema de backups le asignare varios discos dedicados con su propia particion y puestos en raid, pero antes de formatear decidí que tenía que hacer un backup de mis archivos de configuración mas importantes para no batallar tanto cuando reinstale RedHat así que me avente un scriptcillo para hacerlo. El script graba los archivos de configuración que a mi juicio son los mas importantes y/o que he modificado y también los restaura. Se lo dejo aquí ojala les sirva.


UPDATE: En este link esta el post con la nueva version del script que es mucho mas facil de usar, eficiente y util.
Básicamente lo usas de esta manera:

#./configmanager configFilesFolder

Luego el script te pregunta que quieres hacer, si un Backup o un Restore, el parametro que le pasas es el folder en donde quieres que se graben tus archivos de configuración o del cual los leera. Probablemente mas adelante haga una versión que no necesite el parametro y que haga un fallback hacie el current directory pero por lo pronto necesita que le pases ese parametro.

Si le dices que haga Backup (B) grabara todos los archivos de la lista que estan en el script y los pondra en el folder que le pasas de parametro.

Si le pides Restore (R) usara los archivos del folder para pasarlos a su debido lugar.

Dependiendo de que distro de Linux usemos los paths cambian, por ejemplo en Debian y sus derivados los archivos de configuración de Apache se encuentran en /etc/apache/conf/ en lugar de /etc/httpd/conf pero eso se puede modificar fácilmente, bueno aquí les dejo el script ojala les sirva =).


#!/bin/bash

#
# This script takes care of Backup configuration files, restore them, 
# and then reload the services this config files belong to
# this script is valid for Red Hat based distros it may not work 
# on other distros cause of the diference in commands but it can 
# be easely changed to fit any distro
#
# Jorge A. Moreno 
#
# morenog.jorge[at]gmail.com
# fixed-term.jorge.moreno[at]us.bosch.com
#

 #
 # This function copies all config files to an output directory that must be passed by the user
 # it also takes cares of preserving as many attributes as posible to ease resoration easier
 # this way it keeps the same permissions the held while backed up
 #
 function backup 
 {
  cp --preserve=all /etc/httpd/conf/httpd.conf $1
  cp --preserve=all /etc/sysconfig/vncservers $1
  cp --preserve=all /etc/samba/smb.conf $1
  cp --preserve=all /etc/BackupPC/config.pl $1
  cp --preserve=all /etc/httpd/BackupPC.users $1
  cp --preserve=all /etc/init.d/backuppc $1
  cp --preserve=all /etc/sysconfig/network-scripts/ifcfg-eth0 $1
  cp --preserve=all /etc/sysconfig/network-scripts/ifcfg-eth1 $1
  cp --preserve=all /etc/sysconfig/network-scripts/ifcfg-bond0 $1
  cp --preserve=all /etc/ssh/sshd_config $1
  cp --preserve=all /etc/hosts $1
  cp --preserve=all /etc/hosts.allow $1
  cp --preserve=all /etc/hosts.deny $1
  cp --preserve=all /etc/sysconfig/iptables $1
  echo "Backup completed!"
  echo "Files have been dumped in: "$1" "
  exit
 }

 #
 # This function restores all previously saved config files to their default directory.
 # It takes the argument directory as source to look for the configuration files
 # to be restored, then if found, these are restored to their defaul locations.
 # 
 function restore 
 {
  echo "This Will Overwrite your current config files! Are you sure?? (Y/N)"
  read uSure
  echo "***********"
  if [ "$uSure" == "Y" ] || [ "$uSure" == "y" ]
  then
   \cp --preserve=all $1/httpd.conf /etc/httpd/conf/httpd.conf
   \cp --preserve=all $1/vncservers /etc/sysconfig/vncservers
   \cp --preserve=all $1/smb.conf /etc/samba/smb.conf
   \cp --preserve=all $1/config.pl /etc/BackupPC/config.pl
   \cp --preserve=all $1/BackupPC.users /etc/httpd/BackupPC.users
   \cp --preserve=all $1/backuppc /etc/init.d/backuppc
   \cp --preserve=all $1/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-eth0
   \cp --preserve=all $1/ifcfg-eth1 /etc/sysconfig/network-scripts/ifcfg-eth1
   \cp --preserve=all $1/ifcfg-bond0 /etc/sysconfig/network-scripts/ifcfg-bond0
   \cp --preserve=all $1/sshd_config /etc/ssh/sshd_config
   \cp --preserve=all $1/hosts /etc/hosts
   \cp --preserve=all $1/hosts.allow /etc/hosts.allow
   \cp --preserve=all $1/hosts.deny /etc/hosts.deny
   \cp --preserve=all $1/iptables /etc/sysconfig/iptables
   echo "Restore completed!"

   #We better ask if the service are to be reloaded/restarted before doing it =P
   echo "Shall I reload/restart modified services? (I will restart those I cannot reload)"
    if [ "$uSure" == "Y" ] || [ "$uSure" == "y" ]
    then
     reloadServices #reload/restart services
    else
     leave #do not reload or restart better to leave
    fi
  else
   leave
  fi
 exit
 }
 
 # Only prints a friendly message and then leaves
 function leave 
 {
  echo "Ok leaving!"
  exit
 }
 
 # Used to output an error when no source/output directory was given, the script
 # needs a parameter from which it will read the config files (if restored) or to stored them (if backed up)
 function noParams 
 {
  echo "You need to pass a source/output directory"
  exit
 }

 #
 # This takes cares of reloading the services which configuration files we restored
 #
 # Note that some services cannot be reloaded, they must be Restarted! meaning they will
 # stop even if it is for a few seconds, so this script reloads the services it can and
 # those it cannot are restarted.
 #
 function reloadServices 
 {
  echo "Reloading services!"
  service backuppc reload
  service httpd reload
  service network reload
  service smb reload
  service iptables restart
  exit
 }

 #
 # This is the main meny and the messages the user see when running the script, from
 # here, other functions are called to perform work
 #

 echo "I will dump files in: "$1" or look for them in there"

 # Little checking to make sure params where given if not quit!
 if [ -n "$1" ]; then
  {
  # Selection menu, what do you want to do?
  echo "What do you want me to do? Backup?(B), Restore(R), or Quit(Q)"
  read ans
  echo "***********"
  if [ "$ans" == "B" ] || [ "$ans" == "b" ]
  then
   backup $1 # Calling backup function
  elif [ "$ans" == "R" ] || [ "$ans" == "r" ]
  then
   echo "Im pulling all config files from: "$1" "
   restore $1 # Calling restore function
  elif [ "$ans" == "Q" ] || [ "$ans" == "q" ]
  then
   leave # Calling leave function, quiting!
  else
   echo "Not recognized!" # Error messaged displayed when an invalid key was hit
   exit
  fi
  }
 else
  {
  noParams # No params were given, cannot do work without them!
  }
 fi
 exit


No hay comentarios:

Publicar un comentario

Deja tu comentario aquí, cuenta que te parecio el artículo o simplemente saluda!