Created
March 23, 2016 03:26
-
-
Save eternnoir/7f87c55152e5d2bc36d4 to your computer and use it in GitHub Desktop.
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
# -*- coding: utf-8 -*- | |
import commands | |
import datetime | |
import time | |
import os | |
DATABASE_NAME = 'mattermost' | |
BACKUP_FOLDER = '/home/sdduser/backup/backupdata' | |
HISTORY_FOLDER = '/home/sdduser/backup/historydata' | |
MMDATA_FOLDER = '/mattermost' | |
KEEP_FILE_DAYS = 30 | |
dt = datetime.datetime.now().strftime('%Y%m%d') | |
def backup_postgres(): | |
print('Start backup database.') | |
command_str = 'su postgres -c "/usr/bin/pg_dump %s > %s/mattermostdb_%s.bak"' % (DATABASE_NAME, BACKUP_FOLDER, dt) | |
print(command_str) | |
status, result = commands.getstatusoutput(command_str) | |
print(result) | |
def backup_mattermost_data(): | |
print("Satrt backup mattermost data") | |
command_str = 'tar -zcf %s/mattermost_data_%s.tar.gz %s' % (BACKUP_FOLDER, dt, MMDATA_FOLDER) | |
status, result = commands.getstatusoutput(command_str) | |
print(result) | |
def tar_files(): | |
print("Start to tar all backup files.") | |
command_str = 'tar -zcf %s/%s_mattermost_backup.tar.gz %s' % (HISTORY_FOLDER, dt, MMDATA_FOLDER) | |
status, result = commands.getstatusoutput(command_str) | |
print(result) | |
def clear_folder(): | |
print("Satrt clear backup folder.") | |
command_str = 'rm -rf %s/*' % (BACKUP_FOLDER) | |
status, result = commands.getstatusoutput(command_str) | |
print(result) | |
def clear_old_backup(): | |
removed = 0 | |
d = path(DIRECTORY) | |
#Replace DIRECTORY with your required directory | |
time_in_secs = time.time() - (KEEP_FILE_DAYS * 24 * 60 * 60) | |
for i in d.walk(): | |
if i.isfile(): | |
if i.mtime <= time_in_secs: | |
i.remove() | |
removed += 1 | |
print(removed) | |
if __name__ == "__main__": | |
clear_folder() | |
backup_postgres() | |
backup_mattermost_data() | |
tar_file() | |
clear_old_backup |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment