Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Function to ask the user to select a subdirectory or dump the whole /tmp directory
- function ask_for_dump()
- print("Do you want to dump the entire /tmp directory or a specific subdirectory?")
- print("1. Dump the whole /tmp directory")
- -- List subdirectories in /tmp
- local subdirs = list_subdirectories("/.tmp")
- if #subdirs > 0 then
- print("2. Choose a subdirectory to dump:")
- for i, dir in ipairs(subdirs) do
- print(string.format("%d. %s", i + 1, dir)) -- Show subdirectories as options
- end
- else
- print("No subdirectories found in /tmp.")
- end
- local choice = tonumber(io.read())
- if choice == 1 then
- dump_directory("/.tmp", "/disk/logs")
- elseif choice > 1 and choice <= #subdirs + 1 then
- local subdir = subdirs[choice - 1] -- Get the selected subdirectory
- local source_path = "/.tmp/" .. subdir
- local dest_path = "/disk/logs/" .. subdir
- dump_directory(source_path, dest_path)
- else
- print("Invalid choice, please try again.")
- ask_for_dump() -- Retry if input is invalid
- end
- end
- -- Function to list subdirectories in a given directory
- function list_subdirectories(path)
- local subdirs = {}
- for _, file in ipairs(fs.list(path)) do
- local full_path = path .. "/" .. file
- if fs.isDir(full_path) then
- table.insert(subdirs, file)
- end
- end
- return subdirs
- end
- -- Function to copy all files from source to destination directory
- function dump_directory(source, destination)
- -- Check if source directory exists
- if not fs.exists(source) then
- print("Source directory does not exist.")
- return
- end
- -- Create the destination directory if it doesn't exist
- if not fs.exists(destination) then
- fs.makeDir(destination)
- end
- -- Check if the destination already has files (logs)
- if fs.exists(destination) then
- print("The destination already contains files. Do you want to make a copy instead? (y/n)")
- local choice = io.read()
- if choice == "y" then
- -- Make a copy by appending "_copy" to the existing log file names
- destination = destination .. "_copy"
- if not fs.exists(destination) then
- fs.makeDir(destination)
- end
- elseif choice == "n" then
- -- If not copying, delete existing logs
- print("Deleting existing files in the destination...")
- delete_files_in_directory(destination)
- else
- print("Invalid choice, please try again.")
- return
- end
- end
- -- Loop through the source directory and copy each file/subdirectory to the destination
- for _, file in ipairs(fs.list(source)) do
- local source_file = source .. "/" .. file
- local dest_file = destination .. "/" .. file
- -- Remove the file if it already exists in the destination
- if fs.exists(dest_file) then
- fs.delete(dest_file)
- end
- if fs.isDir(source_file) then
- -- Recursively copy subdirectories
- dump_directory(source_file, dest_file)
- else
- -- Copy file
- fs.copy(source_file, dest_file)
- end
- end
- print("Dump complete.")
- end
- -- Function to delete all files inside a directory
- function delete_files_in_directory(path)
- for _, file in ipairs(fs.list(path)) do
- local full_path = path .. "/" .. file
- if fs.isDir(full_path) then
- delete_files_in_directory(full_path) -- Recursively delete subdirectories
- else
- fs.delete(full_path) -- Delete files
- end
- end
- end
- -- Run the function to ask the user
- ask_for_dump()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement