Example #1
0
def backup_files():
    """
    To backup the files 'rs.connexionsw' server:
    fab -H rs.web.connexionsw backup_files
    """
    print(green("Backup files on '{}'").format(env.host_string))
    name = env.host_string.replace('.', '_')
    name = name.replace('-', '_')
    path = Path(name, 'files')
    run('mkdir -p {0}'.format(path.remote_folder()))
    with cd(path.files_folder()), hide('stdout'):
        run('tar -czf {} .'.format(path.remote_file()))
    get(path.remote_file(), path.local_file())
Example #2
0
class TestPath(unittest.TestCase):

    def setUp(self):
        self.path = Path('csw_web', 'postgres')
        self.user = getpass.getuser()
        self.year = datetime.now().strftime('%Y')

    def test_backup_file_name(self):
        backup_file_name = self.path.backup_file_name()
        self.assertIn('csw_web_', backup_file_name)
        self.assertIn(self.user, backup_file_name)
        self.assertIn(self.year, backup_file_name)
        self.assertIn('sql', backup_file_name)

    def test_backup_file_name_postgres(self):
        backup_file_name = self.path.backup_file_name()
        self.assertIn('sql', backup_file_name)

    def test_backup_file_name_files(self):
        path = Path('csw_web', 'files')
        backup_file_name = path.backup_file_name()
        self.assertIn('tar.gz', backup_file_name)

    def test_backup_file_name_user(self):
        path = Path('raymond@csw_web', 'postgres')
        self.assertNotIn('raymond', path.backup_file_name())

    def test_database_name(self):
        database_name = self.path.database_name()
        self.assertNotIn('sql', database_name)

    def test_files_folder(self):
        self.assertEquals('/home/web/repo/files', self.path.files_folder())

    def test_invalid_name(self):
        with self.assertRaises(TaskError) as cm:
            Path('csw_web_*', 'postgres')
        self.assertIn('invalid characters', cm.exception.value)

    def test_invalid_file_type(self):
        with self.assertRaises(TaskError) as cm:
            Path('csw_web_', 'smartie')
        self.assertIn('invalid file type', cm.exception.value)

    def test_local_file_files(self):
        path = Path('csw_web', 'files')
        local_file = path.local_file()
        self.assertIn('/repo/backup/files/csw_web_', local_file)
        self.assertIn('.tar.gz', local_file)

    def test_local_file_postgres(self):
        local_file = self.path.local_file()
        self.assertIn('home', local_file)
        self.assertIn(self.user, local_file)
        self.assertIn('/repo/backup/postgres/csw_web_', local_file)
        self.assertIn('.sql', local_file)

    def test_remote_file_files(self):
        path = Path('csw_web', 'files')
        remote_file = path.remote_file()
        self.assertIn('/repo/backup/files/csw_web_', remote_file)
        self.assertIn('.tar.gz', remote_file)

    def test_remote_file_name_postgres(self):
        remote_file = self.path.remote_file()
        self.assertIn('/repo/backup/postgres/csw_web_', remote_file)
        self.assertIn('.sql', remote_file)

    def test_remote_folder_files(self):
        path = Path('csw_web', 'files')
        remote_folder = path.remote_folder()
        self.assertIn('/repo/backup/files', remote_folder)

    def test_remote_folder_postgres(self):
        remote_folder = self.path.remote_folder()
        self.assertIn('/repo/backup/postgres', remote_folder)

    def test_test_database_name(self):
        self.assertEquals(
            self.path.test_database_name(),
            'test_csw_web_{}'.format(getpass.getuser())
        )

    def test_user_name(self):
        self.assertEquals(self.path.user_name(), self.user)