From e7f996c480b3be437794041077b297f3e9ddc1bb Mon Sep 17 00:00:00 2001 From: Hui Lan Date: Sat, 7 Dec 2019 09:58:54 +0800 Subject: create backup_files.py for backing up files Define a function copy_and_backup_file(src_file, dest_dir) to do backup and compression work. The function copy_and_backup_file is used in update_network_by_force.py. -Hui --- Code/backup_files.py | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 Code/backup_files.py (limited to 'Code/backup_files.py') diff --git a/Code/backup_files.py b/Code/backup_files.py new file mode 100644 index 0000000..39a0d30 --- /dev/null +++ b/Code/backup_files.py @@ -0,0 +1,54 @@ +# Usage: use the copy_and_backup_file() as a utility function for backing up a file. +# +# Purpose: the function copy_and_backup_file(src, dest_dir) copies file src to destination directory (if exists) and compress +# the copied file in the destination directory (to save space). +# +# +# Created on 7 December 2019 by Hui Lan (lanhui@zjnu.edu.cn) + +import os, sys +from configure import UPDATE_NETWORK_LOG_FILE +from datetime import datetime + +MINIMUM_SPACE_REQUIREMENT = 1 # Gigabytes + +def write_log_file(s, fname): + if not os.path.exists(fname): + return None + f = open(fname, 'a') + curr_time = datetime.now().strftime('%Y-%m-%d %H:%M') + s = '[' + curr_time + ']: ' + s + if not '\n' in s: + s += '\n' + f.write(s) + f.close() + + +def make_paths(s): + if not os.path.isdir(s): + os.makedirs(s) + + +def disk_has_enough_space(): + available_G = 4 * os.statvfs('/home').f_bavail / (1024*1024) # compute available space (in Gigabytes). Each block has 4k bytes, work for Linux/UNIX systems only + if available_G < MINIMUM_SPACE_REQUIREMENT: + print('[backup_files.py] home directory does not have enough space (only %4.1f G is available) ' % (available_G)) + write_log_file('[backup_files.py] WARNING: home directory does not have enough space (only %4.1f G is available)! No backup is carried out.' % (available_G), UPDATE_NETWORK_LOG_FILE) + sys.exit() + + +def copy_and_backup_file(src_file, dest_dir): + disk_has_enough_space() # make sure we have enough space firs.t + if not os.path.exists(src_file): + sys.exit() + make_paths(dest_dir) # if dest_dir does not exist, create it. + curr_date = datetime.now().strftime('%Y%m%d') + dest_file = os.path.join(dest_dir, os.path.basename(src_file) + '.' + curr_date) + cmd = 'cp %s %s && cd %s && gzip -f %s' % (src_file, dest_file, dest_dir, dest_file) + os.system(cmd) + write_log_file('[backup_files.py] File %s has been backed up to %s and zipped (.gz)' % (src_file, dest_file), UPDATE_NETWORK_LOG_FILE) + + +## main +if __name__ == '__main__': + copy_and_backup_file('../Data/temp/edges.txt', '../Analysis') -- cgit v1.2.1