内容简介:本文风哥教程参考Linux官方文档、Red Hat Enterprise Linux官方文档、Ansible Automation Platform官方文档、Docker官方文档、Kubernetes官方文档和Podman官方文档等内容,详细介绍了相关技术的配置和使用方法。学习交流加群风哥微信: itpux-com
本文档详细介绍tar和rsync命令
风哥提示:
的使用方法,包括数据备份、压缩、同步等操作。
Part01-tar命令基础
1.1 tar命令常用选项
# -c: 创建归档文件
# -x: 解压归档文件
# -t: 列出归档文件内容
# -v: 显示详细过程
# -f: 指定归档文件名
# -z: 使用gzip压缩
# -j: 使用bzip2压缩
# -J: 使用xz压缩
# -C: 指定解压目录
# –exclude: 排除文件或目录
# 查看tar命令帮助
$ tar –help | head -30
GNU `tar’ saves many files together into a single tape or disk archive, and can
restore individual files from the archive.
Usage: tar [OPTION…] [FILE]…
Examples:
tar -cf archive.tar foo bar # Create archive.tar from files foo and bar.
tar -tvf archive.tar # List all files in archive.tar verbosely.
tar -xf archive.tar # Extract all files from archive.tar.
Main operation mode:
-A, –catenate, –concatenate append tar files to an archive
-c, –create create a new archive
-d, –diff, –compare find differences between archive and file system
–delete delete from the archive (not on mag tapes!)
-r, –append append files to the end of an archive
-t, –list list the contents of an archive
Part02-tar备份操作
2.1 创建归档文件
$ mkdir -p /tmp/test_backup/{dir1,dir2,dir3}
$ touch /tmp/test_backup/file{1,2,3}.txt
$ touch /tmp/test_backup/dir1/file{4,5}.txt
$ touch /tmp/test_backup/dir2/file{6,7}.txt
$ touch /tmp/test_backup/dir3/file{8,9}.txt
# 查看测试目录结构
$ tree /tmp/test_backup/
/tmp/test_backup/
├── dir1
│ ├── file4.txt
│ └── file5.txt
├── dir2
│ ├── file6.txt
│ └── file7.txt
├── dir3
│ ├── file8.txt
│ └── file9.txt
├── file1.txt
├── file2.txt
└── file3.txt
3 directories, 9 files
# 创建tar归档文件
$ tar -cvf /tmp/backup.tar /tmp/test_backup/
tar: Removing leading ‘/’ from member names
/tmp/test_backup/
/tmp/test_backup/dir1/
/tmp/test_backup/dir1/file4.txt
/tmp/test_backup/dir1/file5.txt
/tmp/test_backup/dir2/
/tmp/test_backup/dir2/file6.txt
/tmp/test_backup/dir2/file7.txt
/tmp/test_backup/dir3/
/tmp/test_backup/dir3/file8.txt
/tmp/test_backup/dir3/file9.txt
/tmp/test_backup/file1.txt
/tmp/test_backup/file2.txt
/tmp/test_backup/file3.txt
# 查看归档文件
$ ls -lh /tmp/backup.tar
-rw-r–r–. 1 user user 10K Apr 3 15:00:00 /tmp/backup.tar
# 查看归档文件内容
$ tar -tvf /tmp/backup.tar
drwxrwxr-x user/user 0 2026-04-03 15:00 tmp/test_backup/
drwxrwxr-x user/user 0 2026-04-03 15:00 tmp/test_backup/dir1/
-rw-r–r– user/user 0 2026-04-03 15:00 tmp/test_backup/dir1/file4.txt
-rw-r–r– user/user 0 2026-04-03 15:00 tmp/test_backup/dir1/file5.txt
drwxrwxr-x user/user 0 2026-04-03 15:00 tmp/test_backup/dir2/
-rw-r–r– user/user 0 2026-04-03 15:00 tmp/test_backup/dir2/file6.txt
-rw-r–r– user/user 0 2026-04-03 15:00 tmp/test_backup/dir2/file7.txt
drwxrwxr-x user/user 0 2026-04-03 15:00 tmp/test_backup/dir3/
-rw-r–r– user/user 0 2026-04-03 15:00 tmp/test_backup/dir3/file8.txt
-rw-r–r– user/user 0 2026-04-03 15:00 tmp/test_backup/dir3/file9.txt
-rw-r–r– user/user 0 2026-04-03 15:00 tmp/test_backup/file1.txt
-rw-r–r– user/user 0 2026-04-03 15:00 tmp/test_backup/file2.txt
-rw-r–r– user/user 0 2026-04-03 15:00 tmp/test_backup/file3.txt
2.2 创建压缩归档文件
$ tar -czvf /tmp/backup.tar.gz /tmp/test_backup/
tar: Removing leading ‘/’ from member names
/tmp/test_backup/
/tmp/test_backup/dir1/
/tmp/test_backup/dir1/file4.txt
/tmp/test_backup/dir1/file5.txt
/tmp/test_backup/dir2/
/tmp/test_backup/dir2/file6.txt
/tmp/test_backup/dir2/file7.txt
/tmp/test_backup/dir3/
/tmp/test_backup/dir3/file8.txt
/tmp/test_backup/dir3/file9.txt
/tmp/test_backup/file1.txt
/tmp/test_backup/file2.txt
/tmp/test_backup/file3.txt
# 查看压缩归档文件大小
$ ls -lh /tmp/backup.tar*
-rw-r–r–. 1 user user 10K Apr 3 15:00:00 /tmp/backup.tar
-rw-r–r–. 1 user user 1K Apr 3 15:01:00 /tmp/backup.tar.gz
# 创建bzip2压缩归档
$ tar -cjvf /tmp/backup.tar.bz2 /tmp/test_backup/
tar: Removing leading ‘/’ from member names
/tmp/test_backup/
/tmp/test_backup/dir1/
/tmp/test_backup/dir1/file4.txt
/tmp/test_backup/dir1/file5.txt
/tmp/test_backup/dir2/
/tmp/test_backup/dir2/file6.txt
/tmp/test_backup/dir2/file7.txt
/tmp/test_backup/dir3/
/tmp/test_backup/dir3/file8.txt
/tmp/test_backup/dir3/file9.txt
/tmp/test_backup/file1.txt
/tmp/test_backup/file2.txt
/tmp/test_backup/file3.txt
# 查看所有归档文件大小
$ ls -lh /tmp/backup.tar*
-rw-r–r–. 1 user user 10K Apr 3 15:00:00 /tmp/backup.tar
-rw-r–r–. 1 user user 1K Apr 3 15:01:00 /tmp/backup.tar.gz
-rw-r–r–. 1 user user 1K Apr 3 15:02:00 /tmp/backup.tar.bz2
Part03-tar恢复操作
3.1 解压归档文件
$ rm -rf /tmp/test_backup
# 解压tar归档文件
$ tar -xvf /tmp/backup.tar -C /tmp/
tmp/test_backup/
tmp/test_backup/dir1/
tmp/test_backup/dir1/file4.txt
tmp/test_backup/dir1/file5.txt
tmp/test_backup/dir2/
tmp/test_backup/dir2/file6.txt
tmp/test_backup/dir2/file7.txt
tmp/test_backup/dir3/
tmp/test_backup/dir3/file8.txt
tmp/test_backup/dir3/file9.txt
tmp/test_backup/file1.txt
tmp/test_backup/file2.txt
tmp/test_backup/file3.txt
# 验证解压结果
$ tree /tmp/test_backup/
/tmp/test_backup/
├── dir1
│ ├── file4.txt
│ └── file5.txt
├── dir2
│ ├── file6.txt
│ └── file7.txt
├── dir3
│ ├── file8.txt
│ └── file9.txt
├── file1.txt
├── file2.txt
└── file3.txt
3 directories, 9 files
# 解压gzip压缩归档
$ tar -xzvf /tmp/backup.tar.gz -C /tmp/
# 解压bzip2压缩归档
$ tar -xjvf /tmp/backup.tar.bz2 -C /tmp/
# 解压单个文件
$ tar -xvf /tmp/backup.tar -C /tmp tmp/test_backup/file1.txt
tmp/test_backup/file1.txt
Part04-rsync命令基础
4.1 rsync命令常用选项
# -a: 归档模式,保留权限、时间戳等
# -v: 显示详细过程
# -z: 压缩传输
# -h: 人类可读格式
# –progress: 显示传输进度
# –delete: 删除目标端多余文件
# –exclude: 排除文件或目录
# –bwlimit: 限制带宽
# 查看rsync命令帮助
$ rsync –help | head -30
rsync version 3.2.7 protocol version 31
Copyright (C) 1996-2023 by Andrew Tridgell, Wayne Davison, and others.
Web site: https://rsync.samba.org/
Capabiliti学习交流加群风哥QQ113257174es:
64-bit files, 64-bit inums, 64-bit timestamps, 64-bit long ints,
socketpairs, hardlinks, hardlink-specials, symlinks, IPv6, atimes,
batchfiles, inplace, append, ACLs, xattrs, hidden protect, case-insensitive,
iconv, symtimes, prealloc, stop-at, no crtimes
Optimizations:
SIMD, asm, openssl-crypto
Checksum list:
md5, md4, sha1, sha256, sha512, sha3-224, sha3-256, sha3-384, sha3-512
Compress list:
zstd, lz4, zlibx, zlib
Part05-rsync同步操作
5.1 本地目录同步
$ mkdir -p /tmp/source/{dir1,dir2}
$ touch /tmp/source/file{1,2,3}.txt
$ touch /tmp/source/dir1/file{4,5}.txt
$ touch /tmp/source/dir2/file{6,7}.txt
$ mkdir -p /tmp/destination
# 同步目录(保留权限、时间戳等)
$ rsync -avh /tmp/source/ /tmp/destination/
sending incremental file list
./
file1.txt
file2.txt
file3.txt
dir1/
dir1/file4.txt
dir1/file5.txt
dir2/
dir2/file6.txt
dir2/file7.txt
sent 500 bytes received 200 bytes 1,400.00 bytes/sec
total size is 0 speedup is 0.00
# 验证同步结果
$ tree /tmp/destination/
/tmp/destination/
├── dir1
│ ├── file4.txt
│ └── file5.txt
├── dir2
│ ├── file6.txt
│ └── file7.txt
├── file1.txt
├── file2.txt
└── file3.txt
2 directories, 7 files
# 增量同步(只同步变化的文件)
$ touch /tmp/source/file8.txt
$ rsync -avh /tmp/source/ /tmp/destination/
sending incremental file list
./
file8.txt
sent 300 bytes received 100 bytes 800.00 bytes/sec
total size is 0 speedup is 0.00
# 验证增量同步结果
$ tree /tmp/destination/
/tmp/destination/
├── dir1
│ ├── file4.txt
│ └── file5.txt
├── dir2
│ ├── file6.txt
│ └── file7.txt
├── file1.txt
├── file2.txt
├── file3.txt
└── file8.txt
2 directories, 8 files
5.2 远程目录同步
$ rsync -avhz /tmp/source/ user@192.168.1.100:/backup/
user@192.168.1.100’s password:
sending incremental file list
./
file1.txt
file2.txt
file3.txt
file8.txt
dir1/
dir1/file4.更多视频教程www.fgedu.net.cntxt
dir1/file5.txt
dir2/
dir2/file6.txt
dir2/file7.txt
sent 1.00K bytes received 400 bytes 400.00 bytes/sec
total size is 0 speedup is 0.00
# 从远程服务器同步
$ rsync -avhz user@192.168.1.100:/backup/ /tmp/local_backup/
user@192.168.1.100’s password:
receiving incremental file list
./
file1.txt
file2.txt
file3.txt
file8.txt
dir1/
dir1/file4.txt
dir1/file5.txt
dir2/
dir2/file6.txt
dir2/file7.txt
sent 400 bytes received 1.00K bytes 400.00 bytes/sec
total size is 0 speedup is 0.00
# 使用SSH端口同步
$ rsync -avhz -e “ssh -p 2222” /tmp/source/ user@192.168.1.100:/backup/
# 显示传输进度
$ rsync -avhz –progress /tmp/source/ user@192.168.1.100:/backup/
Part06-实战案例
6.1 定期备份脚本
$ cat > /usr/local/bin/backup.sh << 'EOF' #!/bin/bash # 备份配置 BACKUP_DIR="/backup" SOURCE_DIR="/data" DATE=$(date +%Y%m%d_%H%M%S) BACKUP_FILE="backup_${DATE}.tar.gz" # 创建备份目录 mkdir -p $BACKUP_DIR # 执行备份 echo "Starting backup at $(date)" tar -czvf ${BACKUP_DIR}/${BACKUP_FILE} $SOURCE_DIR # 验证备份 if [ $? -eq 0 ]; then echo "Backup completed successfully" echo "Backup file: ${BACKUP_DIR}/${BACKUP_FILE}" ls -lh ${BACKUP_DIR}/${BACKUP_FILE} else echo "Backup failed" exit 1 fi # 删除7天前的备份 find $BACKUP_DIR -name "backup_*.tar.gz" -mtime +7 -delete # 同步到远程服务器 rsync -avhz ${BACKUP_DIR}/${BACKUP_FILE} user@192.168.1.100:/remote_backup/ echo "Backup and sync completfrom PG视频:www.itpux.comed at $(date)" EOF # 赋予执行权限 $ chmod +x /usr/local/bin/backup.sh # 执行备份脚本 $ sudo /usr/local/bin/backup.sh Starting backup at Thu Apr 3 15:10:00 CST 2026 /data/ /data/file1.txt /data/file2.txt /data/file3.txt Backup completed successfully Backup file: /backup/backup_20260403_151000.tar.gz -rw-r--r--. 更多学习教程公众号风哥教程itpux_com1 root root 1K Apr 3 15:10:00 /backup/backup_20260403_151000.tar.gz Backup and sync completed at Thu Apr 3 15:10:30 CST 2026 # 配置定时任务 $ sudo crontab -e 0 2 * * * /usr/local/bin/backup.sh >> /var/log/backup.log 2>&1
1. 使用tar进行完整备份
2. 使用rsync进行增量同步
3. 定期验证备份文件完整性
4. 配置定时任务自动备份
5. 备份文件异地存储
本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html
