Skip to content

Instantly share code, notes, and snippets.

@wastee
Created February 6, 2021 06:17
Show Gist options
  • Save wastee/1703a640b833f9161bc7360af89fe44e to your computer and use it in GitHub Desktop.
Save wastee/1703a640b833f9161bc7360af89fe44e to your computer and use it in GitHub Desktop.
import os
import subprocess
import datetime
import tarfile
# 备份路径
NGINX_DIR = '/etc/nginx'
WEB_DIR = '/var/www'
SUPERV_DIR = '/etc/supervisor'
LETS_DIR = '/etc/letsencrypt'
# 时间戳
timest = datetime.datetime.now()
timest_str = timest.strftime('%Y-%m-%d')
oldtimest = datetime.datetime.now() - datetime.timedelta(days=10)
oldtimest_str = oldtimest.strftime('%Y-%m-%d')
# 目标
BACKUPS_DIR = '/var/backups/ladobak/'
if not os.path.exists(BACKUPS_DIR):
os.makedirs(BACKUPS_DIR)
DROPBOX_DIR = '/' + timest_str + '/'
# 备份名字
NGINX_BAK = 'NginxBak_' + timest_str + '.tar.gz'
LADO_BAK = 'WebBak_' + timest_str + '.tar.gz'
SUPERV_BAK = 'SupervisorBak_' + timest_str + '.tar.gz'
LETS_BAK = 'LestBak_' + timest_str + '.tar.gz'
# 旧备份名字
OLD_NGINX_BAK = 'NginxBak_' + oldtimest_str + '.tar.gz'
OLD_LADO_BAK = 'WebBak_' + oldtimest_str + '.tar.gz'
OLD_SUPERV_BAK = 'SupervisorBak_' + oldtimest_str + '.tar.gz'
OLD_LETS_BAK = 'LestBak_' + oldtimest_str + '.tar.gz'
# 方法
def make_targz(output_filename, source_dir):
with tarfile.open(output_filename, "w:gz") as tar:
tar.add(source_dir, arcname=os.path.basename(source_dir))
make_targz(BACKUPS_DIR + NGINX_BAK, NGINX_DIR)
make_targz(BACKUPS_DIR + LADO_BAK, WEB_DIR)
make_targz(BACKUPS_DIR + SUPERV_BAK, SUPERV_DIR)
make_targz(BACKUPS_DIR + LETS_BAK, LETS_DIR)
subprocess.call(['bash', '/root/dropbox_uploader.sh', 'upload',
BACKUPS_DIR + NGINX_BAK, DROPBOX_DIR + NGINX_BAK, ])
subprocess.call(['bash', '/root/dropbox_uploader.sh', 'upload',
BACKUPS_DIR + LADO_BAK, DROPBOX_DIR + LADO_BAK, ])
subprocess.call(['bash', '/root/dropbox_uploader.sh', 'upload',
BACKUPS_DIR + SUPERV_BAK, DROPBOX_DIR + SUPERV_BAK, ])
subprocess.call(['bash', '/root/dropbox_uploader.sh', 'upload',
BACKUPS_DIR + LETS_BAK, DROPBOX_DIR + LETS_BAK, ])
if os.path.isfile(BACKUPS_DIR + OLD_NGINX_BAK):
os.remove(BACKUPS_DIR + OLD_NGINX_BAK)
if os.path.isfile(BACKUPS_DIR + OLD_LADO_BAK):
os.remove(BACKUPS_DIR + OLD_LADO_BAK)
if os.path.isfile(BACKUPS_DIR + OLD_SUPERV_BAK):
os.remove(BACKUPS_DIR + OLD_SUPERV_BAK)
if os.path.isfile(BACKUPS_DIR + OLD_LETS_BAK):
os.remove(BACKUPS_DIR + OLD_LETS_BAK)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment