TAR 实现 Linux 操作系统自动备份

引用转载内容https://blog.51cto.com/wgkgood/1330200

TAR 实现 Linux 操作系统自动备份

Tar 命令工具除了用于日常打包、解压源码包或者压缩包之外,最大的亮点是还可以用于 Linux 操作系统文件及目录的备份,使用 tar -g 可以基于 GNU 格式的增量备份,备份原理是基于检查目录或者文件的 atime、mtime、ctime 属性是否被修改。文件及目录时间属性详解如下:
❑ 文件被访问的时间(Access time,atime);
❑ 文件内容被改变的时间(Modified time,mtime);
❑ 文件写入、权限更改的时间(Change time,ctime)。
总结,更改文件内容 mtime 和 ctime 都会改变,但 ctime 可以在 mtime 未发生变化时被更改,例如修改文件权限,文件 mtime 时间不变,而 ctime 时间改变。
TAR 增量备份案例演示步骤如下:

  1. /root 目录创建 test 文件夹,同时在 test 文件夹中,新建test1.txt,test2.txt 文件,如图所示:
[root@hecs-x-medium-2-linux-20200611091300 ~]# cd /root
[root@hecs-x-medium-2-linux-20200611091300 ~]# ls
[root@hecs-x-medium-2-linux-20200611091300 ~]# mkdir test
[root@hecs-x-medium-2-linux-20200611091300 ~]# cd test/
[root@hecs-x-medium-2-linux-20200611091300 test]# touch test1.txt test2.txt
[root@hecs-x-medium-2-linux-20200611091300 test]# ls
test1.txt  test2.txt

  1. 使用 tar 命令第一次完整备份 test 文件夹中的内容,-g 指定快照 snapshot文件,第一次没有该文件则会自动创建,如图所示
[root@hecs-x-medium-2-linux-20200611091300 test]# pwd
/root/test
[root@hecs-x-medium-2-linux-20200611091300 test]# tar -g /data/backup/snapshot -czvf /data/backup/2020test.tar.gz *
test1.txt
test2.txt
[root@hecs-x-medium-2-linux-20200611091300 test]# cat /data/backup/snapshot
GNU tar-1.26-2
1592364124756978502[root@hecs-x-medium-2-linux-20200611091300 test]#
  1. 使用 tar 命令第一次完整备份 test文件夹中之后,会生成快照文件:/data/backup/snapshot,后期增量备份会以 snapshot 文件为参考,在 test文件夹中再创建 test3.txt test4.txt 文件,然后通过 tar 命令增量备份 test目录所有内容,如图所示:
[root@hecs-x-medium-2-linux-20200611091300 test]# ls
test1.txt  test2.txt
[root@hecs-x-medium-2-linux-20200611091300 test]# pwd
/root/test
[root@hecs-x-medium-2-linux-20200611091300 test]# ls
test1.txt  test2.txt
[root@hecs-x-medium-2-linux-20200611091300 test]# touch test3.txt test4.txt
[root@hecs-x-medium-2-linux-20200611091300 test]# ls
test1.txt  test2.txt  test3.txt  test4.txt
[root@hecs-x-medium-2-linux-20200611091300 test]# tar -g /data/backup/snapshot -czvf /data/backup/2020test_add1.tar.gz *
test3.txt
test4.txt
[root@hecs-x-medium-2-linux-20200611091300 test]# 

如上图 所示,增量备份时,需-g 指定第一次完整备份的快照 snapshot 文件,同时增量打包的文件名不能跟第一次备份后的文件名重复,通过 tar –tf 可以查看打包后的文件内容

[root@hecs-x-medium-2-linux-20200611091300 test]# tar -tf /data/backup/2020test_add1.tar.gz
test3.txt
test4.txt

在test4.txt文件中插入一部分内容,再新建test5.txt文件,修改test1.txt文件权限,再进行一次增量备份

[root@hecs-x-medium-2-linux-20200611091300 test]# ls
test1.txt  test2.txt  test3.txt  test4.txt
[root@hecs-x-medium-2-linux-20200611091300 test]# vi test4.txt ##插入内容
[root@hecs-x-medium-2-linux-20200611091300 test]# touch test5.txt
[root@hecs-x-medium-2-linux-20200611091300 test]# ls
test1.txt  test2.txt  test3.txt  test4.txt  test5.txt
[root@hecs-x-medium-2-linux-20200611091300 test]# cat test3.txt 
[root@hecs-x-medium-2-linux-20200611091300 test]# chmod 777 test1.txt ##修改权限
[root@hecs-x-medium-2-linux-20200611091300 test]# tar -g /data/backup/snapshot -czvf /data/backup/2020test_add2.tar.gz *
test4.txt
test5.txt
test1.txt
[root@hecs-x-medium-2-linux-20200611091300 test]# tar -tf /data/backup/2020test_add2.tar.gz
test4.txt
test5.txt
test1.txt

Shell+TAR 实现增量备份

企业中日常备份的数据包括/boot、/etc、/root、/data 目录等,备份的策略参考:每周 1-6 执行增量备份,每周日执行全备份。同时在企业中备份操作系统数据均使用Shell 脚本完成,此处auto_backup.sh 脚本供参考

#!/bin/sh
#define
SOURCE_DIR=(
    $*
)
TARGET_DIR=/data/backup/
YEAR=`date +%Y`
MONTH=`date +%m`
DAY=`date +%d`
WEEK=`date +%u`
FILES=system_backup.tgz
CODE=$?
if
    [ -z "$*" ];then
    echo -e "Please Enter Your Backup Files or Directories\n--------------------------------------------\nExample $0 /boot /etc ......"
    exit
fi
#Determine Whether the Target Directory Exists
if
    [ ! -d $TARGET_DIR/$YEAR/$MONTH/$DAY ];then
    mkdir -p $TARGET_DIR/$YEAR/$MONTH/$DAY
    echo "This $TARGET_DIR is Created Successfully !"
fi
#EXEC Full_Backup Function Command
Full_Backup()
{
if
    [ "$WEEK" -eq "7" ];then
    rm -rf $TARGET_DIR/snapshot
    cd $TARGET_DIR/$YEAR/$MONTH/$DAY ;tar -g $TARGET_DIR/snapshot -czvf $FILES `echo ${SOURCE_DIR[@]}`
    [ "$CODE" == "0" ]&&echo -e  "--------------------------------------------\nThese Full_Backup System Files Backup Successfully !"
fi
}
#Perform incremental BACKUP Function Command
Add_Backup()
{
   cd $TARGET_DIR/$YEAR/$MONTH/$DAY ;
if
    [ -f $TARGET_DIR/$YEAR/$MONTH/$DAY/$FILES ];then
    read -p "These $FILES Already Exists, overwrite confirmation yes or no ? : " SURE
    if [ $SURE == "no" -o $SURE == "n" ];then
    sleep 1 ;exit 0
    fi
#Add_Backup Files System
    if
        [ $WEEK -ne "7" ];then
        cd $TARGET_DIR/$YEAR/$MONTH/$DAY ;tar -g $TARGET_DIR/snapshot -czvf $$_$FILES `echo ${SOURCE_DIR[@]}`
        [ "$CODE" == "0" ]&&echo -e  "-----------------------------------------\nThese Add_Backup System Files Backup Successfully !"
   fi
else
   if
      [ $WEEK -ne "7" ];then
      cd $TARGET_DIR/$YEAR/$MONTH/$DAY ;tar -g $TARGET_DIR/snapshot -czvf $FILES `echo ${SOURCE_DIR[@]}`
      [ "$CODE" == "0" ]&&echo -e  "-------------------------------------------\nThese Add_Backup System Files Backup Successfully !"
   fi
fi
}
Full_Backup;Add_Backup

增加到定时任务中执行

crontab -e
0 1 * * * /bin/sh /data/sh/auto_backup.sh /boot /etc/ >> /tmp/back.log 2>&1

Tar 命令参数详解

-A, --catenate, --concatenate 将存档与已有的存档合并
-c, --create 建立新的存档
-d, --diff, --compare 比较存档与当前文件的不同之处
--delete 从存档中删除
-r, --append 附加到存档结尾
-t, --list 列出存档中文件的目录
-u, --update 仅将较新的文件附加到存档中
-x, --extract, --get 解压文件
-j, --bzip2, --bunzip2 有 bz2 属性的软件包;
-z, --gzip, --ungzip 有 gz 属性的软件包;
-b, --block-size N 指定块大小为 Nx512 字节(缺省时 N=20);
-B, --read-full-blocks 读取时重组块;
-C, --directory DIR 指定新的目录;
--checkpoint 读取存档时显示目录名;
-f, --file [HOSTNAME:]F 指定存档或设备,后接文件名称;
--force-local 强制使用本地存档,即使存在克隆;
-G, --incremental 建立老 GNU 格式的备份;
-g, --listed-incremental 建立新 GNU 格式的备份;
-h, --dereference 不转储动态链接,转储动态链接指向的文件;
-i, --ignore-zeros 忽略存档中的 0 字节块(通常意味着文件结
束);
--ignore-failed-read 在不可读文件中作 0 标记后再退出;
-k, --keep-old-files 保存现有文件;从存档中展开时不进行覆盖;
-K, --starting-file F 从存档文件 F 开始;
-l, --one-file-system 在本地文件系统中创建存档;
-L, --tape-length N 在写入 N*1024 个字节后暂停,等待更换磁盘;
-m, --modification-time 当从一个档案中恢复文件时,不使用新的时间
标签;
-M, --multi-volume 建立多卷存档,以便在几个磁盘中存放;
-O, --to-stdout 将文件展开到标准输出;
-P, --absolute-paths 不要从文件名中去除 '/';
-v, --verbose 详细显示处理的文件;
--version 显示 tar 程序的版本号;
--exclude FILE 不把指定文件包含在内;
-X, --exclude-from FILE 从指定文件中读入不想包含的文件的列表。

TAR 案例演示

tar -cvf test.tar.gz test #查看test.tar.gz包中内容;
tar -rf test.tar.gz test.txt #将test.txt文件追加到test.tar.gz 中
tar -xvf test.tar.gz #解压test.tar.gz程序包;
tar -czvf test.tar.gz test #使用gzip 格 式打 包并 压 缩test目录;
tar -cjvf test.tar.bz2 test #使用bzip2格式打包并压缩test目录;
tar -czf test.tar.gz * -X list.txt #使用 gzip 格式打包并压当前目录所有文件,排除list.txt 中记录的文件;
tar -czf test.tar.gz * --exclude=zabbix-3.2.4.tar.gz --exclude=nginx-1.12.0.tar.gz #使用gzip格式打包并压当前目录所有文件及目录,排除 zabbix-3.2.4.tar.gz 和 nginx-1.12.0.tar.gz 软件包。
  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值