清单 4. 避免显式地指定目录
$ tar cf etc.tar /etc
;
这样可以确保将恢复的文件放到备用的目录,而不是运行中的某个位置 。
这个用于删除旧文件的脚本将使用文件名,提取其中嵌入的日期和时间,并确定该文件是否属于指定的界限,例如,在不在特定的天数之内(请参见清单 5) 。
清单 5. 用于删除旧文件的脚本
#!/usr/local/bin/perlmy $choice = shift;my @files = @ARGV;my @selection;if ($choice =~ /thismonth/){my ($day,$mon,$year) = dateaslist();my $match = sprintf("dd",$year,$mon);foreach my $file (@files){if ($file =~ m/$match/ && $choice eq "thismonth"){push @selection,$file;}elsif ($file !~ m/$match/ && $choice eq "notthismonth"){push @selection,$file;}}}elsif ($choice =~ /today/){my ($day,$mon,$year) = dateaslist();my $match = sprintf("ddd",$year,$mon,$day);foreach my $file (@files){if ($file =~ m/$match/ && $choice eq "today"){push @selection,$file;}elsif ($file !~ m/$match/ && $choice eq "nottoday"){push @selection,$file;}}}elsif ($choice =~ /last(/d )days/){my $days = $1;my ($day,$mon,$year) = dateaslist(time()-($1*24*3600));my $match = sprintf("ddd",$year,$mon,$day);my $spec = sprintf("last輆ys",$days);my $notspec = sprintf("notlast輆ys",$days);foreach my $file (@files){my ($date) = ($file =~ m/(/d{8})/);push @selection,$file if ($date >= $match && $choice eq $spec);push @selection,$file if ($date < $match && $choice eq $notspec);}}print join " ",@selection;sub dateaslist{my ($time) = @_;$time = time() unless defined($time);my ($day,$mon,$year) = (localtime($time))[3..5];$mon;$year = 1900;return($day,$mon,$year);}
使用这个脚本,您可以通过各种方法挑选出备份文件(请参见清单 6) 。
清单 6. 挑选备份文件
$ filesbydate.pl last5days # Files created in the last 5 days$ filesbydate.pl notlast14days# Files 15 days or older$ filesbydate.pl nothismonth# Files not created this month
;请记住,比较操作针对于文件名,而不是文件系统的创建或修改日期,所以该脚本可以对头天晚上创建的文件进行操作 。
使用 rsync 存储备份数据
rsync 工具可以在不同的位置或计算机之间复制整个目录结构,它通过使用一种特殊的算法,可以实现仅传输更改过的文件 。这使得它成为一种非常高效的复制文件的方法,特别是在不同的计算机之间,这也意味着,备份过程将很快完成 。
有两种使用 rsync 的方法,要么作为一种简单的同步方法,使用这种方法,您可以将所有重要的文件复制到新的驱动器或系统,要么作为一种备份方法,按日期复制整个目录树,这与在前面的示例中创建 tar 备份文件的方法相同 。
前面这种方法既快速又简单,但是您不能回到出现故障之前某个特定的日期 。而后面这种方法提供了基于日期的选择,但是它需要更多的管理(尤其是因为您需要删除不再需要的旧版本的文件)以及相当数量的存储空间,因为没有对这些文件进行压缩 。然而,您获得了更简单和更直接的访问方式 。
如何对 rsync 进行设置,这超出了本文的范围,但在完成了相应的配置工作之后,进行信息的传输和同步就非常简单了 。清单 7 显示了使用 rsync 进行文件同步的脚本 。
清单 7. 使用 rsync 对文件进行同步的脚本
#!/bin/bashDESTBASE=admin@atuin:/mnt/backupprepareHOST=`hostname`cd /export/data/svnrsync --stats --rsh=/usr/bin/ssh --delete --recursive --times -og --links . $DESTBASE/$HOST/svncd /export/home/websrsync --stats --rsh=/usr/bin/ssh --delete --recursive --times -og --links . $DESTBASE/$HOST/webscd /var/bindrsync --stats --rsh=/usr/bin/ssh --delete --recursive --times -og --links . $DESTBASE/$HOST/bindcd /etcrsync --stats --rsh=/usr/bin/ssh --delete --recursive --times -og --links . $DESTBASE/$HOST/etc
;指定的 rsync 命令选项如下所示:
推荐阅读
- 小米10的操作系统是哪个版本
- 华为Mate 10系统EMUI 8.0.0.128SP2版本怎么样?值得更新吗?
- Linux的LUPA认证考试系统beta2发布
- 开源操作系统推动智能手机发展
- 什么是虹吸同层排水系统原理
- 微信备份怎么看到内容
- Linux系统通过手机GPRS上网设置简介
- linux实用系统管理命令回顾
- Unix/Linux中的shell 机制
- linux系统性能检测