Saturday, December 14, 2013

How to Completely Backup and Restore Linux Mail Server

========
Concept:
========

Following files/dirs are required for traditional Linux user management:
* /etc/passwd - contains various pieces of information for each user account

* /etc/shadow - contains the encrypted password information for user's accounts and optional the password aging information.

* /etc/group - defines the groups to which users belong

* /etc/gshadow - group shadow file (contains the encrypted password for group)

* /var/spool/mail - Generally user emails are stored here.

* /home - All Users data is stored here.

You need to backup all of the above files and directories from old server to new Linux server.

================
Backup Procedure
================

Step 1:

First create a tar ball of old uses (old Linux system). Create a directory:
# mkdir /root/move/

Step 2:

Setup UID filter limit:
# export UGIDLIMIT=500


Users that are added to the Linux system always start with UID and GID values of as specified by Linux distribution or set by admin.
Limits according to different Linux distro:

    RHEL/CentOS/Fedora Core : Default is 500 and upper limit is 65534 (/etc/libuser.conf).
    Debian and Ubuntu Linux : Default is 1000 and upper limit is 29999 (/etc/adduser.conf).

Step 3:

Now copy /etc/passwd accounts to /root/move/passwd.mig using awk to filter out system account (i.e. only copy user accounts)
# awk -v LIMIT=$UGIDLIMIT -F: '($3>=LIMIT) && ($3!=65534)' /etc/passwd > /root/move/passwd.mig


Step 4:

Copy /etc/group file:
# awk -v LIMIT=$UGIDLIMIT -F: '($3>=LIMIT) && ($3!=65534)' /etc/group > /root/move/group.mig  [for RHEL/CentOS/Fedora ]
# awk -v LIMIT=$UGIDLIMIT -F: '($3>=LIMIT) && ($3!=29999)' /etc/group > /root/move/group.mig  [for Debian/Ubuntu ]

Step 5:

Copy /etc/shadow file:
# awk -v LIMIT=$UGIDLIMIT -F: '($3>=LIMIT) && ($3!=65534) {print $1}' /etc/passwd | tee - |egrep -f - /etc/shadow > /root/move/shadow.mig   [for RHEL/CentOS/Fedora ]
# awk -v LIMIT=$UGIDLIMIT -F: '($3>=LIMIT) && ($3!=29999) {print $1}' /etc/passwd | tee - |egrep -f - /etc/shadow > /root/move/shadow.mig   [for Debian/Ubuntu ]


Step 6:

Copy /etc/gshadow (rarely used):
# cp /etc/gshadow /root/move/gshadow.mig

step 7:

Make a backup of /home and /var/spool/mail dirs:
# tar -zcvpf /root/move/home.tar.gz /home
# tar -zcvpf /root/move/mail.tar.gz /var/mail
# tar -zcvpf /root/move/mail1.tar.gz /var/spool/mail

Step 8:
Use scp or usb pen or tape to copy or paste /root/move to a new Linux system.

Where,

    Users that are added to the Linux system always start with UID and GID values of as specified by Linux distribution or set by admin. Limits according to different Linux distro:
        RHEL/CentOS/Fedora Core : Default is 500 and upper limit is 65534 (/etc/libuser.conf).
        Debian and Ubuntu Linux : Default is 1000 and upper limit is 29999 (/etc/adduser.conf).

    You should never ever create any new system user accounts on the newly installed Cent OS Linux. So above awk command filter out UID according to Linux distro.
    export UGIDLIMIT=500 - setup UID start limit for normal user account. Set this value as per your Linux distro.

    awk -v LIMIT=$UGIDLIMIT -F: '($3>=LIMIT) && ($3!=65534)' /etc/passwd > /root/move/passwd.mig - You need to pass UGIDLIMIT variable to awk using -v option (it assigns value of shell variable UGIDLIMIT to awk program variable LIMIT). Option -F: sets the field separator to : . Finally awk read each line from /etc/passwd, filter out system accounts and generates new file /root/move/passwd.mig. Same logic is applies to rest of awk command.
   
    tar -zcvpf /root/move/home.tar.gz /home - Make a backup of users /home dir
   
    tar -zcvpf /root/move/mail.tar.gz /var/spool/mail - Make a backup of users mail dir

================
Restore Procedure
================

Step 1:
First, make a backup of current users and passwords:
# mkdir /root/newsusers.bak
# cp /etc/passwd /etc/shadow /etc/group /etc/gshadow /root/newsusers.bak

Step 2:
Create a directory in new Linux Server in which to be restored:
# mkdir /root/move/
paste all copied item to this directory from usb pen or tape like (group.mig, gshadow.mig, home.tar.gz, mail.tar.gz, passwd.mig, shadow.mig)


Step 3:
Now restore passwd and other files in /etc/
# cd /root/move/
# cat passwd.mig >> /etc/passwd
# cat group.mig >> /etc/group
# cat shadow.mig >> /etc/shadow
# /bin/cp gshadow.mig /etc/gshadow

Please note that you must use >> (append) and not > (create) shell redirection.


Step 4:
Now copy and extract home.tar.gz to new server /home
# cd /
# tar -zxvf /root/move/home.tar.gz


Step 5:
Now copy and extract mail.tar.gz (Mails) to new server /var/mail
# cd /
# tar -zxvf /root/move/mail.tar.gz

Step 5:
Now copy and extract mail1.tar.gz (Mails) to new server /var/spool/mail
# cd /
# tar -zxvf /root/move/mail1.tar.gz

Step 6:
# reboot

No comments:

Post a Comment