Backup and restore commands for Linux files, directories and file systems
Common compression instructions in Linux system
Common compressed file formats in Linux systems:
Files compressed by .z compress programs; files compressed by .zip zip programs; files compressed by .gz gzip programs; files compressed by .bz2 bzip2 programs; files compressed by .xz xz programs; data packaged by .tar tar programs are not compressed; data packaged by .tar.gz tar programs are compressed by gzip; data packaged by .tar.bz2 tar programs are compressed by bzip2 Data packaged by .tar.xz tar program and compressed by xz; 1. Gzip command:
Common options:
-c: output compressed data to the screen, which can be processed by data flow redirection;-d: decompressed parameters;-t: can be used to check the consistency of a compressed file ~ to see if there are any errors in the file;-v: it can display information such as the compression ratio of the original file / compressed file. -#: # means a number and represents the compression level.-1 is the fastest, but the compression ratio is the worst,-9 is the slowest, but the compression ratio is the best. The default is-6.
Examples are as follows:
(1) Compression:
[root@localhost ~] # cp / etc/passwd / mnt # copy a file [root@localhost ~] # gzip-v / mnt/passwd # to compress / mnt/passwd: 61.1%-- replaced with / mnt/passwd.gz [root@localhost ~] # ll / etc/passwd / mnt/passwd* # compare the size of this file before and after compression-rw-r--r--. 1 root root 2311 July 14 19:02 / etc/passwd-rw-r--r--. 1 root root 923 August 20 22:34 / mnt/passwd.gz
(2) View the contents of the compressed file:
[root@localhost ~] # zcat / mnt/passwd.gz
(3) decompress the compressed file:
[root@localhost ~] # gzip-d / mnt/passwd.gz [root@localhost ~] # ls / mntpasswd2, bzip command:
Common options:
-c: output the data generated by the compression process to the screen;-d: unzipped parameters;-k: keep the original file without deleting the original file;-z: compressed parameters (default value, can not be added). -v: can display the original file / compressed file compression ratio and other information;-#: # means a number, representing the compression level,-1 is the fastest, but the compression ratio is the worst,-9 is the slowest, but the compression ratio is the best, the default is-6.
Examples are as follows:
(1) Compression
[root@localhost ~] # bzip2-v / mnt/passwd # Compression [root@localhost ~] # ls / mnt # View the compressed file passwd.bz2
(2) View the contents of the compressed file:
[root@localhost ~] # bzcat / mnt/passwd.bz2 # outputs file information on the screen
(3) decompression
[root@localhost ~] # bzip2-d / mnt/passwd.bz2 # decompress [root@localhost ~] # ls / mnt # View passwd3 and xz commands:
Common options:
-d: decompress the parameters;-t: test the integrity of the compressed file to see if there are any errors;-l: list the relevant information about the compressed file;-k: keep the original file without deletion;-c: the meaning of the data output from the screen;-#: # means the compression level,-1 is the fastest, but the compression ratio is the worst,-9 is the slowest, but the compression ratio is the best, the default is-6.
Examples are as follows:
(1) Compression:
[root@localhost ~] # xz-v / mnt/passwd [root@localhost ~] # ls / mntpasswd.xz
(2) View:
[root@localhost ~] # xz-l / mnt/passwd.xz # View the size of the file before and after compression [root@localhost ~] # xzcat / mnt/passwd.xz # display the file information on the screen
(3) decompress and view
[root@localhost ~] # xz-d / mnt/passwd.xz [root@localhost ~] # ls / mntpasswd4, tar archiving command
Common options:
-c: create a packaged file, which can be paired with-v to view the name of the packaged file during the process;-C: specify the target folder to release when decompressing. -f: indicates the use of archived files, usually followed by the name of the file being processed. -j: call the bzip2 program to compress or decompress. -J: compress or decompress through the xz program. -p: keep the permissions of files and directories when packing. -P: keep the absolute path of files and directories when packing. -t: list to view the files in the package. -v: outputs the details of the decompression / compression process. -x: unlock the package file in .tar format. -z: call the gzip program to compress or decompress.
Examples of use:
[root@localhost ~] # tar jcf filename.tar.bz2 File or directory to be compressed # Compression [root@localhost ~] # tar jtvf filename.tar.bz2 # View the contents of the compressed file [root@localhost ~] # tar jxvf filename.tar.bz2-C to which directory you want to extract 2, other common compression and backup tools 5, dd command (commonly used):
Common options:
If:input file (original file) can also be a device; of:output file (backed up file) can also be a device; bs: the size of a planned block (block), if not specified, the default is 512Bytes (bytes); count: the meaning of how many blocks.
Examples are as follows:
[root@localhost ~] # dd if=/dev/zero of=/a/123.txt bs=10M count=5# copies the information in the / dev/zero file to the / a directory to create a 123.txt file, 10m five times at a time, and the final size of the file is 50m. 6. Cpio command (not commonly used):
Backup common options:
-o: output data copy to a file or device;-B: increase the default Blocks to 5120 bytes, which is 512 bytes by default; (this has the advantage of speeding up the storage of large files).
Restore the options and parameters that will be used:-I: copy data from a file or device to the system;-d: automatically create a directory! The contents of the data backed up with cpio may not be in the same layer directory, so we must use the "- d" option to allow cpio to automatically create a new directory when restoring.
Some shareable options and parameters:-v: allow file names to be displayed on the screen during storage;-c: a newer way of protable format storage.
Examples are as follows:
(1) backup
To use this command, you need to use the find command to find it before you can export the file to the command to use
# copy all the files in the / boot directory to the / mnt directory using the command of cpio, and name them boot.cpio [root@localhost ~] # find / boot | cpio-ocvB > / mnt/boot.cpio [root@localhost ~] # ls-lh / mnt/boot.cpio-rw-r--r--. 1 0 root 140m August 20 23:30 / mnt/boot.cpio
(2) restore
[root@localhost ~] # cpio-idvc
< /mnt/boot.cpio #将压缩文件还原到原本的目录中[root@localhost ~]# find / | cpio -ocvB >/ dev/st0 # backup [root@localhost ~] # cpio-idvc < / dev/st0 # restore 3. Backup and restore of XFS file system 1. Backup of xfs file system-- xfsdump command:
Limitations of the xfsdump command:
Xfsdump does not support mounted file systems, so you can only back up mounted ones; Xfsdump must use the permissions of root to operate (involving file system relationships); Xfsdump can only back up xfs file systems; data backed up by Xfsdump (files or storage media) can only be decompressed by xfsrestore; Xfsdump distinguishes each backup file by the UUID of the file system, so you cannot back up two file systems with the same UUID.
Common options:
-L:xfsdump records the session header of each backup. You can enter a simple description for this file system here.-M:xfsdump can record the header of storage media. Here, you can enter a simple description of this media. -l: it is the lowercase of L, that is, the specified level. There are 10 grades in all. The default is 0, that is, full backup, and 1x9 refers to incremental backup. -f: somewhat similar to tar, followed by the resulting file, you can also take an example such as / dev/st0 device file name or other general file name;-I: uppercase "I" to list the current backup information status from / var/lib/xfsdump/inventory.
Examples are as follows:
[root@localhost ~] # xfsdump-l O-L boot_all-Mboot_all-f / srv/boot.dump / boot# record the filename of the full backup as / srv/boot.dump# if you enter interactive mode without adding-L and-M options xfsdump: using file dump (drive_simple) strategyxfsdump: version 3.1.4 (dump format 3.0)-type ^ C for status and controlxfsdump: WARNING: most recent level 0 dump was interrupted But not resuming that dump since resume (- R) option not specifiedxfsdump: level 0 dump of localhost.localdomain:/boot # start backing up native / boot system xfsdump: dump date: Wed Aug 21 00:04:00 2019 # backup time xfsdump: session id: 2ab52f27-54d8-4cb3-b99f-5f78601afeed # IDxfsdump of dump: session label: "boot_all" # simply give a name memory # start backup program xfsdump: ino map phase 1: constructing initial dump listxfsdump : ino map phase 2: skipping (no pruning necessary) xfsdump: ino map phase 3: skipping (only one dump stream) xfsdump: ino map construction completexfsdump: estimated dump size: 146934272 bytesxfsdump: creating dump session media file 0 (media 0 File 0) xfsdump: dumping ino mapxfsdump: dumping directoriesxfsdump: dumping non-directory filesxfsdump: ending media filexfsdump: media file size 146605160 bytesxfsdump: dump size (non-dir files): 146370000 bytesxfsdump: dump complete: 1 seconds elapsedxfsdump: Dump Summary:xfsdump: stream 0 / srv/boot.dump OK (success) xfsdump: Dump Status: SUCCESS [root@localhost ~] # xfsdump-I # check whether there are any files backed up by xfsdump. 2. Restore of xfs file system-xfsrestore command:
Common options are as follows:
-I: uppercase "I" has the same output as xfsdump! Can query backup data, including Label name and backup time, etc.-f: followed by backup files! Tape drives such as / dev/st0 are likely to be connected in the enterprise! -L: data that can be queried with "- I". Enter after this option;-s: you need to connect to a specific directory, and you can also restore a file or directory;-r: if you are using files to store backup data, this is not needed. If there are multiple files on a disk, you need to use it to achieve cumulative recovery;-I: enter interactive mode (generally not required)
Examples are as follows:
[root@localhost ~] # xfsrestore-I # View backup file data [root@localhost ~] # xfsrestore-f / srv/boot.dump-L boot_all / boot# overwrite backup data directly
-this is the end of this article. Thank you for reading-