How to use tar command to back up Linux Cloud Server?

11-09-2023

Basic CommandExecute the following command to back up the entire file system:

sudo /usr/bin/tar -czpvf /home/zhaomu/backup/linux_backup.tar.gz /

The parameters are described as follows:

-c: means filing.

-z: backup in gzip format. The backup speed of gzip is faster, but the backup files generated by other methods are also larger.

-p: Keep the permissions of the file while backing up, so as not to cause permission problems when restoring.

-v: Displays the details of the backup process.

-f: Specify the directory and file name of the backup.

/:means to back up the entire file system.

Enhanced command 1. Exclude files that don't need to be backed up.

The above backup command is not the best solution, because there are a lot of files in the whole file system that don't need to be backed up, including system files, temporary files, historical backup files, etc. So we need to exclude all these documents.

sudo /usr/bin/tar --exclude-from=/home/zhaomu/exclude.txt -czpvf /home/zhaomu/backup/linux_backup.tar.gz /

The --exclude-from instruction is used here to define the path of the excluded file. The contents of a typical exclusion file are as follows:

/home/zhaomu/backup/*/tmp/*/proc/*/dev/*/sys/*/run/*/var/tmp/*/var/run/*/var/lock/*

First of all, the files in the /home/zhaomu/backup directory need to be excluded, otherwise the previously backed-up data will be backed up repeatedly, resulting in an increasingly large backup file. Secondly, in the Linux system, tmp, proc, dev, sys, and run all belong to dynamically created directories, and they don't need to be backed up, but they need to be preserved, so we exclude the files in these directories, but not the whole directory.

2. Add a date to the file name

By adding a date to the file name, you can distinguish the backups created at different times and establish a backup chain mechanism.

sudo /usr/bin/tar --exclude-from=/home/zhaomu/exclude.txt -czpvf /home/zhaomu/backup/linux_backup-$(date +%F-%H-%M).tar.gz /

Where $(date +%F-%H-%M) represents the current date, current hour and current minute, for example, Linux _ backup-2020-03-02-15-22.tar.gz.

3. Use xz compression algorithm.

We changed the -z instruction to the -J instruction, and the file extension was changed from tar.gz to tar.xz.. Xz compression algorithm is slower than gzip compression algorithm, but the compression ratio is higher, so the backup file created is smaller.

sudo /usr/bin/tar --exclude-from=/home/zhaomu/exclude.txt -cJpvf /home/zhaomu/backup/linux_backup-$(date +%F-%H-%M).tar.xz /

Script integration We can integrate the above commands into a script for execution:

vi /home/zhaomu/bin/linux_backup.sh

The content of the script is as follows:

#! bin/sh_tarfile=/home/zhaomu/backup/linux_backup-$(date +%F-%H-%M).tar.xzsudo /usr/bin/tar --exclude-from=/home/zhaomu/exclude.txt -cJpvf $ /

Give script execution permission:

sudo chmod +x /home/zhaomu/bin/linux_backup.sh

Run the script to start the backup:

sh /home/zhaomu/bin/linux_backup.sh

Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us