1import os 2 3from errbot import BotPlugin, botcmd 4 5 6class Backup(BotPlugin): 7 """Backup related commands.""" 8 9 @botcmd(admin_only=True) 10 def backup(self, msg, args): 11 """Backup everything. 12 Makes a backup script called backup.py in the data bot directory. 13 You can restore the backup from the command line with errbot --restore 14 """ 15 filename = os.path.join(self.bot_config.BOT_DATA_DIR, "backup.py") 16 with open(filename, "w") as f: 17 f.write( 18 "## This file is not executable on its own. use errbot -r FILE to restore your bot.\n\n" 19 ) 20 f.write('log.info("Restoring repo_manager.")\n') 21 for key, value in self._bot.repo_manager.items(): 22 f.write('bot.repo_manager["' + key + '"] = ' + repr(value) + "\n") 23 f.write('log.info("Restoring plugin_manager.")\n') 24 for ( 25 key, 26 value, 27 ) in ( 28 self._bot.plugin_manager.items() 29 ): # don't mimic that in real plugins, this is core only. 30 f.write('bot.plugin_manager["' + key + '"] = ' + repr(value) + "\n") 31 32 f.write('log.info("Installing plugins.")\n') 33 f.write('if "installed_repos" in bot.repo_manager:\n') 34 f.write(' for repo in bot.repo_manager["installed_repos"]:\n') 35 f.write(" log.error(bot.repo_manager.install_repo(repo))\n") 36 37 f.write('log.info("Restoring plugins data.")\n') 38 f.write( 39 "bot.plugin_manager.update_plugin_places(bot.repo_manager.get_all_repos_paths())\n" 40 ) 41 for plugin in self._bot.plugin_manager.plugins.values(): 42 if plugin._store: 43 f.write( 44 'pobj = bot.plugin_manager.plugins["' + plugin.name + '"]\n' 45 ) 46 f.write("pobj.init_storage()\n") 47 48 for key, value in plugin.items(): 49 f.write('pobj["' + key + '"] = ' + repr(value) + "\n") 50 f.write("pobj.close_storage()\n") 51 52 return f'The backup file has been written in "{filename}".' 53