Friday, December 1, 2017

Learn to use GREP command with examples

Learn to use GREP command with examples

Grep is a very important & powerful Linux commands. It stands for ‘Global Regular Expression Print’ & is used for matching & printing a search pattern or a regular expression from a single or multiple text files. It will look for the pattern if the mentioned files & will than print the result on screen or to an output file.

In this tutorial, we are going to learn to use grep command with examples. Syntax for the command is as follows,
$ grep option search_pattern file_path
Now let’s start to use grep command with examples…..

Searching a pattern from a single file

To search & locate a pattern from a single file, use the following command,
$ grep danger example.txt
This will look for all the lines with word ‘danger’ in the file ‘example.txt’ & will produce the result on screen.

Search a pattern in multiple files

We can also search the pattern from multiple files, using the following command,
$ grep danger example.txt test.txt
Another example would be,
$ grep danger *.*
This will look for search pattern ‘danger’ in all the files with any name or extension.

Redirecting output to a file

Just like we can do with any other command, we can also redirect the output of the grep command to a file, use
$ grep danger example.txt > output.txt

Case insensitive search

Grep searches are case sensitive by default. If we need to run case-insensitive search, we can use ‘i’ option with grep command,
$ search -i danger example.txt
This will print any variation of search pattern ‘danger‘, ‘DANGER‘ or ‘Danger‘ or ‘daNGer‘ as the output on the screen.

Searching a pattern in sub-directories

We can also search a pattern in sub-directories & all the files they contain by using ‘r’ option,
$ grep -r danger /home/dan

Display line number for search pattern

To display the line number on the which search pattern has matched, we can use ‘n’ option with grep command,
$ grep -n danger example.txt

Highlight the matched pattern

To highlight the matched search pattern, we can use ‘color’ with grep command,
$ grep -color danger example.txt

Print lines starting with the pattern

To print all the lines as output on screen which starts with out search pattern, we can use ‘^’ (carrot symbol) option ,
$ grep ^danger example.txt

Print lines ending with the pattern

To print all the lines that end with the search pattern, use ‘$’ symbol,
$ grep danger$ example.txt

Print all lines excluding the matched ones

To print all the lines, excluding the lines which have the matched search pattern, we can use ‘v’ option,
$ grep -v danger example.txt

Combining grep with another command

We can also use grep command to search a pattern from another command’s output. For example, we need to get the process information about httpd from all the running processes, we can combine grep command with ‘ps’ command,
$ ps -ef | grep httpd
This will bring process information for httpd. Similarly we can use this for other services & other commands as well. Another example,
$ netstat -anp | grep 3306
This will produce list of all connections with mysql database.
With this we end our tutorial on learning grep command with examples. These were only some examples & we have only covered some of the most commonly used grep command’s options, but there are plenty more options, which you can see with ‘grep –help’ command.

Tuesday, November 28, 2017

[Mail Server]: Complete guide to install Zimbra Mail on CentOS & RHEL

Complete guide to install Zimbra Mail on CentOS & RHEL

Zimbra Collaboration Suite (ZCS) or commonly known as Zimbra Mail, is collaborative software suite that includes an email server and a web client. It also includes contacts, file sharing, calendar , tasks etc which can be easily accessed via web mail.

Zimbra offers two editions, the commercially supported edition called ‘Network Edition’ & an free/open source version called ‘Open Source Edition’, which is the focus of this tutorial. In this tutorial, we are going to discuss how to install Zimbra mail on CentOS & RHEL machines.
Let’s start by discussing some requirements for Zimbra mail on CentOS,

Pre-requisites

1- We need to have a systems with freshly installed OS, minimal OS is recommended. We can also use a system with pre-installed OS but make sure that we have web, database & mail services (Postfix, Sendmail) disabled.
Even with freshly installed OS, we will have some of these services working, mainly POSTFIX. Disable it,
$ sudo systemctl stop postfix
$ sudo systemclt disable postfix
2- We will also need ‘A record’ & ‘MX record ‘ for our mail server in DNS, this is required to send & receive mails. We can also use a local DNS server for the same.
If not have one already, refer to our article on “How to create a BIND Server” to create a local DNS server.
3- Create a hostname entry for your server in ‘/etc/hosts’ file,
$ sudo vim /etc/hosts
192.168.1.120 testmail.linuxtechlab.com testmail
where, ‘testmail.linuxtechlab.com’ is FQDN of the server & ‘testmail’ is the name of the server.

Installation

Before we can install Zimbra mail on Centos or RHEL system, we need to install dependency packages for ZCS. Install them using the following command,
$ sudo yum install wget perl perl-core ntpl nmap sudo libidn gmp libaio libstdc++ unzip sysstat sqlite
After these packages have been installed, we are now ready to download & install latest version of Zimbra mail on CentOS and RHEL systems. At the time of writing this tutorial, 8.7.11 is the latest version available. Download it using the following command,
$ wget https://files.zimbra.com/downloads/8.7.11_GA/zcs-8.7.11_GA_1854.RHEL7_64.20170531151956.tgz
For CentOS & RHEL 6, use the following command to download the install script,
$ wget https://files.zimbra.com/downloads/8.7.11_GA/zcs-8.7.11_GA_1854.RHEL6_64.20170531151956.tgz
Downloaded file is a tar package, extract it
$ tar -xzf zcs-8.7.11_GA_1854.RHEL7_64.20170531151956.tgz
Next, enter into the extracted folder,
$ cd zcs-8.7.11_GA_1854.RHEL7_64.20170531151956
Now, we will start the installation script for the Zimbra mail,
$ ./install.sh –platform-override
Note:- ‘–platform-override’ option is used because script is meant to run on Redhat but to install Zimbra mail on CentOS, we need to add that option.
Once the script starts, we would be firstly required to accept the license. Accept the license by pressing ‘Y’ key,
zimbra mail on centos
& than will be asked if you would like to use ‘Zimbra Package repository’, press Y. Next, select the packages you need to install. We are selecting all packages for installation,
zimbra mail on centos
Installation will than start, it will take some time to complete,
zimbra mail on centos
Once the installation completes, you will get the following screen as output,
Zimbra mail on centos
Here we can change any configuration for our server, if we find them wrong or just want to modify them.
We will be changing password for admin user, change it by pressing ‘3’ key & provide the password. Once changes have been made, apply all the configurations made by pressing ‘a’ key & Zimbra will than apply the configurations to the server.
Next save the configuration file by pressing ‘s’ key & quit from the menu by pressing ‘q’ key. Our Zimbra server on CentOS system is now ready, start the Zimbra service,
$ su – zimbra
$ zmcontrol start
& login to the Zimbra mail from a browser with the URL,
http://testmail.linuxtechlab.com
Enter the username ‘admin’ & password that we provided during installation,
zimbra mail on centos
After logging in, we can than perform functions like add new accounts, install certificated etc.
This was our tutorial on how to install Zimbra mail on CentOS & RHEL systems.

How to rename user in Linux (also rename group & home directory)

How to rename user in Linux (also rename group & home directory)

Rename user in Linux

For renaming user in Linux systems, we will use ‘usermod’ command. Syntax for the command is,
$ usermod -l new_username old_username
For example, if we have a user named ‘dan’ & want to rename it to ‘susan’, execute the following command from terminal;
$ sudo usermod -l susan dan
This will only change the username & everything else, like group, home directory, UID will remain same.
Note:- You should need to logged out from the account you are trying to rename. You can also kill all the processes running for that user, to do so execute the following command,
$ sudo pkill -u dan
$ sudo pkill -9 -u dan

Renaming Home directory

For renaming home directory to correspond to the renamed user, we use ‘-d’ option with ‘usermod’ command.,
$ sudo usermod -d /home/susan -m susan

Changing UID for the user

To change the UID of the user , execute the following command,
$ sudo usermod -u 2000 susan
where ‘2000’ is the new UID for user.

Renaming the group

To rename the group from ‘dan’ to ‘susan‘, we will use ‘groupmod’ command. Use the following command to rename the group,
$ groupmod -n susan dan
Once we have made the required changes, we can than check the changes made using the ‘id’command,
$ id susan

Thursday, October 5, 2017

Install BackupPC on CentOS

Install BackupPC on CentOS

System Setup

Setting up repos

The first thing to do is to install the CentOS testing repo, along with the yum-priorities plugin.
# cd /etc/yum.repos.d
# wget http://dev.centos.org/centos/5/CentOS-Testing.repo
# yum install yum-priorities

Follow the guide on the wiki for setting up [http://wiki.centos.org/PackageManagement/Yum/Priorities?highlight=(yum)|(priorities)|yum-priorities]. If you have RPMForge's repo installed, you'll want to make sure all the CentOS Base items are priority 1, RPMForge's items priority 2, and CentOS testing repo is priority 3. If you don't have RPMForge's repo installed, then all the Perl packages will be downloaded from the testing repo, otherwise, they will come from RPMForge. To setup the RPMForge repo, follow the guide on the wiki for the [repo].

Install BackupPC

Next, install the BackupPC RPM, Apache, and mod_perl. All the Perl dependencies will be collected automatically.
# yum --enablerepo=c5-testing install backuppc httpd mod_perl

Configure Apache

As mentioned, the BackupPC user created on the system when installing the RPM has to run Apache in order for everything to work properly with the CGIs and mod_perl. Go ahead and setup the appropriate values in httpd.conf.
# vim /etc/httpd/conf/httpd.conf
## Change User apache to User backuppc
User backuppc
Servername backuppc.domain.com:80

Save and quit the file, then change the backuppc.conf file that was created under the conf.d directory.
# vim /etc/httpd/conf.d/backuppc.conf
## Change Allow from 127.0.0.1 to all
Allow from all

Save and quit this file, and then create the user and password that you are going to allow access to the web interface.
# htpasswd -c /var/lib/backuppc/passwd/htpasswd your_user
New password: your_password
Re-type new password: your_password
Adding password for user your_user

Last, start and configure Apache to start up at boot time. Then browse to your machine and make sure Apache is dishing out a test page.
# service httpd start
# chkconfig httpd on

Browse to http://your_server and make sure Apache is working ok.

BackupPC Server Configuration

BackupPC Main Config

The initial configuration needs to be edited on the command line with a few parameters, and then later on the rest can be either done from the command line, or from the web interface. To start, open up the main BackupPC configuration file, and set the following parameters. The TopDir path is where the actual backup data resides. The default is /var/lib/backuppc. I have an encrypted partition used to store backups, so my path is /srv/backuppc. Change to suit your needs.
# vim /etc/BackupPC/config.pl

## Default transfer method BackupPC uses.
$Conf{XferMethod} = 'rsync';

## Path to where actual backup data is stored.
$Conf{TopDir}      = '/var/lib/backuppc';

## Path to init.d which is used to to start server.
$Conf{ServerInitdPath} = '/etc/init.d/backuppc';
$Conf{ServerInitdStartCmd} = '$sshPath -q -x -l root $serverHost$serverInitdPath start';

## Allowed user that you created using htpasswd.
$Conf{CgiAdminUsers}     = 'your_user';

BackupPC Sudo Setup

The backuppc user needs to have sudo access to run the gtar and tar commands. Otherwise, BackupPC won't run correctly. Sudo should already be installed on your system, so you can change what you need using the visudo command.
# visudo

## Comment out Defaults requiretty
## Add the following two lines.
Defaults !lecture
backuppc ALL=NOPASSWD:/bin/gtar,/bin/tar

Save and quit the file, then restart Apache, start and configure BackupPC to start and turn on at boot time.
# service httpd restart
# service backuppc start
# chkconfig backuppc on

Open up a web browser and go to the BackupPC web interface at http://your_server/backuppc. You'll need to sign on using the username and password you created earlier, then you should be displayed a general server information page about BackupPC. If not, retrace your steps and make sure Apache and BackuPC are configured correctly.

BackupPC SSH Keys

Since concentrating on rsync backups, you'll want to create passwordless keys used for the backuppc process to connect remotely to your hosts being backed up. As root create the hidden SSH directory under /var/lib/backuppc and change the permissions accordingly.
# cd /var/lib/backuppc
# mkdir .ssh
# chown backuppc.backuppc .ssh
# chmod 700 .ssh

Next, drop in as the backuppc user. You'll have to specify a shell because by default the backuppc user has no shell assigned to it. Then create the passwordless SSH keys using ssh-keygen.
# su -s /bin/bash backuppc
bash-3.2$ ssh-keygen -t dsa

Generating public/private dsa key pair.
Enter file in which to save the key (/var/lib/backuppc/.ssh/id_dsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /var/lib/backuppc/.ssh/id_dsa.
Your public key has been saved in /var/lib/backuppc/.ssh/id_dsa.pub.
The key fingerprint is:
xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx backuppc@host.domain.name

Server Key to Client

For each client you're going to configure backups for, you'll need to copy the key you created from the server over to the client. To do so, continue from the last step, and run the ssh-copy-id command while still logged in as the backuppc user on the server.
bash-3.2$ ssh-copy-id -i .ssh/id_dsa.pub root@host_to_backup

It should have copied the key over to the host, and then also logged you into the host with SSH.

Client Setup

BackupPC Basics

Before you start using the web interface, let's explain a few basics about the configurations and options used with hosts. Most other guides I read left me bewildered from this aspect. The main configuration file you edited earlier, located at /etc/BackupPC/config.pl, is where all the defaults for BackupPC reside. This configuration file can either edited on the command line, or through the GUI where documentation is linked in to the options. When you log in to the web interface, you should see an Edit Config tab on the left-hand side. When you click this, you're actually editing the /etc/BackupPC/config.pl file. Until you read the documentation, pretty much leave alone all the defaults options or you'll find a non-working backup system pretty quickly.
While the main configuration file is what sets all the defaults, you can actually override these settings on a per-host basis. After you add a host to the web interface, any configurations you add to the host overrides the default. This then creates a separate directory, /etc/BackupPC/pc/host_name.pl, file. This file contains all the settings that are either changed, or different than what the defaults are from /etc/BackupPC/config.pl. So for instance, what would be displayed in these files would be the directories or excluded directories that are to be backed up for the host that you set.
Unless you are backing up the identical directories on all hosts, you'll want to use the override feature to setup the directories after you create the host. This is done on the Xfer tab with the RsyncShareName. Right below this setting, is the BackupFilesExclude where directories like /var/cache can be excluded from being backed up.
Under the main BackupSettings tab, you'll find the DumpPreUserCmd. This allows you to use any kind of script to be specified, which is run before the actual rsync of directories. For instance, if you have databases that need dumped and included in the backups, they are specified here. There already exists an AutoMySQLBackup from SourceForge, so this will be explained in the next section. The DumpPostUserCmd allows you run a command after the backup occurs, and the DumpPreShareCmd and DumpPostShareCmd allow you to runs scripts before and after the share of a dump. NOTE: A lot of people seem confused about parameters in these settings. You cannot treat the fields as command prompts or shells. Shell syntax is not passed here, so if you have multiple commands or scripts to run, put them in one script on the client side where they can be executed one after the other.
Last, the Schedule tab is where you setup how full and incremental backups happen. You can either set this up at the default level in Edit Config, or you can again override settings on a per-host basis. The default setting is to keep only one full backup, and 6 incrementals. This gives you a week's worth of backup. The FullPeriod is set for 6.97, which means every 7 days a full backup dump occurs. IncrPeriod is set to 0.97 which means that every day an incremental dump will occur. The IncrKeepCnt is set to 6 which gives you 6 days of incrementals and the other day of a full backup. You can also setup BlackoutPeriods, or times you don't want backups to occur. Every setting in here is explained by the documentation, so read up.

Add Client in Web Interface

Next, add the client and all the configurations from within the web interface. Click on the Edit Hosts tab and then the Add button. Fill in the host's name that you want to backup, and then under the User section, put backuppc as the user. This needs to be backuppc, because the backup is launched as this user. It won't work otherwise. When you're done, click the save button at the top. Click on the Host Summary link on the left, and you should see your new host. To start configuring the host settings, click on the host name, then a new section at the top opens. Click on Edit Config at the very top left-hand side under the host's name.
From here, everything you setup will override the default settings thereby storing them in the /etc/BackupPC/pc/host_name.pl file. Click on the Xfer button, and start adding directories under the RsyncShareName. Below this setting in the Include/Exclude area, enter anything in BackupFilesExclude that you want excluded. When finished, make sure you click save at the top. This is pretty much it for adding a host. If you select the host, you can then manual start and stop backups to see if they work.
By default, BackupPC wakes up every hour around the clock to see if hosts need queued up for a backup. To change this behavior, look under the Server configuration tab under EditConfig. Change WakeupSchedule to suit your needs.

Dumping Databases

To include a database dump in BackupPC on the host being backed up, you have two choices. First, write your own script that is run on the host prior to the dump. Second, you can use the AutoMySQLBackup script project found on SourceForge to dump your MySQL databases. With some basic editing to the script, you can easily make it work for PostgreSQL databases too. To get started, download the script from [http://sourceforge.net/projects/automysqlbackup|SourceForge]. Copy the script to somewhere like /usr/local/bin on the host you are wanting to backup. I generally rename the script to reflect the host name for which the dump is going to take place. This step isn't necessary, and only done for logic's sake. You do have to make sure that the script is executable, however.
# scp automysqlbackup.sh.2.5 host_name:/usr/local/bin
# ssh host_name
# mv automysqlbackup.sh.2.5 mysql_hostname_dump
# chmod +x mysql_hostname_dump

The script is heavily documented for what all the options do. Make sure to go through and read it once so you understand what the script is doing. AutoMySQLBackup connects to and dumps all the databases you specify to /backups, e-mails you if you set it up, and also does rotational steps to make sure you have a set of database dumps.
Make sure to fill in USERNAME, PASSWORD, and the DBNAMES. If you want mailing setup, configure it as the directions state in the script. It's pretty basic to configure, so once you have the options set up correctly, go ahead and run the script once manually on the host being backed up. You can then see if the /backups directory was created, and whether or not the dump occured.
# cd /usr/local/bin
# ./mysql_hostname_dump

....tons of output...or errors...fix accordingly.

# cd /backups
# ll

You should see a directory structure as daily, weekly, and monthly with all your database names in a directory of their own inside these. You can traverse these to locate the actual .sql.gz files which are your databases.

Add DumpPreUserCmd to BackupPC Host

Now that the dump script is set up physically on the host, configure the DumpPreUserCmd in BackupPC's web interface with the command to SSH to the host being backed up and execute AutoMySQLBackup. Select a host in the web interface, and then choose the EditConfig tab at the top left-hand side. Choose the BackupSettings tab and go down under the User Commands section. The very first line is DumpPreUserCmd. Add the following line in this section.
$sshPath -q -x -l root $host /usr/local/bin/mysql_hostname_dump

Choose save at the top, and then head over to the Xfer tab for the host. Make sure you add the /backups directory to the RsyncShareName, otherwise the dumps will never be copied off the host. That's it, now your host's MySQL databases are being dumped prior to the BackupPC rsync of directories. As mentioned, you can easily edit AutoMySQLBackup to be a AutoPostgreSQLBackup script. Just replace all the MySQL syntax with PostgreSQL commands and options.

BackupPC's Other Capabilities

As mentioned, BackupPC can also backup SMB shares, use tar for backups, and archive backups to other media. The appropriate documentation of BackupPC explains each.
$Conf{XferMethod} = 'rsync';

The valid values are:

      - 'smb':     backup and restore via smbclient and the SMB protocol.
                   Easiest choice for WinXX.

      - 'rsync':   backup and restore via rsync (via rsh or ssh).
                   Best choice for linux/unix.  Good choice also for WinXX.

      - 'rsyncd':  backup and restore via rsync daemon on the client.
                   Best choice for linux/unix if you have rsyncd running on
                   the client.  Good choice also for WinXX.

      - 'tar':    backup and restore via tar, tar over ssh, rsh or nfs.
                  Good choice for linux/unix.

      - 'archive': host is a special archive host.  Backups are not done.
                   An archive host is used to archive other host's backups
                   to permanent media, such as tape, CDR or DVD.

If you're looking to backup Windows hosts, then you'll want to use either SMB, creating shares on the Windows machines, or search the Internet for how to use rsync and Cygwin on Windows. That is beyond the scope of this article. Otherwise, have fun backing up machines.

Backing up Windows Hosts with autofs

An alternative way to backup Windows hosts would be use autofs, which is a tool for automatically mounting and unmounting filesystems. If you're looking to do remote backups, then this isn't the way for you, since this would be best used on a local trusted network. Passwords are stored on the BackupPC server in files. If you have Windows PCs and servers internally on your networks, though, this method works great since it doesn't require installation of software or clients on the Windows machines. Instead it uses Samba shares on Windows machine to automatically mount filesystems when asked.

Configure autofs

The autofs package should already be installed on your Linux server. The first thing to do is add a seperate file to handle the Windows hosts, along with the options you want passed along when automounting Windows hosts. Open up /etc/auto.master and add these.
# vim /etc/auto.master
/windows        /etc/auto.windows       --timeout=30    --ghost

These options tell autofs to use /windows as the base mount point, get all Windows autofs hosts from /etc/auto.windows, use an inactivity timeout of 30 seconds, and create ghost (empty) directories of the mount point. This means that after 30 seconds of inactivity on the mount point, it will unmount the share, and since the empty directories will be there, you can't remove them when they are not mounted.
Next, add a host to the auto.windows file with the appropriate options you need.
# vim /etc/auto.windows
machinename     -fstype=cifs,ro,credentials=/etc/.autofs.smbpasswd      ://machinename/C\$

This line means mount "machinename" using cifs with read-only access. Generally, you probably don't want anything other than read-only access, since you're only backing up files. The key file stores the samba user name and password allowed to make the mount. Last, use the C drive as the mount point. If you have Windows installed on another drive, or you have data on another drive letter, just use it.
Add the key file with the proper credentials needed to mount samba shares. You probably don't have to have the domain name added in, as it should still work without it.
# vim /etc/.auto.smbpasswd
username=DOMAIN/username
password=password
# chmod 600 /etc/.auto.smbpasswd

That's pretty much it to configuring it. Restart the autofs daemon and try mounting your share now. When you navigate to the directory it will automatically mount your samba share on your Windows machine. As long as you're in the mounted directory, it will stay mounted. As soon as your leave the directory, the timeout of 30 seconds will start, and then your share will unmount itself.
# service autofs restart
# cd /windows/machinename
# mount
//machinename/C$ on /windows/machinename type cifs (ro,mand)

Configure BackupPC to use autofs host

Go ahead and add a host the way you already have been doing within BackupPC's web interface. There aren't many differences between adding normal Linux hosts and the autofs Windows hosts. Basically, the big difference is that you don't need to use SSH to access the host, since you're mounting the Windows filesystem locally on BackupPC.
After you add the host, navigate to Edit Config -> Xfer. Change the following rsync commands:
RsyncClientCmd = $rsyncPath $argList
RsyncClientRestoreCmd = $rsyncPath $argList

Now, under your share names to backup, you can use the same formatting as with normal Linux servers being backed up. If you wanted to backup a directory with spaces in them, type them as you see them. Removing the + at the end of $argList+ deals with handling escaping better since this is locally backing up, and not backing up across SSH, at least that's my understanding.
RsyncShareName = /windows/machinename/backup
RsyncShareName = /windows/machinename/Documents and Settings

That should be it. You should start to see your Windows directories being backed up now.

Source: https://wiki.centos.org/HowTos/BackupPC