mongo_backup.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. """
  2. MIT License
  3. Copyright (c) 2024 TheHamkerCat
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. """
  20. from os import remove
  21. from os import system as execute
  22. from pyrogram import enums, filters
  23. from pyrogram.types import Message
  24. from wbb import MONGO_URL, SUDOERS, app
  25. @app.on_message(filters.command("backup") & SUDOERS)
  26. async def backup(_, message: Message):
  27. if message.chat.type != enums.ChatType.PRIVATE:
  28. return await message.reply("This command can only be used in private")
  29. m = await message.reply("Backing up data...")
  30. code = execute(f'mongodump --uri "{MONGO_URL}"')
  31. if int(code) != 0:
  32. return await m.edit(
  33. "Looks like you don't have mongo-database-tools installed "
  34. + "grab it from mongodb.com/try/download/database-tools"
  35. )
  36. code = execute("zip backup.zip -r9 dump/*")
  37. if int(code) != 0:
  38. return await m.edit(
  39. "Looks like you don't have `zip` package installed, BACKUP FAILED!"
  40. )
  41. await message.reply_document("backup.zip")
  42. await m.delete()
  43. remove("backup.zip")