hjaltiatlason

Backup and restore on a linux host

Apr 7th, 2021 (edited)
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.81 KB | None | 0 0
  1. Performing Backups and Restores on a Linux Host
  2.  
  3. Introduction:
  4.  
  5. The Linux command line provides several utilities for creating and restoring backups. The tar command can be used to create compressed archives of files and directories. The dd command can be used to copy and restore drives and partitions, and to create image files of drives and partitions that can be used for backups. Both archives and image files, as well as regular files and directories, can be remotely synced using the rsync command. In this lab, you will be tasked with using these utilities to back up important data on a Linux host and sync it to a backup server.
  6.  
  7. Additional Information:
  8.  
  9. You work as a Storage Administrator for a local print shop, and you have been tasked with performing some backups and restores on your production server (server01) to a backup server (server02). First, you need to create a tar archive of the following directories using gzip compression: /home, /etc, /opt, /usr, /root, and /srv. The archive should be named full_bkp.tgz and should be located in /home/cloud_user. Then, you need to create a tar archive of the /var directory using bzip2 compression. The archive should be named var_bkp.tbz.
  10.  
  11. Next, you need to restore the /dev/xvdg device using the /home/cloud_user/xvdg.img image file. Once restored, create an image file of /dev/xvdf called xvdf.img.bz (using bzip2 compression). It should be located in /home/cloud_user. After the image file has been created, wipe the /dev/xvdf device of all data.
  12.  
  13. Finally, you need to sync the tar archives you have created, as well as the disk image, to /home/cloud_user/archive on server02. Then, you need to perform a sync of the following directories on sever01 to the /home/cloud_user/sync directory on server02: /home, /etc, /opt, /usr/local, /var/lib, /var/log, /root, and /srv.
  14.  
  15. Notes:
  16. All tasks except remote sync should be performed as the root user.
  17. Use cloud_user when performing a remote sync, as remote root login is disabled.
  18.  
  19.  
  20. Solution:
  21.  
  22. Log in to both hosts (server01 and server02) in separate tabs using the credentials provided for the lab. Assume the root user for both hosts.
  23.  
  24. ########  Tar    ########
  25.  
  26. $ ssh cloud_user@<PUBLIC_IP_ADDRESS>
  27. $ sudo -i
  28. Use the tar Command to Create Backups of Directories on the server01 Host
  29. In the server01 host, use the tar command and gzip compression to create a full_bkp.tgz archive on the included directories. This takes a few minutes.
  30. # tar -cvzf /home/cloud_user/full_bkp.tgz /home/ /etc/ /opt/ /usr/local/ /root/ /srv
  31. Use the tar command and bzip2 compression to create a var_bkp.tbz archive in the /var directory. This takes a few minutes.
  32. # tar -cvjf /home/cloud_user/var_bkp.tbz /var/
  33. Run a listing on /home/cloud_user/. You should see your two new archives, full_bkp.tgz and var_bkp.tbz.
  34. # ll /home/cloud_user/
  35.  
  36.  
  37. ########  DD    ########
  38.  
  39. Use the dd Command to Create and Restore Backups of Devices on the server01 Host
  40. Clear server01 and list the device information. You should see your main root device (xvda) as well as two additional devices (xvdf and xvdg).
  41. # clear
  42. # lsblk
  43. Use xvdg.img to restore the xvdg device. Specify /home/cloud_user/xvdg.img as the input file, /dev/xvdg as the output file, and 4M as the byte size.
  44. # dd if=/home/cloud_user/xvdg.img of=/dev/xvdg bs=4M
  45. List the device information again. You should now see two partitions in the xvdg device, indicating the device was successfully restored.
  46. # lsblk
  47. Create an image file of /dev/xvdf using bzip2 compression, and call it xvdf.img.bz. Specify /dev/xvdf as the input file and 4M as the byte size.
  48. # dd if=/dev/xvdf bs=4M | bzip2 -c > /home/cloud_user/xvdf.img.bz
  49. Run a listing on /home/cloud_user/. You should now see the image listed in addition to the two archives you created.
  50. # ll /home/cloud_user/
  51. Run another listing to view the size of the compressed file. It should be 1.3K.
  52. # ll -h /home/cloud_user/
  53. Use /dev/zero to wipe the /dev/xvdf device. Specify /dev/zero as the input file, /dev/xvdf as the output file, and 4M as the byte size.
  54. # dd if=/dev/zero of=/dev/xvdf bs=4M
  55. List the device information to verify the xvdf device no longer has partitions, indicating it was successfully wiped.
  56. # lsblk
  57. Clear server01.
  58. # Clear
  59.  
  60. ########  Rsync   ########
  61. Synchronize Backups to server02 Using the rysnc Command on the server01 Host
  62. Use rsync to sync the var_bkp.tbz and full_bkp.tgz archives to server02. Ensure permissions and timestamps are preserved, and remember remote root logins are not permitted.
  63. # rsync -avzh /home/cloud_user/var_bkp.tbz /home/cloud_user/full_bkp.tgz cloud_user@10.0.1.102:/home/cloud_user/archive
  64. When prompted, enter y to continue connecting, then copy and paste the password provided for the lab.
  65. Use rsync to sync xvdf.img.bz to server02. Do not use archive mode or compression.
  66. # rsync -vh /home/cloud_user/xvdf.img.bz cloud_user@10.0.1.102:/home/cloud_user/archive
  67. When prompted, copy and paste the password provided for the lab.
  68. Use rsync to sync these directories to server02: /home, /etc, /opt, /usr/local, /var/lib, /var/log, /root, and /srv. Ensure permissions and timestamps are preserved, and remember not to use a trailing /.
  69. # rsync -avzh /home /etc /opt /usr/local /var/lib /var/log /root /srv cloud_user@10.0.1.102:/home/cloud_user/sync
  70. When prompted, copy and paste the password provided for the lab.
  71.  
  72. ######Validate the Backup Files and Directories Were Successfully Copied on the server02 Host#######
  73. On the server02 host, run a listing on /home/cloud_user/archive. You should see both archives you created as well as the image file you created.
  74. # ll /home/cloud_user/archive/
  75. Run another listing on /home/cloud_user/sync. You should see all the directories you synced to server02.
  76. # ll /home/cloud_user/sync
  77. Run a final listing to view all the files within one of your synced directories.
  78. # ll /home/cloud_user/sync/etc/
Add Comment
Please, Sign In to add comment