Skip to content

Commit 80dbcec

Browse files
committedAug 21, 2016
Remove DRY code. Restore support compress file
1 parent 9b5aff2 commit 80dbcec

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed
 

‎README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Utility app for database snapshot taking and restoring.
1010

1111
`bench db_snapshot restore snapshot_name`
1212

13-
`bench db_snapshot restore-from-file filename`
13+
`bench db_snapshot restore-from-file sql_file_path`
1414

1515
`bench db_snapshot delete snapshot_name`
1616

‎db_snapshot/snapshot.py

+16-15
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import os, json
44
import frappe
5+
from frappe.installer import extract_sql_gzip, import_db_from_sql
56
from frappe.utils import get_site_path, get_site_base_path, get_backups_path
67
from frappe.utils.backups import new_backup
78

@@ -34,8 +35,8 @@ def delete(self):
3435
self.data = data
3536
save_snapshots_data(data)
3637

37-
def update(self, ddict):
38-
self.data.update(ddict)
38+
def update_path(self, path):
39+
self.data.update({self.name: path})
3940
save_snapshots_data(self.data)
4041

4142
def take(self):
@@ -51,11 +52,13 @@ def take(self):
5152
os.mkdir(snapshot_path)
5253

5354
os.rename(filename, snapshotname)
54-
self.data.update({self.name: snapshotname})
55-
save_snapshots_data(self.data)
55+
self.update_path(snapshotname)
5656

5757
def restore(self):
58-
restore_from_file(self.get())
58+
path = str(self.get())
59+
restore_from_file(path)
60+
if path.endswith('sql.gz'):
61+
self.update_path(path[:-3])
5962

6063

6164
def get_snapshots_path():
@@ -81,15 +84,13 @@ def get_snapshots_data():
8184
return json.load(f)
8285

8386

84-
def restore_from_file(filename):
85-
args = {
86-
"filename": filename,
87-
"user": frappe.conf.db_name,
88-
"password": frappe.conf.db_password,
89-
"db_name": frappe.conf.db_name,
90-
"db_host": frappe.db.host,
91-
}
87+
def restore_from_file(sql_file_path):
88+
frappe.flags.in_install_db = True
89+
90+
if sql_file_path.endswith('sql.gz'):
91+
sql_file_path = extract_sql_gzip(os.path.abspath(sql_file_path))
92+
93+
import_db_from_sql(sql_file_path, True)
9294

93-
cmd_string = """gunzip < %(filename)s | mysql -u %(user)s -p%(password)s %(db_name)s -h %(db_host)s""" % args
94-
frappe.utils.execute_in_shell(cmd_string)
95+
frappe.flags.in_install_db = False
9596
frappe.clear_cache()

0 commit comments

Comments
 (0)
Please sign in to comment.