From 55cf641b4b4925f24660e9d8a255c60ec9d74af3 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Mon, 27 Apr 2015 11:28:20 -0500 Subject: [PATCH] Applying backup_local fixes to v2 --- v2/ansible/module_utils/basic.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/v2/ansible/module_utils/basic.py b/v2/ansible/module_utils/basic.py index b875160bb20..8f9b03f882d 100644 --- a/v2/ansible/module_utils/basic.py +++ b/v2/ansible/module_utils/basic.py @@ -1293,14 +1293,18 @@ class AnsibleModule(object): def backup_local(self, fn): '''make a date-marked backup of the specified file, return True or False on success or failure''' - # backups named basename-YYYY-MM-DD@HH:MM~ - ext = time.strftime("%Y-%m-%d@%H:%M~", time.localtime(time.time())) - backupdest = '%s.%s' % (fn, ext) - try: - shutil.copy2(fn, backupdest) - except shutil.Error, e: - self.fail_json(msg='Could not make backup of %s to %s: %s' % (fn, backupdest, e)) + backupdest = '' + if os.path.exists(fn): + # backups named basename-YYYY-MM-DD@HH:MM:SS~ + ext = time.strftime("%Y-%m-%d@%H:%M:%S~", time.localtime(time.time())) + backupdest = '%s.%s' % (fn, ext) + + try: + shutil.copy2(fn, backupdest) + except (shutil.Error, IOError), e: + self.fail_json(msg='Could not make backup of %s to %s: %s' % (fn, backupdest, e)) + return backupdest def cleanup(self, tmpfile):