def test_check_remote_command_fail(self): cmds = [] if tools.check_command('nice'): cmds.append('nice') self.cfg.set_run_nice_on_remote_enabled(True) if tools.check_command('ionice'): cmds.append('ionice') self.cfg.set_run_ionice_on_remote_enabled(True) if tools.check_command('nocache'): cmds.append('nocache') self.cfg.set_run_nocache_on_remote_enabled(True) if tools.check_command('screen') and tools.check_command('flock'): cmds.extend(('screen', 'flock', 'rmdir', 'mktemp')) self.cfg.set_smart_remove_run_remote_in_background(True) # make one after an other command from 'cmds' fail by symlink them # to /bin/false self.cfg.set_ssh_prefix_enabled(True) false = tools.which('false') for cmd in cmds: msg = 'current trap: %s' %cmd with self.subTest(cmd = cmd): with TemporaryDirectory() as self.remotePath: self.cfg.set_snapshots_path_ssh(self.remotePath) os.symlink(false, os.path.join(self.remotePath, cmd)) self.cfg.set_ssh_prefix("export PATH=%s:$PATH; " %self.remotePath) ssh = sshtools.SSH(cfg = self.cfg) with self.assertRaisesRegex(MountException, r"Remote host .+ doesn't support '.*?%s.*'" %cmd, msg = msg): ssh.check_remote_commands()
def test_check_remote_command(self): self.cfg.set_run_nice_on_remote_enabled(tools.check_command('nice')) self.cfg.set_run_ionice_on_remote_enabled(tools.check_command('ionice')) self.cfg.set_run_nocache_on_remote_enabled(tools.check_command('nocache')) self.cfg.set_smart_remove_run_remote_in_background(tools.check_command('screen') and tools.check_command('flock')) os.mkdir(self.remotePath) ssh = sshtools.SSH(cfg = self.cfg) ssh.check_remote_commands()
def test_check_command(self): """ Test the function check_command """ self.assertFalse(tools.check_command('')) self.assertFalse(tools.check_command("notExistedCommand")) self.assertTrue(tools.check_command("ls")) self.assertTrue(tools.check_command('backintime'))
def on_btn_diff_clicked(self): snapshot_id = self.get_list_snapshot_id() if len(snapshot_id) <= 0: return combo_index = self.combo_diff.currentIndex() if combo_index < 0: return snapshot2_id = str(self.combo_diff.itemData(combo_index).toString()) path1 = self.snapshots.get_snapshot_path_to(snapshot_id, self.path) path2 = self.snapshots.get_snapshot_path_to(snapshot2_id, self.path) #check if the 2 paths are different if path1 == path2: KMessageBox.error( self, QString.fromUtf8(_('You can\'t compare a snapshot to itself'))) return diff_cmd = self.config.get_str_value('kde4.diff.cmd', 'kompare') diff_params = self.config.get_str_value('kde4.diff.params', '%1 %2') if not tools.check_command(diff_cmd): KMessageBox.error( self, QString.fromUtf8(_('Command not found: %s') % diff_cmd)) return params = diff_params params = params.replace('%1', "\"%s\"" % path1) params = params.replace('%2', "\"%s\"" % path2) cmd = diff_cmd + ' ' + params + ' &' os.system(cmd)
def test_check_remote_command_with_spaces(self): self.cfg.set_smart_remove_run_remote_in_background(tools.check_command('screen') and tools.check_command('flock')) self.remotePath = os.path.join(self.tmpDir.name, 'foo bar') self.cfg.set_snapshots_path_ssh(self.remotePath) os.mkdir(self.remotePath) ssh = sshtools.SSH(cfg = self.cfg) ssh.check_remote_commands()
def on_btn_diff_clicked(self): sid1 = self.list_snapshots.currentSnapshotID() sid2 = self.combo_diff.currentSnapshotID() if not sid1 or not sid2: return path1 = sid1.pathBackup(self.path) path2 = sid2.pathBackup(self.path) #check if the 2 paths are different if path1 == path2: messagebox.critical(self, _('You can\'t compare a snapshot to itself')) return diff_cmd = self.config.get_str_value('qt4.diff.cmd', DIFF_CMD) diff_params = self.config.get_str_value('qt4.diff.params', DIFF_PARAMS) if not tools.check_command(diff_cmd): messagebox.critical(self, _('Command not found: %s') % diff_cmd) return params = diff_params params = params.replace('%1', "\"%s\"" % path1) params = params.replace('%2', "\"%s\"" % path2) cmd = diff_cmd + ' ' + params + ' &' os.system(cmd)
def on_btn_diff_clicked( self ): sid1 = self.list_snapshots.currentSnapshotID() sid2 = self.combo_diff.currentSnapshotID() if not sid1 or not sid2: return path1 = sid1.pathBackup(self.path) path2 = sid2.pathBackup(self.path) #check if the 2 paths are different if path1 == path2: messagebox.critical( self, _('You can\'t compare a snapshot to itself') ) return diff_cmd = self.config.get_str_value( 'qt4.diff.cmd', DIFF_CMD ) diff_params = self.config.get_str_value( 'qt4.diff.params', DIFF_PARAMS ) if not tools.check_command( diff_cmd ): messagebox.critical( self, _('Command not found: %s') % diff_cmd ) return params = diff_params params = params.replace( '%1', "\"%s\"" % path1 ) params = params.replace( '%2', "\"%s\"" % path2 ) cmd = diff_cmd + ' ' + params + ' &' os.system( cmd )
def check_fuse(self): """ Check if command in self.mountproc is installed and user is part of group ``fuse``. Raises: exceptions.MountException: if either command is not available or user is not in group fuse """ logger.debug('Check fuse', self) if not tools.check_command(self.mountproc): logger.debug('%s is missing' %self.mountproc, self) raise MountException( _('%(proc)s not found. Please install e.g. %(install_command)s') %{'proc': self.mountproc, 'install_command': "'apt-get install %s'" %self.mountproc}) if self.CHECK_FUSE_GROUP: user = self.config.get_user() try: fuse_grp_members = grp.getgrnam('fuse')[3] except KeyError: #group fuse doesn't exist. So most likely it isn't used by this distribution logger.debug("Group fuse doesn't exist. Skip test", self) return if not user in fuse_grp_members: logger.debug('User %s is not in group fuse' %user, self) raise MountException( _('%(user)s is not member of group \'fuse\'.\n ' 'Run \'sudo adduser %(user)s fuse\'. To apply ' 'changes logout and login again.\nLook at ' '\'man backintime\' for further instructions.') % {'user': user})
def setup_cron( self ): #auto_backup_mode = self.get_automatic_backup_mode() min_backup_mode = self.NONE max_backup_mode = self.NONE if self.get_per_directory_schedule(): for item in self.get_include_folders(): backup_mode = item[1] if self.NONE != backup_mode: if self.NONE == min_backup_mode: min_backup_mode = backup_mode max_backup_mode = backup_mode elif backup_mode < min_backup_mode: min_backup_mode = backup_mode elif backup_mode > max_backup_mode: max_backup_mode = backup_mode print "Min automatic backup: %s" % self.AUTOMATIC_BACKUP_MODES[ min_backup_mode ] print "Max automatic backup: %s" % self.AUTOMATIC_BACKUP_MODES[ max_backup_mode ] else: min_backup_mode = self.get_automatic_backup_mode() max_backup_mode = min_backup_mode print "Automatic backup: %s" % self.AUTOMATIC_BACKUP_MODES[ min_backup_mode ] if self.NONE == min_backup_mode: os.system( "crontab -l | grep -v backintime | crontab -" ) return None #no automatic backup if not tools.check_command( 'crontab' ): return _( 'Can\'t find crontab.\nAre you sure cron is installed ?\nIf not you should disable all automatic backups.' ) #remove old cron os.system( "crontab -l | grep -v backintime | crontab -" ) cron_line = '' if self._5_MIN == min_backup_mode: cron_line = 'echo "*/5 * * * * {cmd}"' elif self._10_MIN == min_backup_mode: cron_line = 'echo "*/10 * * * * {cmd}"' if self.HOUR == min_backup_mode: cron_line = 'echo "@hourly {cmd}"' elif self.DAY == min_backup_mode: cron_line = 'echo "@daily {cmd}"' elif self.WEEK == min_backup_mode and self.MONTH == max_backup_mode: #for every-week and every-month use every-day cron_line = 'echo "@daily {cmd}"' elif self.WEEK == min_backup_mode: cron_line = 'echo "@weekly {cmd}"' elif self.MONTH == min_backup_mode: cron_line = 'echo "@monthly {cmd}"' if len( cron_line ) > 0: cron_line = cron_line.replace( '{cmd}', 'nice -n 19 /usr/bin/backintime --backup-job >/dev/null 2>&1' ) os.system( "( crontab -l; %s ) | crontab -" % cron_line ) return None
def on_btn_diff_with_clicked(self, button): if len(self.store_snapshots) < 1: return #get path from the list iter = self.list_snapshots.get_selection().get_selected()[1] if iter is None: return path1 = self.snapshots.get_snapshot_path_to( self.store_snapshots.get_value(iter, 1), self.path) #get path from the combo path2 = self.snapshots.get_snapshot_path_to( self.store_snapshots.get_value( self.combo_diff_with.get_active_iter(), 1), self.path) #check if the 2 paths are different if path1 == path2: gnomemessagebox.show_error( self.dialog, self.config, _('You can\'t compare a snapshot to itself')) return diff_cmd = self.edit_diff_cmd.get_text() diff_cmd_params = self.edit_diff_cmd_params.get_text() if not tools.check_command(diff_cmd): gnomemessagebox.show_error(self.dialog, self.config, _('Command not found: %s') % diff_cmd) return params = diff_cmd_params params = params.replace('%1', "\"%s\"" % path1) params = params.replace('%2', "\"%s\"" % path2) cmd = diff_cmd + ' ' + params + ' &' os.system(cmd) #check if the command changed old_diff_cmd = self.config.get_str_value('gnome.diff.cmd', 'meld') old_diff_cmd_params = self.config.get_str_value( 'gnome.diff.params', '%1 %2') if diff_cmd != old_diff_cmd or diff_cmd_params != old_diff_cmd_params: self.config.set_str_value('gnome.diff.cmd', diff_cmd) self.config.set_str_value('gnome.diff.params', diff_cmd_params) self.config.save()
def check_fuse(self): """ check if encfs is installed and user is part of group fuse """ logger.debug('Check fuse', self) if not tools.check_command('encfs'): logger.debug('sshfs is missing', self) raise MountException( _('encfs not found. Please install e.g. \'apt-get install encfs\'') ) if self.CHECK_FUSE_GROUP: user = self.config.get_user() try: fuse_grp_members = grp.getgrnam('fuse')[3] except KeyError: #group fuse doesn't exist. So most likely it isn't used by this distribution logger.debug("Group fuse doesn't exist. Skip test", self) return if not user in fuse_grp_members: logger.debug('User %s is not in group fuse' %user, self) raise MountException( _('%(user)s is not member of group \'fuse\'.\n Run \'sudo adduser %(user)s fuse\'. To apply changes logout and login again.\nLook at \'man backintime\' for further instructions.') % {'user': user})
def on_btn_diff_with_clicked( self, button ): if len( self.store_snapshots ) < 1: return #get path from the list iter = self.list_snapshots.get_selection().get_selected()[1] if iter is None: return path1 = self.snapshots.get_snapshot_path_to( self.store_snapshots.get_value( iter, 1 ), self.path ) #get path from the combo path2 = self.snapshots.get_snapshot_path_to( self.store_snapshots.get_value( self.combo_diff_with.get_active_iter(), 1 ), self.path ) #check if the 2 paths are different if path1 == path2: gnomemessagebox.show_error( self.dialog, self.config, _('You can\'t compare a snapshot to itself') ) return diff_cmd = self.edit_diff_cmd.get_text() diff_cmd_params = self.edit_diff_cmd_params.get_text() if not tools.check_command( diff_cmd ): gnomemessagebox.show_error( self.dialog, self.config, _('Command not found: %s') % diff_cmd ) return params = diff_cmd_params params = params.replace( '%1', "\"%s\"" % path1 ) params = params.replace( '%2', "\"%s\"" % path2 ) cmd = diff_cmd + ' ' + params + ' &' os.system( cmd ) #check if the command changed old_diff_cmd = self.config.get_str_value( 'gnome.diff.cmd', 'meld' ) old_diff_cmd_params = self.config.get_str_value( 'gnome.diff.params', '%1 %2' ) if diff_cmd != old_diff_cmd or diff_cmd_params != old_diff_cmd_params: self.config.set_str_value( 'gnome.diff.cmd', diff_cmd ) self.config.set_str_value( 'gnome.diff.params', diff_cmd_params ) self.config.save()
def check_fuse(self): """ check if sshfs is installed and user is part of group fuse """ logger.debug('Check fuse', self) if not tools.check_command('sshfs'): logger.debug('sshfs is missing', self) raise MountException( _('sshfs not found. Please install e.g. \'apt-get install sshfs\'') ) if self.CHECK_FUSE_GROUP: user = self.config.get_user() try: fuse_grp_members = grp.getgrnam('fuse')[3] except KeyError: #group fuse doesn't exist. So most likely it isn't used by this distribution logger.debug("Group fuse doesn't exist. Skip test", self) return if not user in fuse_grp_members: logger.debug('User %s is not in group fuse' %user, self) raise MountException( _('%(user)s is not member of group \'fuse\'.\n ' 'Run \'sudo adduser %(user)s fuse\'. To apply ' 'changes logout and login again.\nLook at ' '\'man backintime\' for further instructions.') % {'user': user})
def test_check_command(self): """ Test the function check_command """ self.assertFalse(tools.check_command("notExistedCommand")) self.assertTrue(tools.check_command("ls"))
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. import os import gettext from PyQt4.QtGui import * from PyQt4.QtCore import * import tools import restoredialog import messagebox import qt4tools _ = gettext.gettext if tools.check_command('meld'): DIFF_CMD = 'meld' DIFF_PARAMS = '%1 %2' elif tools.check_command('kompare'): DIFF_CMD = 'kompare' DIFF_PARAMS = '%1 %2' else: DIFF_CMD = 'false' DIFF_PARAMS = '%1 %2' class DiffOptionsDialog(QDialog): def __init__(self, parent): super(DiffOptionsDialog, self).__init__(parent) self.config = parent.config
from test import generic sys.path.append(os.path.join(os.path.dirname(__file__), '..')) import tools import config import configfile #chroot jails used for building may have no UUID devices (because of tmpfs) #we need to skip tests that require UUIDs DISK_BY_UUID_AVAILABLE = os.path.exists(tools.DISK_BY_UUID) UDEVADM_HAS_UUID = subprocess.Popen(['udevadm', 'info', '-e'], stdout = subprocess.PIPE, stderr = subprocess.DEVNULL ).communicate()[0].find(b'ID_FS_UUID=') > 0 RSYNC_INSTALLED = tools.check_command('rsync') RSYNC_307_VERSION = """rsync version 3.0.7 protocol version 30 Copyright (C) 1996-2009 by Andrew Tridgell, Wayne Davison, and others. Web site: http://rsync.samba.org/ Capabilities: 64-bit files, 64-bit inums, 32-bit timestamps, 64-bit long ints, socketpairs, hardlinks, symlinks, IPv6, batchfiles, inplace, append, ACLs, xattrs, iconv, symtimes rsync comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under certain conditions. See the GNU General Public Licence for details. """ RSYNC_310_VERSION = """rsync version 3.1.0 protocol version 31 Copyright (C) 1996-2013 by Andrew Tridgell, Wayne Davison, and others.
import os import gettext from PyQt4.QtGui import * from PyQt4.QtCore import * import tools import restoredialog import messagebox import qt4tools import snapshots _=gettext.gettext if tools.check_command('meld'): DIFF_CMD = 'meld' DIFF_PARAMS = '%1 %2' elif tools.check_command('kompare'): DIFF_CMD = 'kompare' DIFF_PARAMS = '%1 %2' else: DIFF_CMD = 'false' DIFF_PARAMS = '%1 %2' class DiffOptionsDialog( QDialog ): def __init__( self, parent ): super(DiffOptionsDialog, self).__init__(parent) self.config = parent.config import icon
def setup_cron(self): #auto_backup_mode = self.get_automatic_backup_mode() min_backup_mode = self.NONE max_backup_mode = self.NONE if self.get_per_directory_schedule(): for item in self.get_include_folders(): backup_mode = item[1] if self.NONE != backup_mode: if self.NONE == min_backup_mode: min_backup_mode = backup_mode max_backup_mode = backup_mode elif backup_mode < min_backup_mode: min_backup_mode = backup_mode elif backup_mode > max_backup_mode: max_backup_mode = backup_mode print "Min automatic backup: %s" % self.AUTOMATIC_BACKUP_MODES[ min_backup_mode] print "Max automatic backup: %s" % self.AUTOMATIC_BACKUP_MODES[ max_backup_mode] else: min_backup_mode = self.get_automatic_backup_mode() max_backup_mode = min_backup_mode print "Automatic backup: %s" % self.AUTOMATIC_BACKUP_MODES[ min_backup_mode] if self.NONE == min_backup_mode: os.system("crontab -l | grep -v backintime | crontab -") return None #no automatic backup if not tools.check_command('crontab'): return _( 'Can\'t find crontab.\nAre you sure cron is installed ?\nIf not you should disable all automatic backups.' ) #remove old cron os.system("crontab -l | grep -v backintime | crontab -") cron_line = '' if self._5_MIN == min_backup_mode: cron_line = 'echo "*/5 * * * * {cmd}"' elif self._10_MIN == min_backup_mode: cron_line = 'echo "*/10 * * * * {cmd}"' if self.HOUR == min_backup_mode: cron_line = 'echo "@hourly {cmd}"' elif self.DAY == min_backup_mode: cron_line = 'echo "@daily {cmd}"' elif self.WEEK == min_backup_mode and self.MONTH == max_backup_mode: #for every-week and every-month use every-day cron_line = 'echo "@daily {cmd}"' elif self.WEEK == min_backup_mode: cron_line = 'echo "@weekly {cmd}"' elif self.MONTH == min_backup_mode: cron_line = 'echo "@monthly {cmd}"' if len(cron_line) > 0: cron_line = cron_line.replace( '{cmd}', 'nice -n 19 /usr/bin/backintime --backup-job >/dev/null 2>&1') os.system("( crontab -l; %s ) | crontab -" % cron_line) return None
# with this program; if not, write to the Free Software Foundation,Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. import os import sys import tempfile import unittest from test import generic sys.path.append(os.path.join(os.path.dirname(__file__), '..')) import config import logger import mount import tools HAS_BINDFS = tools.check_command('bindfs') class TestLocal(generic.TestCase): def setUp(self): super(TestLocal, self).setUp() logger.DEBUG = '-v' in sys.argv self.config = config.Config() self.config.set_snapshots_mode('local') self.mount_kwargs = {} @unittest.skipIf(not HAS_BINDFS, 'Skip as this test requires bindfs to be installed') def test_can_mount_local_rw(self): self.internal_test(read_only = False, implicit_read_only = False) @unittest.skipIf(not HAS_BINDFS, 'Skip as this test requires bindfs to be installed')