Advertisement
linux_os2

restore_partition

Aug 5th, 2023
1,902
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rexx 8.39 KB | Software | 0 0
  1. #!/usr/bin/rexx
  2. /*trace ?i ; nop*/
  3.  
  4. mntbackup      = "/mntbackup"
  5. backupdir = mntbackup"/backup_partition"
  6.  
  7. if userid() \= 'root' then do
  8.   RC = dorestore()
  9.   return
  10. end
  11.  
  12. backup_raid_member_UUID = ""
  13. wrong_directory_mounted  = '"Verkeerde_Directory"'
  14. wrong_directory_text     = '"Een verkeerde directory is gemount op de backup-drive"'
  15. error_during_mkdir       = '"Fout_tijdens_mkdir"'
  16. error_during_mkdir_text  = '"Er is een fout opgetreden bij het aanmaken van de backup directory /mntbackup"'
  17. error_during_mount       = '"Fout_tijdens_mount"'
  18. error_during_mount_text  = '"Er is een fout opgetreden bij de mount van de backup directory /mntbackup"'
  19. error_during_umount      = '"Fout_tijdens_umount"'
  20. error_during_umount_text = '"Er is een fout opgetreden bij de unmount van de verkeerde backup directory"'
  21. backupdev_missing        = '"Backup Device not Found"'
  22. backupdev_missing_text   = '"Is de Backupdisk verbonden?"'
  23. partition_missing_text   = '"verkeerde partitie"'
  24. wrong_hardware_model_text = '"Verkeerde Machine"'
  25. 'rm /tmp/backup* > /dev/null 2>&1'
  26.  
  27. /*'hostnamectl | grep "Hardware Model:" > /tmp/backupHardware_Model'*/
  28.  
  29. Hardware_Model = linein('/etc/hostname')
  30. /*Hardware_Model = substr(Hardware_Model,pos(':',Hardware_Model) + 2)*/
  31. select
  32.   when Hardware_Model = "Alienware Area-51 R2" then backupdev_UUID = "50ea40b9-dd6b-4a87-836b-f6965841f279"
  33.   when Hardware_Model = "Marilou Dell"         then backupdev_UUID = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
  34.   when Hardware_Model = "HP ProBook 4510s"     then backupdev_UUID = "e092c18f-0979-46ae-979f-409db1d08e2a"
  35.   when Hardware_Model = "Z10PE-D16 WS" then do
  36.     backupdev_UUID = "54961d29-4f9e-4fe5-8e17-b6ea8864c3f3"
  37.     backupdev_UUID = "05eeba2c-9ab4-4bb2-93ca-85c29bc9d852" /* test ubuntu */
  38.     backup_raid_member_UUID = "a6f4693e-1554-697b-5c93-edf466ac4f07"
  39.     backup_raid_member_UUID = "665499be-765f-c830-cdea-afe5343febbb" /* test ubuntu */
  40.   end
  41.   otherwise
  42.     'echo 'date("I") time("N") wrong_hardware_model_text' >> /tmp/backup.log'
  43.     exit 15
  44. end
  45. 'blkid | grep 'backupdev_UUID' > /tmp/backupuuid'
  46. backupdev = linein('/tmp/backupuuid')
  47. if backupdev = '' then do
  48.   if backup_raid_member_UUID \= '' then do
  49.     'blkid | grep 'backup_raid_member_UUID' > /tmp/backupraidmemberuuid'
  50.     if lines('/tmp/backupraidmemberuuid') = 0 then do
  51.       'echo 'date("I") time("N") backupdev_missing_text' >> /tmp/backup.log'
  52.       exit 15
  53.     end
  54.     backupraidmembers = ''
  55.     do while lines('/tmp/backupraidmemberuuid') \= 0
  56.       line = linein('/tmp/backupraidmemberuuid')
  57.       backupraidmembers = backupraidmembers substr(line,1,length(word(line,1)) -1)
  58.     end
  59.     'cat /proc/mdstat > /tmp/backupmdstat'
  60.     mdarrays = ''
  61.     do while lines('/tmp/backupmdstat') \= 0
  62.       line = linein('/tmp/backupmdstat')
  63.       if word(line,1) = "Personalities" then iterate
  64.       if word(line,2) = ":" then mdarrays = mdarrays","substr(word(line,1),3)
  65.       else iterate
  66.     end
  67.     mdarrays = substr(mdarrays,2)
  68.     interpret 'mdarray = min('mdarrays')'
  69.     if mdarray = 1 then interpret 'backupdev = max('mdarrays') + 1'
  70.                    else backupdev = mdarray -1
  71.       'mdadm --assemble /dev/md'backupdev backupraidmembers
  72.       backupdev = '/dev/md'backupdev':'
  73.     end
  74.   else do
  75.     if userid \= "cron"
  76.       then 'DISPLAY=:1 zenity --info --title='backupdev_missing' --text='backupdev_missing_text
  77.       else 'echo 'date("I") time("N") backupdev_missing_text' >> /tmp/backup.log'
  78.     exit 15
  79.   end
  80. end
  81. backupdev = substr(backupdev,6,length(word(backupdev,1)) -6)
  82. 'mount | grep /dev/'backupdev ' > /tmp/backupmounted'
  83. do x = 0 while lines('/tmp/backupmounted') = 1
  84.   backupmounted = linein('/tmp/backupmounted')
  85. end
  86. select
  87.   when x = 0 then do
  88.     'mount /dev/'backupdev' 'mntbackup' >> /tmp/backupmount.txt 2>&1'
  89.     returncode = RC    
  90.     select
  91.       when returncode = 0 then nop
  92.       when returncode = 1
  93.         then do
  94.           'echo 'date("I") time("N") error_during_mount_text' >> /tmp/backup.log'
  95.           exit 15
  96.         end  
  97.       when returncode = 32 then nop
  98.       otherwise nop
  99.     end
  100.   end
  101.   when x = 1 then do
  102.     if mntbackup \= word(backupmounted,3)
  103.       then
  104.         do
  105.           'umount /dev/'backupdev' > /dev/null 2>&1'
  106.           if RC = \0 then do
  107.             'echo 'date("I") time("N") error_during_umount_text' >> /tmp/backup.log'
  108.             exit 15
  109.           end
  110.           'mount /dev/'backupdev' 'mntbackup' >> /tmp/backupmount.txt 2>&1'
  111.       select
  112.         when RC = 0 then nop
  113.         when RC = 1
  114.           then do
  115.             'echo 'date("I") time("N") error_during_mount_text' >> /tmp/backup.log'
  116.             exit 15
  117.           end  
  118.         when RC = 32 then nop
  119.         otherwise
  120.           'echo 'date("I") time("N") error_during_mount_text' >> /tmp/backup.log'
  121.           exit 15
  122.       end
  123.     end
  124.     mountOK = 1
  125.   end  
  126.   when x > 1 then do
  127.     'umount /dev/'backupdev' > /dev/null 2>&1'
  128.     if RC = \0 then do
  129.       'echo 'date("I") time("N") error_during_umount_text' >> /tmp/backup.log'
  130.       exit 15
  131.     end
  132.     'mount /dev/'backupdev' 'mntbackup' >> /tmp/backupmount.txt 2>&1'
  133.     select
  134.       when RC = 0 then nop
  135.       when RC = 1
  136.         then do
  137.           'echo 'date("I") time("N") error_during_mount_text' >> /tmp/backup.log'
  138.           exit 15
  139.         end  
  140.       when RC = 32 then nop
  141.       otherwise nop
  142.     end
  143.     if RC \= 0 then do
  144.         'echo 'date("I") time("N") wrong_directory_text' >> /tmp/backup.log'
  145.         'rm /tmp/backup*'
  146.         exit 15
  147.       end
  148.       else mountOK = 1
  149.   end  
  150.   otherwise nop
  151. end
  152. 'stat 'backupdir' > /dev/null 2>&1'
  153. returncode = RC
  154. msgmnt = linein('/tmp/backupmount.txt')
  155. select
  156.   when returncode = 0 then nop
  157.   when returncode = 1
  158.     then do
  159.       'mkdir -p 'backupdir' > /dev/null 2>&1'
  160.       'chmod 777 'backupdir' > /dev/null 2>&1'
  161.       if RC \= 0 then do
  162.         'DISPLAY=:1 zenity --info --title='error_during_mkdir' --text='error_during_mkdir_text
  163.         exit 15
  164.       end
  165.     end
  166.   when returncode = 32 then
  167.     if word(msgmnt,7) \= mntbackup"."
  168.       then do
  169.         'echo 'date("I") time("N") wrong_directory_text' >> 'backupdir'/backup.log'
  170.         exit 15
  171.       end
  172.   otherwise
  173.     'echo 'date("I") time("N") wrong_password_text' >> 'backupdir'/backup.log'
  174.     exit 15
  175. end
  176. /***************************************************************************************************************************************************************************************************/
  177. dorestore:
  178. 'rm 'backupdir'/uuid* 'backupdir'/cat* 'backupdir'/DEV* 'backupdir'/restorelist.txt 'backupdir'/infolist 'backupdir'/backup_info_list'
  179. if lines(backupdir'/torestorelist') = 0 then RC = lineout(backupdir'/'date("I")'_'time("N")'restore.log','no tobackuplist found')
  180. do r = 1 while lines(backupdir'/torestorelist')
  181.     restoreline   = linein(backupdir'/torestorelist')
  182.     partuuid     = word(restoreline,1)
  183.     if userid() = 'root'
  184.       then 'blkid | grep 'partuuid' > 'backupdir'/uuid'r
  185.       else 'SUDO_ASKPASS=/usr/local/bin/backup_ask_password sudo -A blkid | grep 'partuuid' > 'backupdir'/uuid'r
  186.     partition = linein(backupdir'/uuid'r)
  187.     partition = substr(word(partition,1),1,length(word(partition,1)) - 1)
  188.     restorefile   = word(restoreline,2)'.zst'
  189.     restorelog   = word(restoreline,2)'.restore.log'
  190.     if userid() = 'root'
  191.       then do
  192.         'echo 'date("I") time("N") ' restore of partition starts >> 'restorelog
  193.         'zstdcat 'restorefile' | partclone.restore -O 'partition' -L 'restorelog
  194.         if RC \= 0
  195.           then RC = lineout(restorelog,date("I") time("N")' restore of partition was unsuccesfull')
  196.           else RC = lineout(restorelog,date("I") time("N")' restore of partition was succesfull')
  197.       end
  198.       else do
  199.         'echo 'date("I") time("N") ' restore of partition starts >> 'restorelog
  200.         'SUDO_ASKPASS=/usr/local/bin/backup_ask_password sudo -A zstdcat 'restorefile' | sudo partclone.restore -O 'partition' -L 'restorelog
  201.         if RC \= 0
  202.           then 'echo 'date("I") time("N") ' restore of partition was unsuccesfull >> 'restorelog
  203.           else 'echo 'date("I") time("N") ' restore of partition was succesfull >> 'restorelog
  204.       end
  205. end
  206. if userid() = 'root'
  207.   then do
  208.     'rm 'backupdir'/torestorelist > /dev/null &2>1'
  209.     'reboot'
  210.   end
  211.   else 'SUDO_ASKPASS=/usr/local/bin/backup_ask_password sudo -A rm 'backupdir'/torestorelist > /dev/null &2>1'
  212. return 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement