Advertisement
gur111

update_scripts.lua

Jun 21st, 2025 (edited)
1,546
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.36 KB | None | 1 0
  1. -- pastebin ID: 4nM1G6YK
  2. -- URL: https://pastebin.com/4nM1G6YK
  3.  
  4.  
  5. -- Update script for downloading latest versions from pastebin
  6. -- Usage: update
  7.  
  8. -- Configuration: Add your pastebin IDs and corresponding filenames here
  9. local scripts = {
  10.     -- Format: ["pastebin_id"] = "filename",
  11.     -- Your actual pastebin IDs:
  12.     ["Qky674zS"] = "central_server",
  13.     ["LXSNkj5r"] = "mine",
  14.     ["QbSGDWvG"] = "turtle_server",
  15.     ["kfHc79KV"] = "remotectl",
  16.     ["uqfTY1e5"] = "platform",
  17.     ["4nM1G6YK"] = "update",
  18.  
  19.     -- Add more scripts here as needed:
  20. }
  21.  
  22. -- Function to check if a file exists
  23. local function fileExists(filename)
  24.     return fs.exists(filename)
  25. end
  26.  
  27. -- Function to backup a file
  28. local function backupFile(filename)
  29.     if fileExists(filename) then
  30.         local backupName = filename .. ".backup"
  31.         if fileExists(backupName) then
  32.             fs.delete(backupName)
  33.         end
  34.         fs.copy(filename, backupName)
  35.         print("  Created backup: " .. backupName)
  36.         return true
  37.     end
  38.     return false
  39. end
  40.  
  41. -- Function to download a script from pastebin
  42. local function downloadScript(pastebinId, filename, createBackup)
  43.     print("Updating " .. filename .. "...")
  44.  
  45.     -- Create backup if requested and file exists
  46.     if createBackup then
  47.         backupFile(filename)
  48.     end
  49.  
  50.     -- Remove existing file if it exists
  51.     if fileExists(filename) then
  52.         print("  Removing existing " .. filename)
  53.         fs.delete(filename)
  54.     end
  55.  
  56.     -- Download new version
  57.     print("  Downloading from pastebin ID: " .. pastebinId)
  58.     local success = shell.run("pastebin", "get", pastebinId, filename)
  59.  
  60.     if success then
  61.         print("  Successfully updated " .. filename)
  62.         return true
  63.     else
  64.         print("  Failed to download " .. filename)
  65.  
  66.         -- Restore backup if download failed and backup exists
  67.         local backupName = filename .. ".backup"
  68.         if createBackup and fileExists(backupName) then
  69.             print("  Restoring backup...")
  70.             fs.copy(backupName, filename)
  71.         end
  72.         return false
  73.     end
  74. end
  75.  
  76. -- Main update function
  77. local function updateAll(createBackups)
  78.     print("Starting script update process...")
  79.     if createBackups then
  80.         print("Backup mode: ON")
  81.     end
  82.     print("=" .. string.rep("=", 50))
  83.  
  84.     local totalScripts = 0
  85.     local successCount = 0
  86.     local failCount = 0
  87.  
  88.     -- Count total scripts
  89.     for _ in pairs(scripts) do
  90.         totalScripts = totalScripts + 1
  91.     end
  92.  
  93.     if totalScripts == 0 then
  94.         print("No scripts configured for update.")
  95.         print("Please edit the 'scripts' table in update.lua to add your pastebin IDs and filenames.")
  96.         return
  97.     end
  98.  
  99.     -- Update each script
  100.     for pastebinId, filename in pairs(scripts) do
  101.         if downloadScript(pastebinId, filename, createBackups) then
  102.             successCount = successCount + 1
  103.         else
  104.             failCount = failCount + 1
  105.         end
  106.         print() -- Empty line for readability
  107.     end
  108.  
  109.     -- Summary
  110.     print("=" .. string.rep("=", 50))
  111.     print("Update complete!")
  112.     print("Total scripts: " .. totalScripts)
  113.     print("Successfully updated: " .. successCount)
  114.     print("Failed: " .. failCount)
  115.  
  116.     if failCount > 0 then
  117.         print("\nSome updates failed. Please check:")
  118.         print("- Internet connection")
  119.         print("- Pastebin IDs are correct")
  120.         print("- Pastebin service is available")
  121.     end
  122.  
  123.     if createBackups and successCount > 0 then
  124.         print("\nBackup files created with .backup extension")
  125.         print("You can remove them with: update cleanup")
  126.     end
  127. end
  128.  
  129. -- Function to list configured scripts
  130. local function listScripts()
  131.     print("Configured scripts:")
  132.     print("=" .. string.rep("=", 50))
  133.  
  134.     if next(scripts) == nil then
  135.         print("No scripts configured.")
  136.         print("Edit update.lua to add your pastebin IDs and filenames.")
  137.     else
  138.         for pastebinId, filename in pairs(scripts) do
  139.             local status = fileExists(filename) and "EXISTS" or "MISSING"
  140.             print(string.format("%-20s -> %-10s [%s]", filename, pastebinId, status))
  141.         end
  142.     end
  143. end
  144.  
  145. -- Function to clean up backup files
  146. local function cleanup()
  147.     print("Cleaning up backup files...")
  148.     local cleanedCount = 0
  149.  
  150.     for _, filename in pairs(scripts) do
  151.         local backupName = filename .. ".backup"
  152.         if fileExists(backupName) then
  153.             fs.delete(backupName)
  154.             print("Removed: " .. backupName)
  155.             cleanedCount = cleanedCount + 1
  156.         end
  157.     end
  158.  
  159.     print("Cleaned up " .. cleanedCount .. " backup files.")
  160. end
  161.  
  162. -- Function to add a new script to the update list
  163. local function addScript(pastebinId, filename)
  164.     if not pastebinId or not filename then
  165.         print("Usage: update add <pastebin_id> <filename>")
  166.         return
  167.     end
  168.  
  169.     -- This is a simple way to show how to add scripts
  170.     -- In practice, you'd want to modify the file or use a separate config file
  171.     print("To add this script to the update list, edit update.lua and add:")
  172.     print('["' .. pastebinId .. '"] = "' .. filename .. '",')
  173.     print("to the scripts table.")
  174. end
  175.  
  176. -- Function to show help
  177. local function showHelp()
  178.     print("Update Script Help")
  179.     print("=" .. string.rep("=", 50))
  180.     print("update              - Update all scripts (no backups)")
  181.     print("update backup       - Update all scripts with backups")
  182.     print("update list         - List configured scripts and status")
  183.     print("update cleanup      - Remove all .backup files")
  184.     print("update add <id> <file> - Show how to add a script")
  185.     print("update help         - Show this help")
  186.     print("")
  187.     print("Configuration:")
  188.     print("Edit the 'scripts' table in update.lua to add your pastebin IDs and filenames")
  189. end
  190.  
  191. -- Command line argument handling
  192. local args = {...}
  193. local command = args[1] or "update"
  194.  
  195. if command == "update" or command == "" then
  196.     updateAll(false)
  197. elseif command == "backup" then
  198.     updateAll(true)
  199. elseif command == "add" then
  200.     addScript(args[2], args[3])
  201. elseif command == "list" then
  202.     listScripts()
  203. elseif command == "cleanup" then
  204.     cleanup()
  205. elseif command == "help" then
  206.     showHelp()
  207. else
  208.     print("Unknown command: " .. command)
  209.     print("Use 'update help' for available commands.")
  210. end
  211.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement