一、日常备份
如果你正在使用 Linux 系统处理一个重要的项目,那么可以创建一个 shell 脚本来自动备份特定目录。这有助于避免从主归档文件( main archive file )执行耗时的恢复过程。
1.1 基本功能拆解
在 Linux 世界中,备份数据的工作是由 tar
命令完成的。
- 先来看一个使用
tar
命令来创建工作目录归档文件的例子:
| |
| [root@VM-8-11-centos tmp] |
| a.txt b.txt c.txt dir1 dir2 error.log info.log service.log test.txt |
| [root@VM-8-11-centos tmp] |
| |
| |
| |
| [root@VM-8-11-centos 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 文件:
| |
| [root@VM-8-11-centos tmp] |
| [root@VM-8-11-centos tmp] |
| |
| |
| [root@VM-8-11-centos tmp] |
| archive.tar |
| [root@VM-8-11-centos tmp] |
- 由于
tar
归档文件会占用大量的磁盘空间,因此最好压缩一下。对此,我们可以使用 -z 选项。(会使用 gzip 压缩 tar
归档文件)
- 另外,采用 .tar.gz 或 .tgz 表示是经过 gzip 压缩过的文件:
| |
| [root@VM-8-11-centos tmp] |
| [root@VM-8-11-centos tmp] |
| |
| |
| [root@VM-8-11-centos tmp] |
| -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] |
- 配置文件中包含我们希望纳入归档的所有目录的绝对路径:
| |
| [root@VM-8-11-centos tmp] |
| /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 | |
| + |
| | | | |
| + |
| | | | | |
| 基点 | + |
| | + |
| + |
| | | | | | |
| | + |
| | | | | | |
| + |
| | |
| /archive/hourly | + |
| | | | |
| + |
| | | |
| + |
| |
-
这个归档目录包含了与一年中的各个月份相对应的子目录并以月份命名,而每月的目录中又包含与当月各天相对应的子目录并以天的序号命名。
-
这样你只需给每个归档文件加上时间戳,然后将其放入与月日对应的目录中即可。
-
首先,必须创建新目录 /archive/hourly 并设置适当的权限:
| |
| [root@VM-8-11-centos ~] |
| [root@VM-8-11-centos ~] |
| |
| |
| [root@VM-8-11-centos ~] |
| [root@VM-8-11-centos ~] |
| |
| |
| [root@VM-8-11-centos ~] |
| drwxr-xr-x 2 4096 Jul 14 11:09 /archive/hourly/ |
| [root@VM-8-11-centos ~] |
| |
| |
| [root@VM-8-11-centos ~] |
| [root@VM-8-11-centos ~] |
| |
| |
| [root@VM-8-11-centos ~] |
| drwxrwxr-x 2 4096 Jul 14 11:09 /archive/hourly/ |
| [root@VM-8-11-centos ~] |
- 新目录设置好之后,需要将按小时归档的配置文件 file_path.conf 移到该目录中:
| |
| [root@VM-8-11-centos archive] |
| /archive |
| [root@VM-8-11-centos archive] |
| |
| |
| [root@VM-8-11-centos archive] |
| [root@VM-8-11-centos archive] |
| |
| |
| [root@VM-8-11-centos archive] |
| [root@VM-8-11-centos archive] |
| [root@VM-8-11-centos hourly] |
| file_path.conf |
| [root@VM-8-11-centos hourly] |
- 只需要修改脚本的相关信息部分(处理逻辑部分与按日归档的脚本一致):
| [root@VM-8-11-centos hourly] |
| |
| |
| |
| 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
命令来获取当前的小时数和分钟数,有了这些信息才能验证最终的归档文件名的正确性。
| |
| [root@VM-8-11-centos hourly] |
| [root@VM-8-11-centos hourly] |
| |
| |
| [root@VM-8-11-centos hourly] |
| 1419 |
| [root@VM-8-11-centos hourly] |
| |
| |
| [root@VM-8-11-centos hourly] |
| |
| |
| /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] |
| |
| |
| [root@VM-8-11-centos hourly] |
| 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] |
三、结束语
“-------怕什么真理无穷,进一寸有一寸的欢喜。”
微信公众号搜索:饺子泡牛奶。