Tuesday, February 20, 2018

rsync - How to Backup Files in Linux With Rsync on the Command Line

How to Backup Files in Linux With Rsync on the Command Line

There are many GUI tools; some come preinstalled on many distros, but since I run a headless file server I use command line tools and that's what I am going to talk about in this article.
I also tend to keep things as simple as possible, so the tool I use for my back-up is 'rsync'.

What’s rsync

Rsync stands for remote sync which was written by Andrew Tridgell and Paul Mackerras back in 1996. It's one of the most used 'tools' in the UNIX world and almost a standard for syncing data. Most Linux distros have rsync pre-installed, but if it’s not there you can install the 'rsync' package for your distribution.
Rsync is an extremely powerful tool and does more than just make copies of your files on your system. You can use it to sync files on two directories on the same PC; you can sync directories on two different systems on the same network; or sync directories residing on machines thousands of miles apart, over the Internet.
The functionality of rsync can be expanded by using different 'options', which we will talk about soon.
The basic syntax of rsync is
rsync option source-directory destination-directory
Let’s assume you have a directory /media/hdd1/data-1 on hard drive 1 and you want to make a copy of it on a new hard drive which is mounted at /media/hdd2. 
The following command will create the directory data-1 on the second hard drive can copy the content of the directory to the destination:
rsync -r /media/hdd1/data-1 /media/hdd2/
The option '-r' ensures that it's recursive and will also sync all directories.
However once the directory data-1 is created on hdd2 then you can start syncing the content of the two directories:
rsync -r /media/hdd1/data-1/ /media/hdd2/data-1/
Don't forget the backward slash at the end otherwise rsync will create a new directory inside the destination directory.
Alternatively you can create a new directory on destination and then sync it with source. Let's assume you created a directory data-2 on the second hard drive and want to sync the two without any confusion:
rsync -r /media/hdd1/data-1/ /media/hdd2/data-2/
This command will simply make an exact copy of your files in the data-1 directory inside the data-2 directory.
What if you have symlinks of different permissions of file ownership and you want to preserve them? Just use the '-a' option and it will preserve the date, ownership, permissions, groups, etc. of the files.
Now you have two sets of directories synced with each other. There is a chance that you may delete some files or folders from the source; I do it all the time. How do we ensure that those are deleted from the destination as well? You need to use the '--delete' option which will take care of such cases.The command becomes:
rsync -a --delete /media/hdd1/data-1/ /media/hdd2/data-2/
If you want to see the progress of files in the terminal, add the '-v' option to it:
rsync -av --delete /media/hdd1/data-1/ /media/hdd2/data-2/
It's also advisable to compress files for transfer so it saves bandwidth over the network, resulting in faster transfer. You should do it if your devices have slower transfer. The option to use is '-z'.
rsync -avz --delete /media/hdd1/data-1/ /media/hdd2/data-2/
You can also throw in '-P' option which is for partial progress.
rsync -avzP --delete /media/hdd1/data-1/ /media/hdd2/data-2/

Working on networked machines

As I wrote in an article earlier I run a local file server at home and mount it on all my devices to access my files. I never save any data on my local machine; I always work on files stored on the primary hard drive on the server. That way my files are always up-to-date and I can pick them from any machine and continue to; no need to copy from one machine to another.
I don't mount the second, or the back-up hard drive. Mounting it and working on files saved on this hard drive will complicate things because when I run the rsync command it will overwrite the changes from the primary hard drive. Though rsync has a trick (or option) up its sleeves to address such issues.  You can use the '-u' option which will force rsync to skip any file which has the modification date later than the source file.

How to sync directories over network

This is where ssh protocol comes into play. I use the following syntax to sync a remote directory with a local directory:
rsync -avzP --delete -e ssh user@server_IP:source-directory /destination_directory_on_local_machine/
Example:
rsync -avzP --delete -e ssh 
 This e-mail address is being protected from spambots. You need JavaScript enabled to view it
 :/home/swapnil/backup/ /media/internal/local_backup/
To sync a local directory with a remote directory the syntax becomes:
rsync -avzP --delete -e ssh source_directory user@server_IP:path_destination_directory
Example:
rsync -avzP --delete -e ssh /home/swapnil/Downloads/ 
 This e-mail address is being protected from spambots. You need JavaScript enabled to view it
 :/home/swapnil/Downloads/

Automate backup

You may want to automate backup so you don't have to add it to your calendar. It will actually be easier to automate the backup then create a calendar entry.
I tend to keep things simple and easy, so I can show new users how easy it is to do such things under Linux. The solution that I use for automation is 'crontab'. It’s simple, lightweight and does the job well. With Crontab I can configure when I want to run the rsync command: daily, weekly,  monthly, or more than once a day (which I won’t do). I have configured mine to run at 11:30 p.m. every day after work so all of the files that I worked on throughout the day get synced.
Depending on your distro you may have to install a package to get crontab on your system. If you are on Arch Linux, for example, you can install ‘cronie’. You can choose the default editor for crontab; I prefer nano. Run this command and replace 'nano' with the desired editor.
export EDITOR=nano
Now run 'crontab -e' to create cron jobs. It will open an empty file where you can configure the command that you want to run at a desired time. (See image, above.)
The format of crontab is simple; it has five fields followed by the command:
m h dm m dw command
Here m stands for minutes (0-59); h for hour (0-23); dm for day of the month (1-31); m for month (1-12); and dw for day of the week (0-6 where 0 is Sunday). The format is numerical and you have to use ‘*’ to commend the fields that you don’t want to use.
I run the command every day at 11.30 so the format will be
30 23 * * * rsync -av --delete /media/hdd1/data-1/ /media/hdd2/data-2/
If you want to run rsync only once a month then you can do something like this:
30 23 1 * * rsync -av --delete /media/hdd1/data-1/ /media/hdd2/data-2/
Now it will run at 11:30 p.m. on 1st of every month. If you don’t want it to run every month than you can configure it to run every six months:
30 23 1 6 * rsync -av --delete /media/hdd1/data-1/ /media/hdd2/data-2/
That will make it run every year on June 1. If you want to run more than one command, then create a new line for every command. Rsync is not the only command you can automate with 'crontab' you can run 'any' command using it.
As you can see both tools - rsync and crontab - are extremely simple and lightweight yet extremely powerful and highly configurable. Linux doesn't have to to complicated!

Keep one copy remotely

One risk of keeping all your data on local machines is that in case of a natural disaster, fire or flood, your local system will be damaged and you will lose your data. It's recommended to keep another copy of your data on a machine located elsewhere. I have one server at my in-laws' place; I call it 'Server In Law'.
The bad news is ISPs don't allow static IP and may block forwarded ports so it's not possible to ssh between two machines and sync data. That's where TeamViewer and SSH Tunnel comes into play. I log into my Server In Law, open a temporary ssh tunnel and then rsync the files.
Since these are GUI-based tools they are beyond the scope of this cli focused article. I may cover it in the future.

 

Friday, February 16, 2018

NTP : Network Time Protocol: Install NTP server on Linux machines

Network Time Protocol: Install NTP server on Linux machines


NTP or Network Time Protocol is a very important service that is used to keep the system time accurate. NTP service is used to synchronize time on all local servers with respect to a designated server called, NTP server. That NTP server itself syncs time from a public NTP server.

NTP synchronized systems clocks are synchronized upto millisecond precision. Most big organizations have NTP servers for managing time for their IT infra. In this tutorial, we will learn to install & configure NTP server on CentOS & Ubuntu machines.


Installation

We need to install a package named ‘ntp’ on the machine which will be configured as NTP servers. To install ntp on the CentOS/RHEL servers, open terminal & run the following command,
$ sudo yum install ntp
For Ubuntu systems, execute the following command from the terminal,
$ sudo apt-get install ntp
Now start the ntp service & enable it for boot time with the following commands,
$ sudo systemctl start ntpd
$ sudo systemctl enable ntpd

Configuration

After we have installed the ntp server, we will now configure it & first thing we need is the address for public ntp servers closest to us or at a desired location. To get the list of all the ntp server, goto the following url,
http://www.pool.ntp.org/zone/@
& select the ntp server of your choosing. We will now make the server entries in ntp configuration file i.e. ‘/etc/ntp.conf’. For this tutorial, we will be using the ntp servers from North Ameraica/United states,
$ sudo vim /etc/ntp.conf
server 0.us.pool.ntp.org
server 1.us.pool.ntp.org
server 2.us.pool.ntp.org
server 3.us.pool.ntp.org
Also enable logging to troubleshoot any issues with ntp, to do this make the entry for following line in the same file,
logfile /var/log/ntp.log
Save the file & exit. Restart the ntp service to implement the changes made,
$ sudo systemctl restart ntpd
Now to make sure that our ntp server is synchronized with the public ntp server, run the following command from the terminal,
$ ntpq –p
This command will show complete information for time sync between local ntp server & public nt server.
Note:- If using firewall on the system, make sure that UDP port 123 is open for ntp to work. Open the ntp port with the following command,
$ firewall-cmd –add-service=ntp –permanent
$ firewall-cmd –reload

Configuring a local client

Once our local ntp server is ready, we will configure it on Linux client machine. But before we do that, we need to make sure that our local ntp server is available for synchronizing time on local network. Open the ntp server & make an entry for following line on ntp.conf,
$ sudo vim /etc/ntp.conf
# Hosts on local network are less restricted.
restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
Here, 192.168.1.0 is the local network. Now restart the ntp service after saving the file & login to client machine on which ntp will be configured.
Similarly as we did on ntp server, we also need to install ntp service on client machine as well. After the service has been installed, open the file ‘ntp.conf’ & add the ntp server IP address to the bottom of the file (its 192.168.1.100 in our case),
$ sudo vim /etc/ntp.conf
server 192.168.1.100
Now restart the ntp service after saving the file. That’s it, our ntp client server is configured & will now sync the time from our local ntp server instead of internet or other sources.
Note:- We can also run the following command to update the time manually on client machine from our local ntp server,
$ ntpdate 192.168.1.100
That’s it guys, with this we end our tutorial on how to install & configure ntp server.

Monday, February 12, 2018

Linux Directory : Reference guide to Linux Directory structure

Reference guide to Linux Directory structure

When we install a Linux distribution we see that a number of directories are created. These created directories may be same or partially different on various Linux distributions. You might know about the directory structures & what purpose they serve. For those of you who don’t know about the Linux directory structure or have partial knowledge or just want to relook on Linux directory structure & its usage, this article will act as a reference guide.

We will start out this article with the Linux directories/partitions that are must for every Linux system & are required for Linux system to work properly. These partitions are ‘/’ (root), ‘/boot’, ‘/swap’. Though the ‘/’ (root), ‘/boot’ partitions are absolutely required but a system might work without swap partition, but its recommended that we have one for our Linux system. So let’s start out with these Linux partitions & their purpose,

Linux Directory structure

‘/’ (root) – / or root partition is one of the most important partition for a Linux machine. This is the partition where all other Linux partitions are. So this is the basis of Linux directory structure.
‘/boot’- Another important partition, boot partition contains all the startup files, kernel files & VMLinuz. In the recent, new Linux distribution, it also holds the Grub data as well.
‘/swap’- Swap partition acts as a virtual memory/RAM for the system & is used when your system runs out of the physical RAM. Though usually it’s a separate partition, we can also use a file to act as swap space.
These are the minimum partitions that are required to run a Linux system. Below mentioned are other partitions in Linux directory structure,
‘/root’- This is the home directory/partition for the administrative user i.e. root.
‘/home’- This is the directory that contains the home directory for all users other than super user root.
‘/etc’- This partitions holds all the important configuration files for the system. Once can also compare this partition to control panel on Windows system.
‘/bin’- This directory contains the common programs that are shared among administrative users, common users & the system.
‘/sbin’- contains all the programs that are used by system & system administrator.
‘/initrd’- This directory contains the system library files, files for the system that are needed by system or other programs to run.
‘/mnt’- this directory acts as a default mount point for all external devices like usb storage device, CD-DVD rom etc.
‘/opt’- Its used as installation folder for extra software & 3rd party softwares.
‘/var’- This folder is the default storage for all the variable files created by users, like log files, mail queue etc. This folder also contains some temporary files.
‘/proc’- This virtual file system contains complete information regarding the system resources.
‘/tmp’- This partition acts as a temporary space for use by the system. Files are stored temporarily on this partition & are cleaned upon every reboot.
‘lost+found’- Every partition has a lost+found directory in its upper directory. This serves as a storage for those files that were saved during failures.
‘/usr’ – This partition mainly has all documentation to user- related programs. It also contains some user-related programs & libraries.
‘/misc’- This partition is used by system for miscellaneous purposes.
This is our article that will act as reference guide for Linux directory structure.