https://github.com/blackyboy/RedHat-Centos-Common-Stuffs
Data loss will be costly. At the very least, critical data loss will have a financial impact on companies of all sizes. In some cases, it can cost your job. I’ve seen cases where sysadmins learned this in the hard way.
There are several ways to backup a Linux system, including rsync and rsnapshot that we discussed a while back.
This article provides 6 practical examples on using dd command to backup the Linux system. dd is a powerful UNIX utility, which is used by the Linux kernel makefiles to make boot images. It can also be used to copy data. Only superuser can execute dd command.
Example 1. Backup Entire Harddisk
To backup an entire copy of a hard disk to another hard disk connected to the same system, execute the dd command as shown below. In this dd command example, the UNIX device name of the source hard disk is /dev/hda, and device name of the target hard disk is /dev/hdb.
If there are any errors, the above command will fail. If you give the parameter “conv=noerror” then it will continue to copy if there are read errors.
Input file and output file should be mentioned very carefully, if you mention source device in the target and vice versa, you might loss all your data.
In the copy of hard drive to hard drive using dd command given below, sync option allows you to copy everything using synchronized I/O.
Instead of taking a backup of the hard disk, you can create an image file of the hard disk and save it in other storage devices.There are many advantages to backing up your data to a disk image, one being the ease of use. This method is typically faster than other types of backups, enabling you to quickly restore data following an unexpected catastrophe.
Example 3. Restore using Hard Disk Image
To restore a hard disk with the image file of an another hard disk, use the following dd command example.
Example 5. Backup a Partition
You can use the device name of a partition in the input file, and in the output either you can specify your target path or image file as shown in the dd command example below.
dd command allows you to create an iso file from a source file. So we can insert the CD and enter dd command to create an iso file of a CD content.
Note: If CD is auto mounted, before creating an iso image using dd command, its always good if you unmount the CD device to avoid any unnecessary access to the CD ROM.
To backup my Linux partitions, I combine dd and gzip, e.g. to back up my Ubuntu root partition which is on /dev/sda5:
copy data over with a filemanager from a live CD.. grsync is an easy GUI for using rsync
To place the image on the
[new] drive:
How to Backup the Remote Linux Servers or Systems Using Rsnapshot
For this we have to Setup a non password login for Root
Only the root can perform a full backup cos only root have the administrative Privilage to access all files what ever we need to backup
Install the utility Rsnapshot
Note : Here in Configuration file Never Use the Spacebar key only u have to use the TAB Key if u need to give any spaces
Change the Directory if u need were to save the Backup by Default
The Current Backup folder is under
If u need to change the time when need to backup
Look at the line no:97,98,99,100
uncomment line no:230,231,232
If u Don’t want to took backup those Directories Comment the line with
Then if we need to took backup from 192.168.1.77 machine to my machine 192.168.1.99 set the command as below the Example in line no:241
Save the configuration file using
if so the test was sucess
To know the location of rsnapshot where is use command
Use command
This will create a cron job for current user
6 Examples to Backup Linux Using dd Command Including Disk to Disk
Data loss will be costly. At the very least, critical data loss will have a financial impact on companies of all sizes. In some cases, it can cost your job. I’ve seen cases where sysadmins learned this in the hard way.
There are several ways to backup a Linux system, including rsync and rsnapshot that we discussed a while back.
This article provides 6 practical examples on using dd command to backup the Linux system. dd is a powerful UNIX utility, which is used by the Linux kernel makefiles to make boot images. It can also be used to copy data. Only superuser can execute dd command.
Example 1. Backup Entire Harddisk
To backup an entire copy of a hard disk to another hard disk connected to the same system, execute the dd command as shown below. In this dd command example, the UNIX device name of the source hard disk is /dev/hda, and device name of the target hard disk is /dev/hdb.
dd if=/dev/sda of=/dev/sdb
“if” represents inputfile, and “of” represents output file. So the exact copy of /dev/sda will be available in /dev/sdb.If there are any errors, the above command will fail. If you give the parameter “conv=noerror” then it will continue to copy if there are read errors.
Input file and output file should be mentioned very carefully, if you mention source device in the target and vice versa, you might loss all your data.
In the copy of hard drive to hard drive using dd command given below, sync option allows you to copy everything using synchronized I/O.
dd if=/dev/sda of=/dev/sdb conv=noerror,sync
Example 2. Create an Image of a Hard DiskInstead of taking a backup of the hard disk, you can create an image file of the hard disk and save it in other storage devices.There are many advantages to backing up your data to a disk image, one being the ease of use. This method is typically faster than other types of backups, enabling you to quickly restore data following an unexpected catastrophe.
dd if=/dev/hda of=~/hdadisk.img
The above creates the image of a harddisk /dev/hda. Refer our earlier article How to view initrd.image for more details.Example 3. Restore using Hard Disk Image
To restore a hard disk with the image file of an another hard disk, use the following dd command example.
dd if=hdadisk.img of=/dev/hdb
The image file hdadisk.img file, is the image of a /dev/hda, so the above command will restore the image of /dev/hda to /dev/hdb.Example 5. Backup a Partition
You can use the device name of a partition in the input file, and in the output either you can specify your target path or image file as shown in the dd command example below.
dd if=/dev/hda1 of=~/partition1.img
Example 6. CDROM Backupdd command allows you to create an iso file from a source file. So we can insert the CD and enter dd command to create an iso file of a CD content.
dd if=/dev/cdrom of=tgsservice.iso bs=2048
dd command reads one block of input and process it and writes it into an output file. You can specify the block size for input and output file. In the above dd command example, the parameter “bs” specifies the block size for the both the input and output file. So dd uses 2048bytes as a block size in the above command.Note: If CD is auto mounted, before creating an iso image using dd command, its always good if you unmount the CD device to avoid any unnecessary access to the CD ROM.
To backup my Linux partitions, I combine dd and gzip, e.g. to back up my Ubuntu root partition which is on /dev/sda5:
dd if=/dev/sda5 bs=4096 | gzip -c > sda5-root.img.gz
Performance of the compression can be improved by creating & deleting a file from /dev/zero before doing the backup, e.g.
dd if=/dev/zero of=zero.bin bs=4096
To Create a Whole Backup of a Drive
dd if=/dev/vda | ssh babinlonston@192.168.1.100 'gzip - > /home/babinlonston/Desktop/backup.gz'
To restore, you have to take the server down and manually image the disk. Perhaps a hard drive swap or something of the sort.copy data over with a filemanager from a live CD.. grsync is an easy GUI for using rsync
To place the image on the
[new] drive:
gzip -d < image.gz | dd of=/dev/sda2
well to recover, you cannot just do it “live”, ie, while the system is running off of that hard disk. You would need to either to boot from a live medium (cd/etc) and do the disk image there, or perhaps pull out the hard drives and put them in another computer.How to Backup the Remote Linux Servers or Systems Using Rsnapshot
For this we have to Setup a non password login for Root
Only the root can perform a full backup cos only root have the administrative Privilage to access all files what ever we need to backup
- Setup a KeyBased Authentication
ssh-keygen
Create a New Key and Copy to the remote machine Using Command
ssh-copy-id -i ~/.ssh/id_rsa.pub
To remote Host using Command
ssh-copy-id -i ~/.ssh/id_rsa.pub sysadmin@192.168.1.77
It will ask for the Password for the machine 192.168.1.77 , give the password to Authenticate Install the utility Rsnapshot
apt-get install rsnapshot
Edit the configuration file of the Rsnapshot Note : Here in Configuration file Never Use the Spacebar key only u have to use the TAB Key if u need to give any spaces
vim /etc/rsnapshot.conf
In the Line no:27Change the Directory if u need were to save the Backup by Default
The Current Backup folder is under
snapshot_root /var/cache/rsnapshot/
If u need to change this default location to some were / as folder named backups
snapshot_root /backups
Then Enable the Line no:57
cmd_ssh /usr/bin/ssh
>Note : If this line Enabled only we can took backup over ssh if not we can’t took backup over ssh .If u need to change the time when need to backup
Look at the line no:97,98,99,100
retain hourly 6
If You Need to backup the Localhosts directory Such as /home/, /etc/, /usr/localuncomment line no:230,231,232
If u Don’t want to took backup those Directories Comment the line with
Then if we need to took backup from 192.168.1.77 machine to my machine 192.168.1.99 set the command as below the Example in line no:241
backup root@192.168.1.77:/etc/ /backup
This Command will backup the Directory /etc from 192.168.1.77 to /backup in 192.168.1.99Save the configuration file using
wq!
Test the rsnapshot configuration
rsnapshot configtest
It want to give u back a result as syntax okif so the test was sucess
To know the location of rsnapshot where is use command
whereis rsnapshot
If we need to backup the remote system 192.168.1.77 by mean time Use command
/usr/bin/rsnapshot/hourly
For Automate backup using cronjob, Setup a Cronjob for rsnapshot This will create a cron job for current user
crontab -e
In Cron job we need to define the entry by how it want to backup by hourly or by daily
0 5 * * * /usr/bin/rsnapshot hourly
this will backup hourly