A quick note from me: this function is subject to a race condition:
def mount(mount_point, credentials, nas, backup_dir):
"""Checks if the NFS share for the backup
exists (if not it creates it) and mounts it"""
if os.path.exists(mount_point):
if not os.path.ismount(mount_point):
subprocess.check_call(['mount', '-t', 'cifs', '-o', credentials, nas, mount_point])
if not os.path.exists(backup_dir):
os.makedirs(backup_dir)
else:
os.makedirs(mount_point)
subprocess.check_call(['mount', '-t', 'cifs', '-o', credentials, nas, mount_point])
When you do a check of the form:
if not os.path.exists(directory):
os.makedirs(directory)
you’ll get an OSError if the directory pops into existence between your if condition and the makedirs() command. I suggest using something like this mkdir_p() function from Stack Overflow mkdir_p() function from Stack Overflow, which creates the directory and ignores errors if it already exists.
Doing so would also let you cut out the duplicate subprocess calls:
def mount(mount_point, credentials, nas, backup_dir):
mkdir_p(mount_point)
if not os.path.ismount(mount_point):
subprocess.check_call(['mount', '-t', 'cifs', '-o',
credentials, nas, mount_point])
mkdir_p(backup_dir)
A quick note from me: this function is subject to a race condition:
def mount(mount_point, credentials, nas, backup_dir):
"""Checks if the NFS share for the backup
exists (if not it creates it) and mounts it"""
if os.path.exists(mount_point):
if not os.path.ismount(mount_point):
subprocess.check_call(['mount', '-t', 'cifs', '-o', credentials, nas, mount_point])
if not os.path.exists(backup_dir):
os.makedirs(backup_dir)
else:
os.makedirs(mount_point)
subprocess.check_call(['mount', '-t', 'cifs', '-o', credentials, nas, mount_point])
When you do a check of the form:
if not os.path.exists(directory):
os.makedirs(directory)
you’ll get an OSError if the directory pops into existence between your if condition and the makedirs() command. I suggest using something like this mkdir_p() function from Stack Overflow, which creates the directory and ignores errors if it already exists.
Doing so would also let you cut out the duplicate subprocess calls:
def mount(mount_point, credentials, nas, backup_dir):
mkdir_p(mount_point)
if not os.path.ismount(mount_point):
subprocess.check_call(['mount', '-t', 'cifs', '-o',
credentials, nas, mount_point])
mkdir_p(backup_dir)
A quick note from me: this function is subject to a race condition:
def mount(mount_point, credentials, nas, backup_dir):
"""Checks if the NFS share for the backup
exists (if not it creates it) and mounts it"""
if os.path.exists(mount_point):
if not os.path.ismount(mount_point):
subprocess.check_call(['mount', '-t', 'cifs', '-o', credentials, nas, mount_point])
if not os.path.exists(backup_dir):
os.makedirs(backup_dir)
else:
os.makedirs(mount_point)
subprocess.check_call(['mount', '-t', 'cifs', '-o', credentials, nas, mount_point])
When you do a check of the form:
if not os.path.exists(directory):
os.makedirs(directory)
you’ll get an OSError if the directory pops into existence between your if condition and the makedirs() command. I suggest using something like this mkdir_p() function from Stack Overflow, which creates the directory and ignores errors if it already exists.
Doing so would also let you cut out the duplicate subprocess calls:
def mount(mount_point, credentials, nas, backup_dir):
mkdir_p(mount_point)
if not os.path.ismount(mount_point):
subprocess.check_call(['mount', '-t', 'cifs', '-o',
credentials, nas, mount_point])
mkdir_p(backup_dir)
A quick note from me: this function is subject to a race condition:
def mount(mount_point, credentials, nas, backup_dir):
"""Checks if the NFS share for the backup
exists (if not it creates it) and mounts it"""
if os.path.exists(mount_point):
if not os.path.ismount(mount_point):
subprocess.check_call(['mount', '-t', 'cifs', '-o', credentials, nas, mount_point])
if not os.path.exists(backup_dir):
os.makedirs(backup_dir)
else:
os.makedirs(mount_point)
subprocess.check_call(['mount', '-t', 'cifs', '-o', credentials, nas, mount_point])
When you do a check of the form:
if not os.path.exists(directory):
os.makedirs(directory)
you’ll get an OSError if the directory pops into existence between your if condition and the makedirs() command. I suggest using something like this mkdir_p() function from Stack Overflow, which creates the directory and ignores errors if it already exists.
Doing so would also let you cut out the duplicate subprocess calls:
def mount(mount_point, credentials, nas, backup_dir):
mkdir_p(mount_point)
if not os.path.ismount(mount_point):
subprocess.check_call(['mount', '-t', 'cifs', '-o',
credentials, nas, mount_point])
mkdir_p(backup_dir)