001 #!/usr/local/bin/perl -w 002 use strict; 003 use File::Temp qw( tempfile ); 004 use File::Basename; 005 use Log::Log4perl qw(:easy); 006 use Sysadm::Install qw( :all ); 007 008 Log::Log4perl->easy_init($DEBUG); 009 010 my $ip = "192.168.0.111"; 011 my $user = "mschilli"; 012 my $mnt_dir = "/home/partimag"; 013 014 if( ! -d $mnt_dir ) { 015 mkdir $mnt_dir; 016 } 017 018 network(); 019 mount(); 020 backup_all(); 021 unmount(); 022 023 1; 024 025 ########################################### 026 sub network { 027 ########################################### 028 sysrun "sudo", "dhclient", "eth0"; 029 } 030 031 ########################################### 032 sub unmount { 033 ########################################### 034 sysrun "fusermount", "-u", $mnt_dir; 035 } 036 037 ########################################### 038 sub mount { 039 ########################################### 040 DEBUG "Writing private key"; 041 my $privkey_file = (tempfile())[1]; 042 blurt privkey(), $privkey_file; 043 044 DEBUG "Writing known hosts"; 045 my $kh_file = (tempfile())[1]; 046 blurt known_hosts(), $kh_file; 047 048 my $rc = sysrun( "sudo", "sshfs", 049 "$user\@$ip:/backup/clonezilla", 050 $mnt_dir, "-o", 051 "ssh_command=ssh -i $privkey_file " . 052 "-o BatchMode=yes " . 053 "-o GlobalKnownHostsFile=$kh_file" ); 054 055 if( $rc ) { 056 die "sshfs failed (backup host down?)"; 057 } 058 } 059 060 ########################################### 061 sub backup_all { 062 ########################################### 063 my $last_backup_path = 064 ( reverse sort <$mnt_dir/[0-9]*> )[0]; 065 066 my $continue_with; 067 068 my @drives = < /dev/sda[0-9]* >; 069 070 if( defined $last_backup_path and 071 ! -f "$last_backup_path/DONE" ) { 072 $continue_with = ( 073 sort { -M $a <=> -M $b } 074 < $last_backup_path/*gz* > )[0]; 075 $continue_with = 076 basename $continue_with; 077 $continue_with =~ s/\..*//; 078 DEBUG "Continue: $continue_with"; 079 } 080 081 my $not_yet; 082 083 if( defined $continue_with ) { 084 $not_yet = 1; 085 } 086 087 my ( $sec, $min, $hour, $mday, $mon, 088 $year ) = localtime( time ); 089 090 my $date = 091 sprintf "%04d-%02d-%02d-%02d-%02d-%02d", 092 $year + 1900, $mon+1, $mday, $hour, 093 $min, $sec; 094 095 my @parts = (); 096 097 for my $part ( sort @drives ) { 098 099 my $base = basename $part; 100 101 if( $base eq $continue_with ) { 102 $not_yet = 0; 103 } 104 105 next if $not_yet; 106 107 push @parts, basename $part; 108 } 109 110 backup( $date, @parts ); 111 } 112 113 ########################################### 114 sub backup { 115 ########################################### 116 my( $date, @parts ) = @_; 117 118 DEBUG "Backing up @parts"; 119 sysrun qw( sudo /opt/drbl/sbin/ocs-sr 120 -b -q2 -c -j2 -z1 -i 2000 -sc -p true 121 savedisk ), "$date-img", @parts; 122 123 blurt "", "$mnt_dir/$date-img/DONE"; 124 } 125 126 ########################################### 127 sub known_hosts { 128 ########################################### 129 return <<'EOT' 130 XXX 131 EOT 132 } 133 134 ########################################### 135 sub privkey { 136 ########################################### 137 return <<'EOT' 138 -----BEGIN RSA PRIVATE KEY----- 139 YYY 140 -----END RSA PRIVATE KEY----- 141 EOT 142 }