Created
December 5, 2015 09:40
-
-
Save holly/1cf8d87001ac2f4a1873 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
#!/usr/bin/env python3 | |
# vim:fileencoding=utf-8 | |
""" [NAME] script or package easy description | |
[DESCRIPTION] script or package description | |
""" | |
from datetime import datetime | |
from argparse import ArgumentParser | |
import pprint | |
import time | |
import warnings | |
import os, sys, io | |
import os.path | |
import signal | |
import datetime | |
import subprocess | |
__author__ = 'holly' | |
__version__ = '1.0' | |
PG_DUMPALL = '/usr/bin/pg_dumpall' | |
DBUSER = 'postgres' | |
LOTATE_NUM = 30 | |
DESCRIPTION = 'this is description' | |
parser = ArgumentParser(description=DESCRIPTION) | |
parser.add_argument('-d', '--datadir', action='store', default=os.path.join(os.environ["HOME"], "backups"), help="backup file output directory") | |
parser.add_argument('-c', '--compress', action='store_true', help="enable gzip compress") | |
parser.add_argument('--version', action='version', version='%(prog)s ' + __version__) | |
args = parser.parse_args() | |
def main(): | |
""" [FUNCTIONS] method or functon description | |
""" | |
today = datetime.date.today().strftime("%Y%m%d") | |
dump = os.path.join(args.datadir, "pg_dumpall.sql.{0}".format(today)) | |
cmd = "{0} -U {1}".format(PG_DUMPALL, DBUSER) | |
if args.compress: | |
cmd += " | gzip -c" | |
dump += ".gz" | |
cmd += " >" + dump | |
subprocess.check_call(cmd, shell=True) | |
dumps = sorted(os.listdir(args.datadir), key=lambda x: os.path.getmtime(os.path.join(args.datadir,x))) | |
while len(dumps) > LOTATE_NUM: | |
path = os.path.join(args.datadir, dumps[0]) | |
os.remove(path) | |
dumps.pop(0) | |
sys.exit(0) | |
if __name__ == "__main__": | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment