def setup_gdrive(): credentials = get_credentials() display_msg('\nPlease wait till the Gdrive setup is complete..', 'bold') from apiclient.discovery import build from apiclient.http import MediaFileUpload drive = build('drive', 'v3', credentials=credentials) file_id = create_backup_folder(drive) if file_id is not None: file_metadata = { 'name': 'backup.tar.gz', 'parents': [file_id], 'mimeType': 'application/gzip' } status = execute_command( "echo 'This text file is the initial backup' > backup.txt && " "tar -czvf backup.tar.gz backup.txt" ) if status.returncode == 0: media = MediaFileUpload('backup.tar.gz') resp = drive.files().create(body=file_metadata, media_body=media, fields='id').execute() backup_options['backup_file_id'] = resp.get('id') execute_command('rm backup.txt backup.tar.gz') display_msg('Google Drive configured successfully', 'options') else: error_and_exit('\nInitial file creation failed') else: error_and_exit('\nAn Error occured while creating folder on Gdrive')
def setup_cron(): from crontab import CronTab try: username = execute_command("whoami").stdout.strip() username = str(username, 'utf-8') cron = CronTab(user=username) except Exception as e: error_and_exit('\n{0}\n' 'An Error occured while scheduling backup task'.format(e)) script_command = 'python3 /opt/ghost-backup/backup.py > /opt/ghost-backup/backup.log 2>&1' jobs = cron.find_command(script_command) for job in jobs: if job.command == script_command: backup_options['cron_written'] = True break if not backup_options.get('cron_written', False): job = cron.new( command=script_command, comment='Ghost blog daily backup' ) job.hour.on(0) job.minute.on(0) cron.write() backup_options['cron_written'] = True
def pack_files(): compress_command = 'tar -C {0} -cvzf {1}-{2}.tar.gz {2}.sql'.format( config['dump_path'], config['app_name'], config['timestamp']) if config['images']: compress_command += ' images' if config['themes']: compress_command += ' themes' status = execute_command(compress_command) if status.returncode != 0: print("state code here %s " % status.returncode) error_and_exit( '\nError while packing backup files\n\n{0}'.format( format_subprocess_error(status)), config.get('telegram_user_id'))
def dump_db(): if config['images'] and config['themes']: dump_path = config['images_dir'] if config['images'] else config[ 'themes_dir'] config['dump_path'] = os.path.normpath(dump_path + '/..') else: config['dump_path'] = os.getcwd() config['dump_file'] = config['dump_path'] + '/{0}.sql'.format( config['timestamp']) dump_command = ("mysqldump -h{mysql_hostname} -u'{mysql_username}' " "-p'{mysql_password}' {mysql_db_name} > {0}".format( config['dump_file'], **config)) status = execute_command(dump_command) if status.returncode != 0: print("state code here %s " % status.returncode) error_and_exit( '\nError while taking DB dump\n\n{0}'.format( format_subprocess_error(status)), config.get('telegram_user_id'))
def copy_files(): if not os.path.isdir('/opt/ghost-backup'): os.makedirs('/opt/ghost-backup') execute_command('cp backup.py misc.py .niceneeded.json /opt/ghost-backup')
def delete_backups(): execute_command('rm {0} {1}.tar.gz'.format(config['dump_file'], config['timestamp']))