备份脚本

2023年 7月 15日 25.7k 0

一、日常备份

如果你正在使用 Linux 系统处理一个重要的项目,那么可以创建一个 shell 脚本来自动备份特定目录。这有助于避免从主归档文件( main archive file )执行耗时的恢复过程。

1.1 基本功能拆解

在 Linux 世界中,备份数据的工作是由 tar 命令完成的。

  • 先来看一个使用 tar 命令来创建工作目录归档文件的例子:
# 1.列出待归档的文件。
[root@VM-8-11-centos tmp]# ls
a.txt  b.txt  c.txt  dir1  dir2  error.log  info.log  service.log  test.txt
[root@VM-8-11-centos tmp]# 

# 2.创建归档。
# 注意:(tar)命令会显示一条警告消息,指出它删除了路径开头的斜线。这意味着将路径从绝对路径改为了相对路径,以便将(tar)归档文件提取到文件系统中的任何位置。
[root@VM-8-11-centos tmp]# tar -cf archive.tar /home/tmp/*.*
tar: Removing leading `/' from member names
[root@VM-8-11-centos tmp]# 

# 3.列出创建完成的归档。
[root@VM-8-11-centos tmp]# ls archive.tar 
archive.tar
[root@VM-8-11-centos tmp]# 
  • 如果不想在脚本中输出警告消息,则可以将 STDERR 重定向到 /dev/null 文件:
# 1.创建归档时,重定向警告信息。
[root@VM-8-11-centos tmp]# tar -cf archive.tar /home/tmp/*.* 2>/dev/null 
[root@VM-8-11-centos tmp]# 

# 2.列出创建完成的归档。
[root@VM-8-11-centos tmp]# ls archive.tar 
archive.tar
[root@VM-8-11-centos tmp]# 
  • 由于 tar 归档文件会占用大量的磁盘空间,因此最好压缩一下。对此,我们可以使用 -z 选项。(会使用 gzip 压缩 tar 归档文件)
  • 另外,采用 .tar.gz 或 .tgz 表示是经过 gzip 压缩过的文件:
# 1.加入(-z)选项,并以(.tgz)命名创建压缩归档。
[root@VM-8-11-centos tmp]# tar -zcf archive.tgz /home/tmp/*.* 2>/dev/null
[root@VM-8-11-centos tmp]# 

# 2.经过压缩后的归档(.tgz后缀),字节数明显小于未压缩归档(.tar后缀)。
[root@VM-8-11-centos tmp]# ls -l archive.*
-rw-rw-r--+ 1 root root 10240 Jul 10 15:39 archive.tar
-rw-rw-r--+ 1 root root   900 Jul 10 15:47 archive.tgz
[root@VM-8-11-centos tmp]# 
  • 配置文件中包含我们希望纳入归档的所有目录的绝对路径:
# 1.查看配置文件内容。(Does_not_exist 表示不存在的路径,用于稍后脚本的测试。)
[root@VM-8-11-centos tmp]# cat file_path.conf 
/home/jan/BackupScriptProject/ 
/home/jan/Downloads/ 
/home/jan/Does_not_exist/ 
/home/jan/PythonConversion/
[root@VM-8-11-centos tmp]# 
  • 为了让脚本读取配置文件,将每个目录名加入归档列表中,这需要用到 read 命令。并使用 exec 命令来重定向标准输入(STDIN):
# $config_file:读取配置文件的每一条记录。
exec 0+  day|01 |
                                  +---------+         |      +---------+
                                  |         |         |
                     +------------+   01月  +---------+
                     |            |         |         |
     基点            |            +---------+         |      +---------+
                     |                                +----->+  day|02 |
 +----------+        |            +---------+                +---------+
 |          |        |            |         |
 |          +---------------------+   02月  |
 |          |        |            |         |
 +----------+        |            +---------+
                     |
/archive/hourly      |            +---------+
                     |            |         |
                     +------------+   ...   |
                                  |         |
                                  +---------+

  • 这个归档目录包含了与一年中的各个月份相对应的子目录并以月份命名,而每月的目录中又包含与当月各天相对应的子目录并以天的序号命名。

  • 这样你只需给每个归档文件加上时间戳,然后将其放入与月日对应的目录中即可。

  • 首先,必须创建新目录 /archive/hourly 并设置适当的权限:

# 1.创建hourly目录。
[root@VM-8-11-centos ~]# sudo mkdir /archive/hourly
[root@VM-8-11-centos ~]# 

# 2.关联该目录的所属组(Archivers)。
[root@VM-8-11-centos ~]# sudo chgrp Archivers /archive/hourly
[root@VM-8-11-centos ~]# 

# 3.列出目录本身的信息,包括所有权信息和所属组信息。
[root@VM-8-11-centos ~]# ls -ogd /archive/hourly/
drwxr-xr-x 2 4096 Jul 14 11:09 /archive/hourly/
[root@VM-8-11-centos ~]# 

# 4.赋予主和组的读写执行权限。
[root@VM-8-11-centos ~]# sudo chmod 775 /archive/hourly
[root@VM-8-11-centos ~]# 

# 5.权限赋予完成。
[root@VM-8-11-centos ~]# ls -ogd /archive/hourly/
drwxrwxr-x 2 4096 Jul 14 11:09 /archive/hourly/
[root@VM-8-11-centos ~]# 
  • 新目录设置好之后,需要将按小时归档的配置文件 file_path.conf 移到该目录中:
# 1.当前所在目录。
[root@VM-8-11-centos archive]# pwd
/archive
[root@VM-8-11-centos archive]# 

# 2.将按小时归档的配置文件移到当前目录。
[root@VM-8-11-centos archive]# mv /archive/file_path.conf /archive/hourly/
[root@VM-8-11-centos archive]# 

# 3.配置文件移动完成。
[root@VM-8-11-centos archive]# cd /archive/hourly/
[root@VM-8-11-centos archive]# 
[root@VM-8-11-centos hourly]# ls
file_path.conf
[root@VM-8-11-centos hourly]#  
  • 只需要修改脚本的相关信息部分(处理逻辑部分与按日归档的脚本一致):
[root@VM-8-11-centos hourly]# cat Hourly_Archive.sh 
#!/bin/bash
####################  相关信息  ####################
# 基点目录。
basedest=/archive/hourly
# 月日及时分。
month=$(date +%m)
day=$(date +%d)
time=$(date +%k%M)
# 递归创建目录及子目录。
mkdir -p $basedest/$month/$day
# 配置文件及归档的目标目录。
config_file=/archive/hourly/file_path.conf
destination=$basedest/$month/$day/archive$time.tar.gz

####################  处理逻辑  ####################
...

2.2 运行按小时归档的脚本

最好通过 date 命令来获取当前的小时数和分钟数,有了这些信息才能验证最终的归档文件名的正确性。

  • 运行脚本:
# 1.赋予执行权限。
[root@VM-8-11-centos hourly]# chmod u+x Hourly_Archive.sh 
[root@VM-8-11-centos hourly]# 

# 2.先通过命令检查当前的时和分。
[root@VM-8-11-centos hourly]# date +%k%M
1419
[root@VM-8-11-centos hourly]# 

# 3.运行脚本。
[root@VM-8-11-centos hourly]# ./Hourly_Archive.sh 


/home/jan/Does_not_exist/, does not exist.
Obviously, I will not include it in this archive.
It is listed on line 3 of the config file.
Continuing to build archive list...

Starting archive...

Archive completed
Resulting archive file is: /archive/hourly/07/14/archive1419.tar.gz

[root@VM-8-11-centos hourly]# 

# 4.查看 tar 压缩文件的内容。
[root@VM-8-11-centos hourly]# tar -tf /archive/hourly/07/14/archive1419.tar.gz 
home/jan/BackupScriptProject/
home/jan/BackupScriptProject/1.txt
home/jan/Downloads/
home/jan/Downloads/2.txt
home/jan/PythonConversion/
home/jan/PythonConversion/3.txt
[root@VM-8-11-centos hourly]# 

三、结束语

“-------怕什么真理无穷,进一寸有一寸的欢喜。”

微信公众号搜索:饺子泡牛奶。

相关文章

JavaScript2024新功能:Object.groupBy、正则表达式v标志
PHP trim 函数对多字节字符的使用和限制
新函数 json_validate() 、randomizer 类扩展…20 个PHP 8.3 新特性全面解析
使用HTMX为WordPress增效:如何在不使用复杂框架的情况下增强平台功能
为React 19做准备:WordPress 6.6用户指南
如何删除WordPress中的所有评论

发布评论