add cli backup

This commit is contained in:
Pratik Vyas 2015-03-04 10:53:52 +05:30
parent 2530ee8bd2
commit d86eb386e5
2 changed files with 30 additions and 7 deletions

View file

@ -684,6 +684,24 @@ def use(site, sites_path='.'):
with open(os.path.join(sites_path, "currentsite.txt"), "w") as sitefile:
sitefile.write(site)
@click.command('backup')
@click.option('--with-files', default=False, is_flag=True, help="Take backup with files")
@pass_context
def backup(context, with_files=False, backup_path_db=None, backup_path_files=None, quiet=False):
"Backup"
from frappe.utils.backups import scheduled_backup
verbose = context.verbose
for site in context.sites:
frappe.init(site=site)
frappe.connect()
odb = scheduled_backup(ignore_files=not with_files, backup_path_db=backup_path_db, backup_path_files=backup_path_files, force=True)
if verbose:
from frappe.utils import now
print "database backup taken -", odb.backup_path_db, "- on", now()
if with_files:
print "files backup taken -", odb.backup_path_files, "- on", now()
frappe.destroy()
# commands = [
# new_site,
# restore,
@ -735,5 +753,6 @@ commands = [
dump_queue_status,
console,
make_app,
use
_use,
backup,
]

View file

@ -30,14 +30,18 @@ class BackupGenerator:
self.backup_path_files = backup_path_files
self.backup_path_db = backup_path_db
def get_backup(self, older_than=24, ignore_files=False):
def get_backup(self, older_than=24, ignore_files=False, force=False):
"""
Takes a new dump if existing file is old
and sends the link to the file as email
"""
#Check if file exists and is less than a day old
#If not Take Dump
last_db, last_file = self.get_recent_backup(older_than)
if not force:
last_db, last_file = self.get_recent_backup(older_than)
else:
last_db, last_file = False, False
if not (self.backup_path_files and self.backup_path_db):
self.set_backup_file_name()
if not (last_db and last_file):
@ -135,19 +139,19 @@ def get_backup():
recipient_list = odb.send_email()
frappe.msgprint(_("Download link for your backup will be emailed on the following email address: {0}").format(', '.join(recipient_list)))
def scheduled_backup(older_than=6, ignore_files=False, backup_path_db=None, backup_path_files=None):
def scheduled_backup(older_than=6, ignore_files=False, backup_path_db=None, backup_path_files=None, force=False):
"""this function is called from scheduler
deletes backups older than 7 days
takes backup"""
odb = new_backup(older_than, ignore_files, backup_path_db=backup_path_db, backup_path_files=backup_path_files)
odb = new_backup(older_than, ignore_files, backup_path_db=backup_path_db, backup_path_files=backup_path_files, force=force)
return odb
def new_backup(older_than=6, ignore_files=False, backup_path_db=None, backup_path_files=None):
def new_backup(older_than=6, ignore_files=False, backup_path_db=None, backup_path_files=None, force=False):
delete_temp_backups(older_than=168)
odb = BackupGenerator(frappe.conf.db_name, frappe.conf.db_name,\
frappe.conf.db_password,
backup_path_db=backup_path_db, backup_path_files=backup_path_files, db_host = frappe.db.host)
odb.get_backup(older_than, ignore_files)
odb.get_backup(older_than, ignore_files, force=force)
return odb
def delete_temp_backups(older_than=24):