Hacked By AnonymousFox
import logging
import os
import shutil
import subprocess
from datetime import datetime
from pwd import getpwnam
try:
from pathlib import Path
except ImportError:
from pathlib2 import Path
logger = logging.getLogger(__name__)
def latest_log():
"""
Retrieve the latest WHM backup log
:return: str
"""
try:
log_timestamps = [int(x.name.replace('.log', '')) for x in
Path.glob(Path('/usr/local/cpanel/logs/cpbackup'), '*.log')]
_latest_log = sorted(log_timestamps, reverse=True)[0]
if datetime.fromtimestamp(_latest_log).strftime('%Y-%m-%d') == datetime.today().strftime('%Y-%m-%d'):
return '/usr/local/cpanel/logs/cpbackup/%s.log' % _latest_log
return None
except Exception as e:
logger.debug('failed to read latest log: %s', e)
return None
def tail_latest_log(lines=11):
"""
Retrieve tailed lines from the latest WHM backup log
:param lines: string - number of lines to tail
:return: list
"""
_latest_log = latest_log()
if _latest_log:
with open(_latest_log, 'r') as f:
read_lines = f.readlines()[-lines:]
return read_lines
else:
return []
def latest_user_archive(user):
"""
Return path of latest WHM backup archive for user
:param user: string - name of user
:return: string
"""
available_archives = list(
Path.glob(Path('/backup/%s/accounts/' % datetime.today().strftime('%Y-%m-%d')), '*.tar.gz'))
for archive in available_archives:
if archive.name == '%s.tar.gz' % user:
return str(archive)
return None
def package_account(username):
"""
Manually archive cPanel accounts
:param username: string - username of account to package
:return: None
"""
logger.debug('checking for null routed paths...')
null_routed = []
null_routed_dirs = []
null_routed_files = []
user_home_directory = Path('/home/{user}/'.format(user=username))
for pattern in ['*', '.*']:
for p in Path.rglob(user_home_directory, pattern):
try:
if Path.is_dir(p) and str(oct(os.stat(str(p)).st_mode)[-3:]) == '000':
null_routed_dirs.append(p)
elif Path.is_file(p) and str(oct(os.stat(str(p)).st_mode)[-3:]) == '000':
null_routed_files.append(p)
except OSError as e:
if e.errno == 40:
logger.debug('unable to stat path: %s', e)
continue
null_routed.extend(null_routed_dirs)
null_routed.extend(null_routed_files)
logger.debug('found null-routed paths: %s', null_routed)
if null_routed: # change permissions to allow pkgacct
logger.info('Applying changes to null-routed paths...')
for null_path in null_routed:
if Path.is_dir(null_path):
os.chown(str(null_path), getpwnam(username)[2], getpwnam(username)[3])
os.chmod(str(null_path), 0o755)
elif Path.is_file(null_path):
os.chown(str(null_path), getpwnam(username)[2], getpwnam(username)[3])
os.chmod(str(null_path), 0o644)
logger.debug('Permissions updated on path: %s (%s)', null_path, str(oct(os.stat(str(null_path)).st_mode)[-3:]))
logger.info('All changes applied')
backup_root = '/backup/{today}/accounts/'.format(today=datetime.today().strftime('%Y-%m-%d'))
# check for existing paths beforehand and purge
for path in list(Path.glob(Path(backup_root), '%s*' % username)):
if Path.is_dir(path):
shutil.rmtree(str(path))
elif Path.is_file(path):
Path.unlink(path)
logger.debug('Removed corrupted item: %s', str(path))
try:
pkg_account = subprocess.Popen('/usr/local/cpanel/bin/pkgacct %s %s' % (username, backup_root),
shell=True, stdout=open('/dev/null', 'w'))
pkg_account.wait()
assert pkg_account.returncode == 0
except AssertionError:
raise Exception('Process exited with non-zero return code')
created_archive = Path('{root}cpmove-{user}.tar.gz'.format(root=backup_root, user=username))
updated_archive = Path('{root}{user}.tar.gz'.format(root=backup_root, user=username))
created_archive.rename(updated_archive)
if null_routed: # revert chages to permissions
logger.info('Reverting changes to null-routed paths...')
for null_path in null_routed:
os.chown(str(null_path), 0, 0) # root user
os.chmod(str(null_path), 0o000)
logger.debug('Permissions updated on path: %s (%s)', null_path, str(oct(os.stat(str(null_path)).st_mode)[-3:]))
logger.info('All changes reverted')
Hacked By AnonymousFox1.0, Coded By AnonymousFox