diff --git a/frappe/commands.py b/frappe/commands.py index cc2d320904..1b827fa949 100644 --- a/frappe/commands.py +++ b/frappe/commands.py @@ -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, ] diff --git a/frappe/utils/backups.py b/frappe/utils/backups.py index 78065bd640..fd09293c56 100644 --- a/frappe/utils/backups.py +++ b/frappe/utils/backups.py @@ -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):