Rsync backup

Skapad 3 år sedan, skriven i Perl, med 246 visningar.
URL http://kod.perl.se/view/6ed4871d Bädda in
Ladda hem koden eller visa koden i råformatVisa bredare version
  1. #!/usr/bin/perl
  2.  
  3. ################################################################################
  4. #
  5. # Perl implementation of rsync-backup.sh
  6. #
  7. # 2014-11-17 - MSL
  8. #
  9. ################################################################################
  10.  
  11. use warnings;
  12. use strict;
  13. use v5.10;
  14.  
  15. use Log::Log4perl qw(:easy);
  16. Log::Log4perl->easy_init(       {       level => $DEBUG, file  => ">>$0.log"},
  17.                                                         {       level => $DEBUG, file  => "STDOUT"      }       );
  18.  
  19. INFO("$0 - Begin Program");
  20.  
  21. ################################################################################
  22. #
  23. # Define backup volumnes
  24. #
  25. ################################################################################
  26.  
  27. my %backup_vols;  # key = partition UUID
  28.  
  29. # entry 00
  30. $backup_vols{'cc284958-7a1a-4519-b5f6-a068ff1397dc'}->{'EXCLUDES'}   = "/root/etc/rsync-backup/excludes.conf";
  31.  
  32. # Volume: /dev/sdb1, 8:17, 750126104576, 6f1a98f5-70ea-4870-ae51-53e52f0383af, ext2, backup001
  33. $backup_vols{'6f1a98f5-70ea-4870-ae51-53e52f0383af'}->{'EXCLUDES'}   = "/root/etc/rsync-backup/excludes.conf";
  34.  
  35. # Volume: /dev/sdb1, 8:17, 1000203837440, 6720f72b-86ad-4568-b8dc-b22f69cf2923, btrfs, backup02
  36. $backup_vols{'6720f72b-86ad-4568-b8dc-b22f69cf2923'}->{'EXCLUDES'}   = "/root/etc/rsync-backup/excludes-1tb.conf";
  37.  
  38. # Volume: /dev/sdb1, 8:17, 999500546048, ed4f4207-7ee5-4263-8b2e-4267a4abf1f9, btrfs, backup03
  39. $backup_vols{'ed4f4207-7ee5-4263-8b2e-4267a4abf1f9'}->{'EXCLUDES'}   = "/root/etc/rsync-backup/excludes-1tb.conf";
  40.  
  41.  
  42. ##############################################################################################
  43. #
  44. #  Occasionally changed system configuration variables
  45. #
  46. ##############################################################################################
  47.  
  48. # bin  boot  cgroup  dev  etc  home  lib  lost+found  media  mnt  opt  proc  root  run  sbin  selinux  srv  storage  sys  tmp  usr  var
  49. my $SOURCES="/bin /boot /etc /home /lib /opt /root /sbin /storage /usr /var";
  50.  
  51. my $FS_MOUNTPOINT = q{/mnt};
  52.  
  53. my $VERBOSE = q{-v};
  54.  
  55. ################################################################################
  56. #
  57. # Utility Paths and options.
  58. #
  59. ################################################################################
  60.  
  61. my $BIN_LSBLK = qx{ which lsblk };
  62. chomp $BIN_LSBLK;
  63. INFO ("lsblk=$BIN_LSBLK");
  64.  
  65. my $LSBLKOPTS = q{ -bnp -o KNAME,MAJ:MIN,SIZE,UUID,FSTYPE,LABEL };
  66.  
  67. my $LSBLK_CMD = qq{ $BIN_LSBLK $LSBLKOPTS };
  68.  
  69.  
  70. ################################################################################
  71. #
  72. # Step 1:  Let's find out what backup volume ( if any ) is mounted.
  73. #
  74. ################################################################################
  75.  
  76.  
  77. #  Initialize Target variables
  78. my ( $T_KNAME, $T_MAJ_MIN, $T_SIZE, $T_UUID, $T_FSTYPE, $T_LABEL ) = ();
  79.  
  80.  
  81. #  Loop through lsblk
  82. foreach my $line ( qx{ $LSBLK_CMD } ) {
  83.  
  84.         #  while we don't have a T_UUID
  85.         if ( not $T_UUID ) {
  86.  
  87.                 #  Extract data from lsblk row
  88.                 my      ($KNAME, $MAJ_MIN, $SIZE, $UUID, $FSTYPE, $LABEL) = split(/\s+/,$line);
  89.  
  90.                 # if we match one of the UUIDs on our list
  91.                 if ( grep (/$UUID/, sort keys %backup_vols) ) {  
  92.  
  93.                         #  save off the device information
  94.                         ($T_KNAME, $T_MAJ_MIN, $T_SIZE, $T_UUID, $T_FSTYPE, $T_LABEL ) = split(/\s+/,$line);
  95.  
  96.                 };  # if
  97.  
  98.         }; # if not T_UUID
  99.  
  100. } # foreach $line
  101.  
  102.  
  103. #  Check to make sure we have a device attached
  104.  
  105. if ( not $T_UUID ) {
  106.         FATAL("Fatal Error:  Could not find a drive to use.");
  107.         FATAL qx{ echo "Output of: $LSBLK_CMD"; $LSBLK_CMD };
  108.         FATAL "$0 - End Program";
  109.         exit 1;
  110. }
  111.  
  112.  
  113. INFO "Volume: $T_KNAME, $T_MAJ_MIN, $T_SIZE, $T_UUID, $T_FSTYPE, $T_LABEL";
  114.  
  115. ################################################################################
  116. #
  117. # Utility Paths and options (continued)
  118. #
  119. ################################################################################
  120.  
  121. my $BIN_MOUNT = qx{ which mount };
  122. chomp $BIN_MOUNT;
  123. INFO ("mount=$BIN_MOUNT");
  124.  
  125. my $MOUNTOPTS = qq{-o context="system_u:object_r:removable_t:s0" };
  126.  
  127. my $CMD_MOUNT = qq{ $BIN_MOUNT $VERBOSE -t $T_FSTYPE -U $T_UUID $FS_MOUNTPOINT $MOUNTOPTS };
  128.  
  129. ################################################################################
  130.  
  131. my $BIN_RSYNC = qx{ which rsync };
  132. chomp $BIN_RSYNC;
  133. INFO ("rsync=$BIN_RSYNC");
  134.  
  135. my $RSYNCOPTS = qq{-a $VERBOSE --delete --delete-excluded --exclude-from=$backup_vols{$T_UUID}->{'EXCLUDES'} };
  136.  
  137. my $CMD_RSYNC = qq{ $BIN_RSYNC $RSYNCOPTS $SOURCES $FS_MOUNTPOINT };
  138.  
  139. ################################################################################
  140.  
  141. my $BIN_UMOUNT = qx{ which umount };
  142. chomp $BIN_UMOUNT;
  143. INFO ("umount=$BIN_UMOUNT");
  144.  
  145. my $UMOUNTOPTS = qq{};
  146.  
  147. my $CMD_UMOUNT = qq{ $BIN_UMOUNT $VERBOSE $FS_MOUNTPOINT };
  148.  
  149.  
  150. ##############################################################################################
  151. #
  152. # Subroutine: execute_cmd( qq{ some command text $variable } );
  153. #
  154. ##############################################################################################
  155. sub execute_cmd {
  156.  
  157.         my $cmd = shift;
  158.         INFO "    Command='$cmd'";
  159.  
  160.         my $retval = qx{$cmd 2>&1}; # || sad_trombone
  161.         DEBUG "CHILD_ERROR_NATIVE=${^CHILD_ERROR_NATIVE}";
  162.  
  163.         DEBUG "----  Command Output  ----";
  164.         DEBUG "$retval";
  165.         DEBUG "--------------------------";
  166.  
  167.         return ${^CHILD_ERROR_NATIVE};
  168.  
  169. }; # sub execute_cmd
  170.  
  171.  
  172. ##############################################################################################
  173. #
  174. #  Begin
  175. #
  176. ##############################################################################################
  177.  
  178. INFO "Beginning rsync backup of data to portable drive.";
  179. INFO "--------------------------------------------------------------------------------";
  180. INFO "    Backup up directories: $SOURCES";
  181. INFO "    to: $FS_MOUNTPOINT";
  182. INFO "    with rsync options: $RSYNCOPTS";
  183.  
  184. INFO "----  Mounting Portable Drive  ---";
  185. my $rtn_code = execute_cmd ( $CMD_MOUNT );
  186.  
  187. if ( $rtn_code == 0 ) {
  188.         INFO "----  Beginning Sync Operation  ---";
  189.         execute_cmd ( $CMD_RSYNC );
  190. };
  191.  
  192. INFO "----  Unmounting Portable Drive  ---";
  193. execute_cmd ( $CMD_UMOUNT );
  194.  
  195. INFO("$0 - End Program");
  196.  
  197.  
  198. ##############################################################################################
  199. __END__

Svara på "Rsync backup"

Här kan du skriva ett svar till kodsnutten ovan