簡易バックアップスクリプト

簡単に作ってみた。

#!/usr/bin/perl

use warnings;
use strict;

my $retval = "1";
my $master_dev = "/dev/sda1";
my $backup_dev = "/dev/hdc";
my $backup_dev1 = "/dev/hdc1";
my $master_dir = "/";
my $backup_dir = "/backup";
my $log = "/var/log/backup.log";


print "backup $master_dir ($master_dev) -> $backup_dir ($backup_dev1)\n";

my $mount_chk = `mount | grep $backup_dir | grep $backup_dev1`;
chomp($mount_chk);
print "mount_check: ";

if ($mount_chk ne "") {
    print "OK\n";

    print "tune2fs: ";
    $retval = system("tune2fs -c -1 -i 0 $master_dev > $log 2>&1");

    if ($retval eq "0") {
        $retval = system("tune2fs -c -1 -i 0 $backup_dev1 >> $log 2>&1");

        if ($retval eq "0") {
            print "OK\n";
        } else {
            print "NG\n";
            exit(1);
        }
    } else {
        print "NG\n";
        exit(1);
    }

    sleep 3;

    print "rsync: ";
    $retval = system("rsync -avx --delete $master_dir $backup_dir/ >> $log 2>&1");

    if ($retval eq "0") {
        print "OK\n";
    } else {
        print "NG\n";
        exit(1);
    }

    print "grub-install: ";
    $retval = system("grub-install --root-directory=$backup_dir $backup_dev >> $log 2>&1");

    if ($retval eq "0") {
        print "OK\n";
    } else {
        print "NG\n";
        exit(1);
    }

} else {
    print "NG\n";
    exit(1);
}

そろそろバックアップをした方が良い年になってきた。
昔は、DISK壊れても、1から作り直せる程度のものが多かったが、今は無理!
これでしばらくは大丈夫かな。

実行後

# ./backup.pl
backup / (/dev/sda1) -> /backup (/dev/hdc1)
mount_check: OK
tune2fs: OK
rsync: OK
grub-install: OK