Esempio n. 1
0
	def test_partial_restore(self):
		_now = now()
		for num in range(10):
			frappe.get_doc({
				"doctype": "ToDo",
				"date": add_to_date(_now, days=num),
				"description": frappe.mock("paragraph")
			}).insert()
		frappe.db.commit()
		todo_count = frappe.db.count("ToDo")

		# check if todos exist, create a partial backup and see if the state is the same after restore
		self.assertIsNot(todo_count, 0)
		self.execute("bench --site {site} backup --only 'ToDo'")
		db_path = fetch_latest_backups(partial=True)["database"]
		self.assertTrue("partial" in db_path)

		frappe.db.sql_ddl("DROP TABLE IF EXISTS `tabToDo`")
		frappe.db.commit()

		self.execute("bench --site {site} partial-restore {path}", {"path": db_path})
		self.assertEquals(self.returncode, 0)
		self.assertEquals(frappe.db.count("ToDo"), todo_count)
Esempio n. 2
0
	def test_backup(self):
		home = os.path.expanduser("~")
		site_backup_path = frappe.utils.get_site_path("private", "backups")

		# test 1: take a backup
		before_backup = fetch_latest_backups()
		self.execute("bench --site {site} backup")
		after_backup = fetch_latest_backups()

		self.assertEquals(self.returncode, 0)
		self.assertIn("successfully completed", self.stdout)
		self.assertNotEqual(before_backup["database"], after_backup["database"])

		# test 2: take a backup with --with-files
		before_backup = after_backup.copy()
		self.execute("bench --site {site} backup --with-files")
		after_backup = fetch_latest_backups()

		self.assertEquals(self.returncode, 0)
		self.assertIn("successfully completed", self.stdout)
		self.assertIn("with files", self.stdout)
		self.assertNotEqual(before_backup, after_backup)
		self.assertIsNotNone(after_backup["public"])
		self.assertIsNotNone(after_backup["private"])

		# test 3: take a backup with --backup-path
		backup_path = os.path.join(home, "backups")
		self.execute("bench --site {site} backup --backup-path {backup_path}", {"backup_path": backup_path})

		self.assertEquals(self.returncode, 0)
		self.assertTrue(os.path.exists(backup_path))
		self.assertGreaterEqual(len(os.listdir(backup_path)), 2)

		# test 4: take a backup with --backup-path-db, --backup-path-files, --backup-path-private-files, --backup-path-conf
		kwargs = {
			key: os.path.join(home, key, value)
			for key, value in {
				"db_path": "database.sql.gz",
				"files_path": "public.tar",
				"private_path": "private.tar",
				"conf_path": "config.json"
			}.items()
		}

		self.execute("""bench
			--site {site} backup --with-files
			--backup-path-db {db_path}
			--backup-path-files {files_path}
			--backup-path-private-files {private_path}
			--backup-path-conf {conf_path}""", kwargs)

		self.assertEquals(self.returncode, 0)
		for path in kwargs.values():
			self.assertTrue(os.path.exists(path))

		# test 5: take a backup with --compress
		self.execute("bench --site {site} backup --with-files --compress")

		self.assertEquals(self.returncode, 0)

		compressed_files = glob(site_backup_path + "/*.tgz")
		self.assertGreater(len(compressed_files), 0)

		# test 6: take a backup with --verbose
		self.execute("bench --site {site} backup --verbose")
		self.assertEquals(self.returncode, 0)
Esempio n. 3
0
    def test_backup(self):
        backup = {
            "includes": {
                "includes": [
                    "ToDo",
                    "Note",
                ]
            },
            "excludes": {
                "excludes": ["Activity Log", "Access Log", "Error Log"]
            },
        }
        home = os.path.expanduser("~")
        site_backup_path = frappe.utils.get_site_path("private", "backups")

        # test 1: take a backup
        before_backup = fetch_latest_backups()
        self.execute("bench --site {site} backup")
        after_backup = fetch_latest_backups()

        self.assertEquals(self.returncode, 0)
        self.assertIn("successfully completed", self.stdout)
        self.assertNotEqual(before_backup["database"],
                            after_backup["database"])

        # test 2: take a backup with --with-files
        before_backup = after_backup.copy()
        self.execute("bench --site {site} backup --with-files")
        after_backup = fetch_latest_backups()

        self.assertEquals(self.returncode, 0)
        self.assertIn("successfully completed", self.stdout)
        self.assertIn("with files", self.stdout)
        self.assertNotEqual(before_backup, after_backup)
        self.assertIsNotNone(after_backup["public"])
        self.assertIsNotNone(after_backup["private"])

        # test 3: take a backup with --backup-path
        backup_path = os.path.join(home, "backups")
        self.execute("bench --site {site} backup --backup-path {backup_path}",
                     {"backup_path": backup_path})

        self.assertEquals(self.returncode, 0)
        self.assertTrue(os.path.exists(backup_path))
        self.assertGreaterEqual(len(os.listdir(backup_path)), 2)

        # test 4: take a backup with --backup-path-db, --backup-path-files, --backup-path-private-files, --backup-path-conf
        kwargs = {
            key: os.path.join(home, key, value)
            for key, value in {
                "db_path": "database.sql.gz",
                "files_path": "public.tar",
                "private_path": "private.tar",
                "conf_path": "config.json",
            }.items()
        }

        self.execute(
            """bench
			--site {site} backup --with-files
			--backup-path-db {db_path}
			--backup-path-files {files_path}
			--backup-path-private-files {private_path}
			--backup-path-conf {conf_path}""",
            kwargs,
        )

        self.assertEquals(self.returncode, 0)
        for path in kwargs.values():
            self.assertTrue(os.path.exists(path))

        # test 5: take a backup with --compress
        self.execute("bench --site {site} backup --with-files --compress")
        self.assertEquals(self.returncode, 0)
        compressed_files = glob.glob(site_backup_path + "/*.tgz")
        self.assertGreater(len(compressed_files), 0)

        # test 6: take a backup with --verbose
        self.execute("bench --site {site} backup --verbose")
        self.assertEquals(self.returncode, 0)

        # test 7: take a backup with frappe.conf.backup.includes
        self.execute(
            "bench --site {site} set-config backup '{includes}' --parse",
            {"includes": json.dumps(backup["includes"])},
        )
        self.execute("bench --site {site} backup --verbose")
        self.assertEquals(self.returncode, 0)
        database = fetch_latest_backups(partial=True)["database"]
        self.assertTrue(
            exists_in_backup(backup["includes"]["includes"], database))

        # test 8: take a backup with frappe.conf.backup.excludes
        self.execute(
            "bench --site {site} set-config backup '{excludes}' --parse",
            {"excludes": json.dumps(backup["excludes"])},
        )
        self.execute("bench --site {site} backup --verbose")
        self.assertEquals(self.returncode, 0)
        database = fetch_latest_backups(partial=True)["database"]
        self.assertFalse(
            exists_in_backup(backup["excludes"]["excludes"], database))
        self.assertTrue(
            exists_in_backup(backup["includes"]["includes"], database))

        # test 9: take a backup with --include (with frappe.conf.excludes still set)
        self.execute(
            "bench --site {site} backup --include '{include}'",
            {"include": ",".join(backup["includes"]["includes"])},
        )
        self.assertEquals(self.returncode, 0)
        database = fetch_latest_backups(partial=True)["database"]
        self.assertTrue(
            exists_in_backup(backup["includes"]["includes"], database))

        # test 10: take a backup with --exclude
        self.execute(
            "bench --site {site} backup --exclude '{exclude}'",
            {"exclude": ",".join(backup["excludes"]["excludes"])},
        )
        self.assertEquals(self.returncode, 0)
        database = fetch_latest_backups(partial=True)["database"]
        self.assertFalse(
            exists_in_backup(backup["excludes"]["excludes"], database))

        # test 11: take a backup with --ignore-backup-conf
        self.execute("bench --site {site} backup --ignore-backup-conf")
        self.assertEquals(self.returncode, 0)
        database = fetch_latest_backups()["database"]
        self.assertTrue(
            exists_in_backup(backup["excludes"]["excludes"], database))