Advertisement
linux_os2

backup_partition

Aug 5th, 2023
2,073
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rexx 10.21 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 = dobackup()
  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. Hardware_Model = linein('/etc/hostname')
  27. if Hardware_Model = ''
  28.   then do
  29.     'hostnamectl | grep "Hardware Model:" > /tmp/backupHardware_Model'
  30.     Hardware_Model = substr(Hardware_Model,pos(':',Hardware_Model) + 2)
  31.   end
  32. select
  33.   when Hardware_Model = "Alienware Area-51 R2" then backupdev_UUID = "50ea40b9-dd6b-4a87-836b-f6965841f279"
  34.   when Hardware_Model = "Marilou Dell"         then backupdev_UUID = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
  35.   when Hardware_Model = "HP ProBook 4510s"     then backupdev_UUID = "e092c18f-0979-46ae-979f-409db1d08e2a"
  36.   when Hardware_Model = "Z10PE-D16 WS" then do
  37.     backupdev_UUID = "54961d29-4f9e-4fe5-8e17-b6ea8864c3f3"
  38.     backupdev_UUID = "05eeba2c-9ab4-4bb2-93ca-85c29bc9d852" /* test ubuntu */
  39.     backup_raid_member_UUID = "a6f4693e-1554-697b-5c93-edf466ac4f07"
  40.     backup_raid_member_UUID = "665499be-765f-c830-cdea-afe5343febbb" /* test ubuntu */
  41.   end
  42.   otherwise
  43.     'echo 'date("I") time("N") wrong_hardware_model_text' >> /tmp/backup.log'
  44.     exit 15
  45. end
  46. 'blkid | grep 'backupdev_UUID' > /tmp/backupuuid'
  47. backupdev = linein('/tmp/backupuuid')
  48. if backupdev = '' then do
  49.   if backup_raid_member_UUID \= '' then do
  50.     'blkid | grep 'backup_raid_member_UUID' > /tmp/backupraidmemberuuid'
  51.     if lines('/tmp/backupraidmemberuuid') = 0 then do
  52.       'echo 'date("I") time("N") backupdev_missing_text' >> /tmp/backup.log'
  53.       exit 15
  54.     end
  55.     backupraidmembers = ''
  56.     do while lines('/tmp/backupraidmemberuuid') \= 0
  57.       line = linein('/tmp/backupraidmemberuuid')
  58.       backupraidmembers = backupraidmembers substr(line,1,length(word(line,1)) -1)
  59.     end
  60.     'cat /proc/mdstat > /tmp/backupmdstat'
  61.     mdarrays = ''
  62.     do while lines('/tmp/backupmdstat') \= 0
  63.       line = linein('/tmp/backupmdstat')
  64.       if word(line,1) = "Personalities" then iterate
  65.       if word(line,2) = ":" then mdarrays = mdarrays","substr(word(line,1),3)
  66.       else iterate
  67.     end
  68.     mdarrays = substr(mdarrays,2)
  69.     interpret 'mdarray = min('mdarrays')'
  70.     if mdarray = 1 then interpret 'backupdev = max('mdarrays') + 1'
  71.                    else backupdev = mdarray -1
  72.       'mdadm --assemble /dev/md'backupdev backupraidmembers
  73.       backupdev = '/dev/md'backupdev':'
  74.     end
  75.   else do
  76.     if userid \= "cron"
  77.       then 'DISPLAY=:1 zenity --info --title='backupdev_missing' --text='backupdev_missing_text
  78.       else 'echo 'date("I") time("N") backupdev_missing_text' >> /tmp/backup.log'
  79.     exit 15
  80.   end
  81. end
  82. backupdev = substr(backupdev,6,length(word(backupdev,1)) -6)
  83. 'mount | grep /dev/'backupdev ' > /tmp/backupmounted'
  84. do x = 0 while lines('/tmp/backupmounted') = 1
  85.   backupmounted = linein('/tmp/backupmounted')
  86. end
  87. select
  88.   when x = 0 then do
  89.     'mount /dev/'backupdev' 'mntbackup' >> /tmp/backupmount.txt 2>&1'
  90.     returncode = RC    
  91.     select
  92.       when returncode = 0 then nop
  93.       when returncode = 1
  94.         then do
  95.           'echo 'date("I") time("N") error_during_mount_text' >> /tmp/backup.log'
  96.           exit 15
  97.         end  
  98.       when returncode = 32 then nop
  99.       otherwise nop
  100.     end
  101.   end
  102.   when x = 1 then do
  103.     if mntbackup \= word(backupmounted,3)
  104.       then
  105.         do
  106.           'umount /dev/'backupdev' > /dev/null 2>&1'
  107.           if RC = \0 then do
  108.             'echo 'date("I") time("N") error_during_umount_text' >> /tmp/backup.log'
  109.             exit 15
  110.           end
  111.           'mount /dev/'backupdev' 'mntbackup' >> /tmp/backupmount.txt 2>&1'
  112.       select
  113.         when RC = 0 then nop
  114.         when RC = 1
  115.           then do
  116.             'echo 'date("I") time("N") error_during_mount_text' >> /tmp/backup.log'
  117.             exit 15
  118.           end  
  119.         when RC = 32 then nop
  120.         otherwise
  121.           'echo 'date("I") time("N") error_during_mount_text' >> /tmp/backup.log'
  122.           exit 15
  123.       end
  124.     end
  125.     mountOK = 1
  126.   end  
  127.   when x > 1 then do
  128.     'umount /dev/'backupdev' > /dev/null 2>&1'
  129.     if RC = \0 then do
  130.       'echo 'date("I") time("N") error_during_umount_text' >> /tmp/backup.log'
  131.       exit 15
  132.     end
  133.     'mount /dev/'backupdev' 'mntbackup' >> /tmp/backupmount.txt 2>&1'
  134.     select
  135.       when RC = 0 then nop
  136.       when RC = 1
  137.         then do
  138.           'echo 'date("I") time("N") error_during_mount_text' >> /tmp/backup.log'
  139.           exit 15
  140.         end  
  141.       when RC = 32 then nop
  142.       otherwise nop
  143.     end
  144.     if RC \= 0 then do
  145.         'echo 'date("I") time("N") wrong_directory_text' >> /tmp/backup.log'
  146.         'rm /tmp/backup*'
  147.         exit 15
  148.       end
  149.       else mountOK = 1
  150.   end  
  151.   otherwise nop
  152. end
  153. 'stat 'backupdir' > /dev/null 2>&1'
  154. returncode = RC
  155. msgmnt = linein('/tmp/backupmount.txt')
  156. select
  157.   when returncode = 0 then nop
  158.   when returncode = 1
  159.     then do
  160.       'mkdir -p 'backupdir' > /dev/null 2>&1'
  161.       'chmod 777 'backupdir' > /dev/null 2>&1'
  162.       if RC \= 0 then do
  163.         'DISPLAY=:1 zenity --info --title='error_during_mkdir' --text='error_during_mkdir_text
  164.         exit 15
  165.       end
  166.     end
  167.   when returncode = 32 then
  168.     if word(msgmnt,7) \= mntbackup"."
  169.       then do
  170.         'echo 'date("I") time("N") wrong_directory_text' >> 'backupdir'/backup.log'
  171.         exit 15
  172.       end
  173.   otherwise
  174.     'echo 'date("I") time("N") wrong_password_text' >> 'backupdir'/backup.log'
  175.     exit 15
  176. end
  177. RC = dobackup()
  178. say "De Backup is genomen"
  179. return
  180. /*************************************************************************************************************************************************************************************************************/
  181. dobackup:
  182. reboot = 0
  183. time = time("N")
  184. do while pos(':',time) \= 0
  185.   firstpartoftime = substr(time,1,pos(':',time)-1)
  186.   lastpartoftime  = substr(time,pos(':',time)+1)
  187.   time = firstpartoftime'-'lastpartoftime
  188. end
  189. do while pos(':',time) \= 0
  190.   firstpartoftime = substr(time,1,pos(':',time)-1)
  191.   lastpartoftime  = substr(time,pos(':',time)+1)
  192.   time = firstpartoftime'-'lastpartoftime
  193. end
  194. if lines(backupdir'/tobackuplist') = 0
  195.   then do
  196.     rc = lineout(backupdir'/'date("I")'_'time_'backup.log','no backuplist')
  197.     exit 15
  198. end
  199. do backupnr = 1 while lines(backupdir'/tobackuplist')
  200.   backupline   = linein(backupdir'/tobackuplist')
  201.   if backupline = 'Reboot' then do
  202.     reboot = 1
  203.     leave
  204.   end
  205.   if backupline = 'PowerOff' then do
  206.     reboot = 0
  207.     leave
  208.   end
  209. trace o
  210.   partuuid     = word(backupline,1)
  211.   'blkid | grep 'partuuid' > /tmp/backuppartuuid'backupnr
  212.   blkidpartition = linein('/tmp/backuppartuuid'backupnr)
  213.   partitionfstype = substr(blkidpartition,(pos('TYPE="',blkidpartition) + 6))
  214.   partitionfstype = substr(partitionfstype,1,pos('" ',partitionfstype) - 1)
  215.   partitiondev    = word(blkidpartition,1)
  216.   partitiondev    = substr(partitiondev,1,length(partitiondev) - 1 )
  217.   backupfile      = '"'backupdir'/'date("I")'_'time'_backup_'partuuid'"'
  218.   blkline. = ''
  219.   blkline.0 = 0
  220.   if userid() \= 'root'
  221.     then 'SUDO_ASKPASS=/usr/local/bin/backup_ask_password sudo -A lsblk > /tmp/lsblk'backupnr
  222.     else 'lsblk > /tmp/lsblk'backupnr
  223.   do tel = 1 while lines('/tmp/lsblk'backupnr)
  224.     lsblkline = linein('/tmp/lsblk'backupnr)
  225.     blkline.tel = lsblkline
  226.     blkline.0 = blkline.0 + 1  
  227.   end
  228.   do t = 1 to blkline.0
  229.     if pos(substr(partitiondev,6),(blkline.t)) \= 0
  230.       then do k = 1 to t
  231.         do j = 1 to t
  232.           if pos('raid',(blkline.t)) = 0 & pos('disk',(blkline.t)) = 0
  233.             then do
  234.               t = t - 1
  235.               iterate
  236.             end
  237.             else do
  238.               if pos('raid',(blkline.t)) \= 0
  239.                 then partitiondisk = '/dev/'substr(word(blkline.t,1),pos('md',blkline.t,1))
  240.                 else partitiondisk = '/dev/'word(blkline.t,1)
  241.               leave t
  242.             end
  243.         end
  244.       end
  245.   end
  246.   if userid() = 'root'
  247.     then do
  248.       rc = lineout(backupfile'.log','backup of the partition starts')
  249.       'partclone.'partitionfstype' -c -L "'backupfile'.log" -s 'partitiondev' | zstd --no-progress -T4 -f -o "'backupfile'.zst"'
  250.       if RC \= 0
  251.         then rc = lineout(backupfile'.log','unsuccesful end')
  252.         else do
  253.           rc = lineout(backupfile'.log','succesfull end')
  254.           'echo 'backupline' > 'backupfile'.info'
  255.         end
  256.     end
  257.     else do
  258.       'echo 'date("I") time("N") 'backup of the partition starts >> 'backupfile'.log'
  259.       'SUDO_ASKPASS=/usr/local/bin/backup_ask_password sudo -A partclone.'partitionfstype' -c -L "'backupfile'.log" -s 'partitiondev' | zstd --no-progress -T4 -f -o "'backupfile'.zst"'
  260.       if RC \= 0
  261.         then 'echo 'date("I") time("N") 'backup of the partition failed >> 'backupfile'.log'
  262.         else do
  263.           'echo 'date("I") time("N") 'backup of the partition was succesfull >> 'backupfile'.log'
  264.           'echo 'backupline' > 'backupfile'.info'
  265.         end
  266.     end
  267.   if userid() = 'root'
  268.     then 'fdisk -l 'partitiondisk' > 'backupfile'.fdisk'
  269.     else 'SUDO_ASKPASS=/usr/local/bin/backup_ask_password sudo -A fdisk -l 'partitiondisk' > 'backupfile'.fdisk'
  270. end
  271. if userid() = 'root'
  272.   then do
  273.     'rm 'backupdir'/tobackuplist'
  274.     if reboot
  275.       then 'reboot'
  276.       else 'poweroff'
  277.   end
  278.   else 'SUDO_ASKPASS=/usr/local/bin/backup_ask_password sudo -A rm 'backupdir'/tobackuplist'
  279. return 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement