Prisijungti

Simple Linux backup file script

TechtronicMindaugas N.
  • 25 Spa '15

Sveiki, esu parases perl skripta kuris yra tikrai paprastas, bet atlieka labai naudinga darba. Jo darbas sukurti failo backup'a.

Tiems kam daznai tenka keisti konfiguracinius failus supranta, kad pries pradedant reikia pirma sukurti failo backup'a nenumatytiems atvejams. Dazniausiai tam naudojamas GNU cp, bet tada atsiranda problemu - pamirstami tie "backupai", o ir jeigu ju atsiranda ne vienas reikia salinti senus arba prireikus grazinti backup sunku atrasti kuris reikalingas... Tai gali buti net ir kritiska saugumo problema.

Tema jau greiciausiai kartojasi, bet pagaliau ji pateko ten kur jos vieta

Veikimas:

minde@pts/0:/home/minde:b
minde@pts/0:/home/minde:b .zshrc
/home/minde/.zshrc.93
minde@pts/0:/home/minde:b .zshrc
/home/minde/.zshrc.51
minde@pts/0:/home/minde:b
[1 ] [39K  ] 2014-01-15 19:53:29 - /home/minde/.zshrc.93
[2 ] [39K  ] 2014-01-15 19:53:30 - /home/minde/.zshrc.51
minde@pts/0:/home/minde:rm /home/minde/.zshrc.93 /home/minde/.zshrc.51
removed ‘/home/minde/.zshrc.93’
removed ‘/home/minde/.zshrc.51’
minde@pts/0:/home/minde:b
minde@pts/0:/home/minde:

Kodas:

<span class="syntaxdefault"></span><span class="syntaxcomment">#!/usr/bin/perl<br /># b --- Simple backup script<br /># Author:  <minde@posix.lt><br /># Created: 15 Jan 2014<br /># Version: 0.01<br /><br /></span><span class="syntaxdefault">use Number</span><span class="syntaxkeyword">::</span><span class="syntaxdefault">Bytes</span><span class="syntaxkeyword">::</span><span class="syntaxdefault">Human qw</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">format_bytes</span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">use Cwd qw</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">abs_path</span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">use POSIX qw</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">strftime</span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">use File</span><span class="syntaxkeyword">::</span><span class="syntaxdefault">Copy qw</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">copy</span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">use File</span><span class="syntaxkeyword">::</span><span class="syntaxdefault">Slurp qw</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">read_file write_file edit_file_lines</span><span class="syntaxkeyword">);<br /><br /></span><span class="syntaxcomment"># Where save backup information<br /></span><span class="syntaxdefault">my $backups </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxstring">"</span><span class="syntaxdefault">$ENV</span><span class="syntaxstring">{HOME}/.brc"</span><span class="syntaxkeyword">;<br /><br /></span><span class="syntaxcomment"># Create if necessary<br /></span><span class="syntaxdefault">open</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">my $fh</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> </span><span class="syntaxstring">'>>'</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> $backups</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> and close</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$fh</span><span class="syntaxkeyword">);<br /><br /></span><span class="syntaxdefault">if </span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$ARGV</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">])</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">{<br /></span><span class="syntaxdefault">  my </span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$file</span><span class="syntaxkeyword">,@</span><span class="syntaxdefault">data</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> abs_path</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$ARGV</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">]);<br /></span><span class="syntaxdefault">  my $time </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> strftime </span><span class="syntaxstring">"%Y-%m-%d %H:%M:%S"</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> localtime</span><span class="syntaxkeyword">;<br /></span><span class="syntaxdefault">  if </span><span class="syntaxkeyword">(-</span><span class="syntaxdefault">f $file</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">{<br /></span><span class="syntaxdefault">    my $back_file </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> $file </span><span class="syntaxkeyword">.</span><span class="syntaxdefault"> </span><span class="syntaxstring">"."</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">.</span><span class="syntaxdefault"> int</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">rand</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">1000</span><span class="syntaxkeyword">));<br /></span><span class="syntaxdefault">    copy</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$file</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> $back_file</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> or die </span><span class="syntaxstring">"Failed to copy: $!\n"</span><span class="syntaxkeyword">;<br /></span><span class="syntaxdefault">    my </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">data </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$time</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> </span><span class="syntaxstring">" "</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> $back_file</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> </span><span class="syntaxstring">"\n"</span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">    write_file</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$backups</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">{</span><span class="syntaxdefault">append </span><span class="syntaxkeyword">=></span><span class="syntaxdefault"> 1 </span><span class="syntaxkeyword">},</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">data</span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">    printf</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"%s\n"</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> $back_file</span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">  </span><span class="syntaxkeyword">}</span><span class="syntaxdefault"> else </span><span class="syntaxkeyword">{<br /></span><span class="syntaxdefault">    printf</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"I can't read that fucking file %s\n"</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> $file</span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">    exit</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">  </span><span class="syntaxkeyword">}<br />}</span><span class="syntaxdefault"> else </span><span class="syntaxkeyword">{<br /></span><span class="syntaxdefault">  my </span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$items</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">$count</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> read_file</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$backups</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> array_ref </span><span class="syntaxkeyword">=></span><span class="syntaxdefault"> 1</span><span class="syntaxkeyword">),</span><span class="syntaxdefault"> int</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">  foreach </span><span class="syntaxkeyword">(@</span><span class="syntaxdefault">$items</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">{<br /></span><span class="syntaxdefault">    $_ </span><span class="syntaxkeyword">=~</span><span class="syntaxdefault"> m</span><span class="syntaxkeyword">/(\</span><span class="syntaxdefault">d</span><span class="syntaxkeyword">+-\</span><span class="syntaxdefault">d</span><span class="syntaxkeyword">+-\</span><span class="syntaxdefault">d</span><span class="syntaxkeyword">+)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">(\</span><span class="syntaxdefault">d</span><span class="syntaxkeyword">+:\</span><span class="syntaxdefault">d</span><span class="syntaxkeyword">+:\</span><span class="syntaxdefault">d</span><span class="syntaxkeyword">+)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">(.+)/</span><span class="syntaxdefault">s</span><span class="syntaxkeyword">;<br /></span><span class="syntaxdefault">    my </span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$date</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">$time</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">$file</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">$size</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">($</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">,$</span><span class="syntaxdefault">2</span><span class="syntaxkeyword">,$</span><span class="syntaxdefault">3</span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">    chomp</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$file</span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">    if </span><span class="syntaxkeyword">(-</span><span class="syntaxdefault">s $file</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">{<br /></span><span class="syntaxdefault">      $size </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> format_bytes</span><span class="syntaxkeyword">(-</span><span class="syntaxdefault">s $file</span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">      $count</span><span class="syntaxkeyword">++;<br /></span><span class="syntaxdefault">    </span><span class="syntaxkeyword">}</span><span class="syntaxdefault"> else </span><span class="syntaxkeyword">{<br /></span><span class="syntaxdefault">      edit_file_lines </span><span class="syntaxkeyword">{</span><span class="syntaxdefault"> $_ </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxstring">''</span><span class="syntaxdefault"> if </span><span class="syntaxkeyword">/</span><span class="syntaxdefault">$date $time $file</span><span class="syntaxkeyword">/</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">}</span><span class="syntaxdefault"> $backups</span><span class="syntaxkeyword">;<br /></span><span class="syntaxdefault">      next</span><span class="syntaxkeyword">;<br /></span><span class="syntaxdefault">    </span><span class="syntaxkeyword">}<br /></span><span class="syntaxdefault">    printf</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"[%-2s] [%-5s] %s %s - %s \n"</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$count</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> $size</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> $date</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> $time</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> $file</span><span class="syntaxkeyword">));<br /></span><span class="syntaxdefault">  </span><span class="syntaxkeyword">}<br /></span><span class="syntaxdefault">  print</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"The system is clean.\n"</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> if not $count</span><span class="syntaxkeyword">;<br />}<br /><br /></span><span class="syntaxdefault">__END__<br /><br /></span><span class="syntaxkeyword">=</span><span class="syntaxdefault">head1 NAME<br /><br />b </span><span class="syntaxkeyword">-</span><span class="syntaxdefault"> Just fucking simple backup files perl script</span><span class="syntaxkeyword">.<br /><br />=</span><span class="syntaxdefault">head1 DESCRIPTION<br /><br />seriously</span><span class="syntaxkeyword">?<br /><br /></span><span class="syntaxdefault">minde</span><span class="syntaxkeyword">@</span><span class="syntaxdefault">pts</span><span class="syntaxkeyword">/</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">:/</span><span class="syntaxdefault">home</span><span class="syntaxkeyword">/</span><span class="syntaxdefault">minde</span><span class="syntaxkeyword">:</span><span class="syntaxdefault">b<br />minde</span><span class="syntaxkeyword">@</span><span class="syntaxdefault">pts</span><span class="syntaxkeyword">/</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">:/</span><span class="syntaxdefault">home</span><span class="syntaxkeyword">/</span><span class="syntaxdefault">minde</span><span class="syntaxkeyword">:</span><span class="syntaxdefault">b </span><span class="syntaxkeyword">.</span><span class="syntaxdefault">zshrc<br /></span><span class="syntaxkeyword">/</span><span class="syntaxdefault">home</span><span class="syntaxkeyword">/</span><span class="syntaxdefault">minde</span><span class="syntaxkeyword">/.</span><span class="syntaxdefault">zshrc.93<br />minde</span><span class="syntaxkeyword">@</span><span class="syntaxdefault">pts</span><span class="syntaxkeyword">/</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">:/</span><span class="syntaxdefault">home</span><span class="syntaxkeyword">/</span><span class="syntaxdefault">minde</span><span class="syntaxkeyword">:</span><span class="syntaxdefault">b </span><span class="syntaxkeyword">.</span><span class="syntaxdefault">zshrc<br /></span><span class="syntaxkeyword">/</span><span class="syntaxdefault">home</span><span class="syntaxkeyword">/</span><span class="syntaxdefault">minde</span><span class="syntaxkeyword">/.</span><span class="syntaxdefault">zshrc.51<br />minde</span><span class="syntaxkeyword">@</span><span class="syntaxdefault">pts</span><span class="syntaxkeyword">/</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">:/</span><span class="syntaxdefault">home</span><span class="syntaxkeyword">/</span><span class="syntaxdefault">minde</span><span class="syntaxkeyword">:</span><span class="syntaxdefault">b<br /></span><span class="syntaxkeyword">[</span><span class="syntaxdefault">1 </span><span class="syntaxkeyword">]</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">[</span><span class="syntaxdefault">39K  </span><span class="syntaxkeyword">]</span><span class="syntaxdefault"> 2014</span><span class="syntaxkeyword">-</span><span class="syntaxdefault">01</span><span class="syntaxkeyword">-</span><span class="syntaxdefault">15 19</span><span class="syntaxkeyword">:</span><span class="syntaxdefault">53</span><span class="syntaxkeyword">:</span><span class="syntaxdefault">29 </span><span class="syntaxkeyword">-</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">/</span><span class="syntaxdefault">home</span><span class="syntaxkeyword">/</span><span class="syntaxdefault">minde</span><span class="syntaxkeyword">/.</span><span class="syntaxdefault">zshrc.93<br /></span><span class="syntaxkeyword">[</span><span class="syntaxdefault">2 </span><span class="syntaxkeyword">]</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">[</span><span class="syntaxdefault">39K  </span><span class="syntaxkeyword">]</span><span class="syntaxdefault"> 2014</span><span class="syntaxkeyword">-</span><span class="syntaxdefault">01</span><span class="syntaxkeyword">-</span><span class="syntaxdefault">15 19</span><span class="syntaxkeyword">:</span><span class="syntaxdefault">53</span><span class="syntaxkeyword">:</span><span class="syntaxdefault">30 </span><span class="syntaxkeyword">-</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">/</span><span class="syntaxdefault">home</span><span class="syntaxkeyword">/</span><span class="syntaxdefault">minde</span><span class="syntaxkeyword">/.</span><span class="syntaxdefault">zshrc.51<br />minde</span><span class="syntaxkeyword">@</span><span class="syntaxdefault">pts</span><span class="syntaxkeyword">/</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">:/</span><span class="syntaxdefault">home</span><span class="syntaxkeyword">/</span><span class="syntaxdefault">minde</span><span class="syntaxkeyword">:</span><span class="syntaxdefault">rm </span><span class="syntaxkeyword">/</span><span class="syntaxdefault">home</span><span class="syntaxkeyword">/</span><span class="syntaxdefault">minde</span><span class="syntaxkeyword">/.</span><span class="syntaxdefault">zshrc.93 </span><span class="syntaxkeyword">/</span><span class="syntaxdefault">home</span><span class="syntaxkeyword">/</span><span class="syntaxdefault">minde</span><span class="syntaxkeyword">/.</span><span class="syntaxdefault">zshrc.51<br />removed ‘</span><span class="syntaxkeyword">/</span><span class="syntaxdefault">home</span><span class="syntaxkeyword">/</span><span class="syntaxdefault">minde</span><span class="syntaxkeyword">/.</span><span class="syntaxdefault">zshrc.93’<br />removed ‘</span><span class="syntaxkeyword">/</span><span class="syntaxdefault">home</span><span class="syntaxkeyword">/</span><span class="syntaxdefault">minde</span><span class="syntaxkeyword">/.</span><span class="syntaxdefault">zshrc.51’<br />minde</span><span class="syntaxkeyword">@</span><span class="syntaxdefault">pts</span><span class="syntaxkeyword">/</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">:/</span><span class="syntaxdefault">home</span><span class="syntaxkeyword">/</span><span class="syntaxdefault">minde</span><span class="syntaxkeyword">:</span><span class="syntaxdefault">b<br />minde</span><span class="syntaxkeyword">@</span><span class="syntaxdefault">pts</span><span class="syntaxkeyword">/</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">:/</span><span class="syntaxdefault">home</span><span class="syntaxkeyword">/</span><span class="syntaxdefault">minde</span><span class="syntaxkeyword">:<br /><br />=</span><span class="syntaxdefault">head1 COPYRIGHT AND LICENSE<br /><br />Copyright </span><span class="syntaxkeyword">(</span><span class="syntaxdefault">C</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> 2014 by Mindaugas N</span><span class="syntaxkeyword">.<br /><br /></span><span class="syntaxdefault">This program is free software</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> you can redistribute it and</span><span class="syntaxkeyword">/</span><span class="syntaxdefault">or modify<br />it under the same terms as Perl itself</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> either Perl version 5.8.2 or</span><span class="syntaxkeyword">,<br /></span><span class="syntaxdefault">at your option</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> any later version of Perl 5 you may have available</span><span class="syntaxkeyword">.<br /><br />=</span><span class="syntaxdefault">head1 BUGS<br /><br />None reported</span><span class="syntaxkeyword">...</span><span class="syntaxdefault"> yet</span><span class="syntaxkeyword">.<br /><br />=</span><span class="syntaxdefault">cut</span>
Atsakyti