How to Use the tar Tool to Backup Linux Cloud Servers?

17-01-2024 02:59:23

Regular backups are an essential part of daily maintenance for cloud servers. Technically, there are many backup solutions available, among which the tar command is widely adopted for its simplicity, high stability, and strong extensibility. This guide will explain how to backup Linux cloud servers using the tar command.

Basic Commands

Execute the following command to backup the entire file system:

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

The parameters are explained as follows:

  • -c: Represents archiving.
  • -z: Backup in gzip format. gzip backups are faster, but the backup files are larger compared to other methods.
  • -p: Preserve file permissions during backup to avoid permission issues during recovery.
  • -v: Display detailed information about the backup process.
  • -f: Specify the backup directory and filename.
  • /: Indicates backing up the entire file system.

Enhanced Commands

1. Excluding Unnecessary Files from Backup

The above backup command is not the optimal solution, as the entire file system contains many files that do not need to be backed up, including system files, temporary files, historical backup files, etc. Therefore, it's necessary to exclude these files.

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

The --exclude-from directive is used here to define the path of files to exclude. A typical exclusion file content is as follows:

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

First, files in the /home/zhaomu/backup directory should be excluded to prevent duplicating previously backed up data, which would increase the backup file size. Secondly, in Linux systems, tmp, proc, dev, sys, run are dynamically created directories that do not need backing up, but their structures should be preserved. Therefore, files under these directories are excluded, but not the entire directories.

2. Adding Dates to Filenames

By adding dates to filenames, backups created at different times can be distinguished, establishing 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 /

The parameter $(date +%F-%H-%M) represents the current date, hour, and minute, for example: linux_backup-2020-03-02-15-22.tar.gz

3. Using the xz Compression Algorithm

By replacing the -z directive with -J, and changing the file extension from tar.gz to tar.xz, we utilize the xz compression algorithm. Although xz is slower than gzip, it offers a higher compression ratio, resulting in smaller backup files.

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

Script Integration

These commands can be integrated into a script for execution:

vi /home/zhaomu/bin/linux_backup.sh

The script content is as follows:

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

Grant execution permissions to the script:

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

Run the script to start the backup:

sh /home/zhaomu/bin/linux_backup.sh