运维工程师都知道,全备和增量备份 Linux 系统脚本编写

在 Linux 中,我们经常需要备份系统重要文件,例如 /etc 、/boot 分区、重要网站数据等等,如果每天都完整备份的话,会占用很大的
空间,那我们改如何来备份呢?每周日进行完整备份,其余每天为增量备份。

全备份:直接备份一个整体
增量备份:在全备份的基础上再次备份

全备份实例
把整个目录备份下来

[root@a shell]# tar -g /tmp/system_data -czvf /tmp/system_bak.tar.gz /shell
tar: /shell: Directory is new
tar: Removing leading `/' from member names
/shell/
/shell/.auto.sh.swp
/shell/a.txt
/shell/awkprof.out
/shell/b.txt
[root@a tmp]# ls
system_bak.tar.gz  system_data
[root@a tmp]# tar xzf system_bak.tar.gz 
[root@a tmp]# ls shell
a.txt  awkprof.out  b.txt

增量备份
只备份新添加的目录

[root@a shell]# touch qqq.txt
[root@a shell]# mkdir aaaa
/shell/aaaa: Directory is new
tar: Removing leading `/' from member names
/shell/
/shell/aaaa/
/shell/qqq.txt
[root@a shell]# cd /tmp
[root@a tmp]# ls
system_add_data.tar.gz  system_bak.tar.gz  system_data
[root@a tmp]# tar xzf system_add_data.tar.gz 
[root@a tmp]# mv shell/ shell_add_bak
[root@a tmp]# ls shell_add_bak/
aaaa  qqq.txt

每周日进行完整备份,其余每天为增量备份。
这个功能我们怎么在shell脚本中实现呢?

[root@a shell]# cat a.sh 
#! /bin/bash
# 20200525
# by caq
# fighting

backdir=$*
targetdir=/date/backup
year=`date +%Y`
month=`date +%m`
day=`date +%d`
week=`date +%u`
sysfiles=system_backup.tgz
addfiles=addsystem_backup.tgz

if [ -z "$*" ];then 
    echo "please your parameter[backdir{/etc|/boot|..}]"
    exit 
fi

if [ ! -d $targetdir/$year/$month/$day ];then
	mkdir -p $targetdir/$year/$month/$day
fi
	
full_back()
{
	if [ $week -eq 7 ];then
		cd $targetdir/$year/$month/$day;
		tar -g $targetdir/snapshot -czvf $files $backdir
		if [ $? = 0 ];then
			echo "sysback successful!"
		fi
	fi	
}
add_back()
{
	if [ $week -ne 7 ];then
		cd $targetdir/$year/$month/$day;
		tar -g $targetdir/snapshot -czvf $addfiles $backdir
		if [ $? = 0 ];then
                	echo "addback successful!"
                fi
	fi 
}
full_back;add_back
进行测试
[root@a shell]# cd /date/backup/2020/05/23/
[root@a 23]# ls
addsystem_backup.tgz

猜你喜欢

转载自blog.csdn.net/qq_45714272/article/details/106375993
今日推荐