Created
April 27, 2020 02:27
-
-
Save YUChoe/dde6b3ff3f87c6e85d757426ab7e1812 to your computer and use it in GitHub Desktop.
backup script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import os | |
import time | |
host_name = 'SBC1' | |
bak_dir = '/home/backup/' | |
fn = '{}-{}.tar.xz'.format(host_name, time.strftime("%Y%m%d_%H%M")) | |
def log(s): | |
with open('/var/log/sbc_src_backup.log', 'a') as fp: | |
fp.write('{} {}{}'.format(time.asctime(), s, '\n')) | |
# __main__ | |
os.system("cd /userx/i/vg/; export XZ_DEFAULTS=\"-T 0\"; /usr/bin/tar cJf {} ktp/* --exclude \"xxx/src/lib/linux-3.10.0-1062.el7\"".format(fn)) | |
os.system('mv /userx/i/vg/{} {}'.format(fn, bak_dir)) | |
lst = [] | |
for n in os.listdir(bak_dir): | |
if '.tar.xz' == n[-7:]: lst.append(n) | |
lst.sort() | |
this_file = os.path.join(bak_dir, fn) | |
last_file = os.path.join(bak_dir, lst[-1]) | |
if this_file != last_file and os.path.isfile(last_file) and os.path.getsize(last_file) == os.path.getsize(this_file): | |
log('same size({}) as {}'.format(os.path.getsize(last_file), last_file)) | |
log('deleting {}'.format(this_file)) | |
os.unlink(this_file) | |
else: | |
log('backup complete: {}'.format(this_file)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment