Advertisement
DOGGYWOOF

Doggy OS Temp Cache dump service

Nov 20th, 2024 (edited)
7
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. -- Function to ask the user to select a subdirectory or dump the whole /tmp directory
  2. function ask_for_dump()
  3. print("Do you want to dump the entire /tmp directory or a specific subdirectory?")
  4. print("1. Dump the whole /tmp directory")
  5.  
  6. -- List subdirectories in /tmp
  7. local subdirs = list_subdirectories("/.tmp")
  8. if #subdirs > 0 then
  9. print("2. Choose a subdirectory to dump:")
  10. for i, dir in ipairs(subdirs) do
  11. print(string.format("%d. %s", i + 1, dir)) -- Show subdirectories as options
  12. end
  13. else
  14. print("No subdirectories found in /tmp.")
  15. end
  16.  
  17. local choice = tonumber(io.read())
  18.  
  19. if choice == 1 then
  20. dump_directory("/.tmp", "/disk/logs")
  21. elseif choice > 1 and choice <= #subdirs + 1 then
  22. local subdir = subdirs[choice - 1] -- Get the selected subdirectory
  23. local source_path = "/.tmp/" .. subdir
  24. local dest_path = "/disk/logs/" .. subdir
  25.  
  26. dump_directory(source_path, dest_path)
  27. else
  28. print("Invalid choice, please try again.")
  29. ask_for_dump() -- Retry if input is invalid
  30. end
  31. end
  32.  
  33. -- Function to list subdirectories in a given directory
  34. function list_subdirectories(path)
  35. local subdirs = {}
  36. for _, file in ipairs(fs.list(path)) do
  37. local full_path = path .. "/" .. file
  38. if fs.isDir(full_path) then
  39. table.insert(subdirs, file)
  40. end
  41. end
  42. return subdirs
  43. end
  44.  
  45. -- Function to copy all files from source to destination directory
  46. function dump_directory(source, destination)
  47. -- Check if source directory exists
  48. if not fs.exists(source) then
  49. print("Source directory does not exist.")
  50. return
  51. end
  52.  
  53. -- Create the destination directory if it doesn't exist
  54. if not fs.exists(destination) then
  55. fs.makeDir(destination)
  56. end
  57.  
  58. -- Check if the destination already has files (logs)
  59. if fs.exists(destination) then
  60. print("The destination already contains files. Do you want to make a copy instead? (y/n)")
  61. local choice = io.read()
  62.  
  63. if choice == "y" then
  64. -- Make a copy by appending "_copy" to the existing log file names
  65. destination = destination .. "_copy"
  66. if not fs.exists(destination) then
  67. fs.makeDir(destination)
  68. end
  69. elseif choice == "n" then
  70. -- If not copying, delete existing logs
  71. print("Deleting existing files in the destination...")
  72. delete_files_in_directory(destination)
  73. else
  74. print("Invalid choice, please try again.")
  75. return
  76. end
  77. end
  78.  
  79. -- Loop through the source directory and copy each file/subdirectory to the destination
  80. for _, file in ipairs(fs.list(source)) do
  81. local source_file = source .. "/" .. file
  82. local dest_file = destination .. "/" .. file
  83.  
  84. -- Remove the file if it already exists in the destination
  85. if fs.exists(dest_file) then
  86. fs.delete(dest_file)
  87. end
  88.  
  89. if fs.isDir(source_file) then
  90. -- Recursively copy subdirectories
  91. dump_directory(source_file, dest_file)
  92. else
  93. -- Copy file
  94. fs.copy(source_file, dest_file)
  95. end
  96. end
  97. print("Dump complete.")
  98. end
  99.  
  100. -- Function to delete all files inside a directory
  101. function delete_files_in_directory(path)
  102. for _, file in ipairs(fs.list(path)) do
  103. local full_path = path .. "/" .. file
  104. if fs.isDir(full_path) then
  105. delete_files_in_directory(full_path) -- Recursively delete subdirectories
  106. else
  107. fs.delete(full_path) -- Delete files
  108. end
  109. end
  110. end
  111.  
  112. -- Run the function to ask the user
  113. ask_for_dump()
  114.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement