Пример #1
0
def do_backup():
    """do the backup using rsync"""
    count = 0
    for index in SOURCES.keys():
        src = SOURCES[index]

        if DRY_RUN != "":
            print "Backing up %s" % src
        #rsync -av $DRY_RUN $RSYNC_OPTS "$HOST:$SRC" $DATEDIR
        if DRY_RUN != "":
            print "CALL rsync -av %s %s \"%s:%s\" %s" % (
                DRY_RUN, RSYNC_OPTS, HOST_WIFI, src, DATEDIR)
        status, out = unix("rsync -av %s %s \"%s:%s\" %s" %
                           (DRY_RUN, RSYNC_OPTS, HOST_WIFI, src, DATEDIR))

        log("do_backup - status = [%d] %s" % (status, out))
        if status == 0:
            # record that a backup has been performed today
            count = count + 1
        elif status == 24 or status == 6144 or out.find(
                "some files vanished") > -1:
            # ignore files vanished warning
            # record that a backup has been performed today
            count = count + 1
        else:
            log("Backup of %s was not successful" % src)

    return count == len(SOURCES)
Пример #2
0
def host_is_up(host_wifi):
    """is the subject of this backup available"""
    ret = True
    status, _out = unix("ping -q -c1 -W5 %s >/dev/null 2>&1" % host_wifi)
    if status != 0:
        log("Backup subject is Off Line at present.")
        ret = False
    return ret
Пример #3
0
def backup_already_running(pid):
    """is the backup already running"""
    ret = False
    pidof = "ps -eo pid,command |awk '{$1=$1};1' | grep 'python /usr/local/bin/myowncrashplan' "
    pidof += "| grep -v grep | cut -d' ' -f1 | grep -v %d" % pid
    _status, out = unix(pidof)
    if out != "":
        log("Backup already running. [pid %s]" % (out))
        ret = True
    return ret
Пример #4
0
def we_already_ran_backup_today(datefile, force):
    """have we run the backup today"""
    ret = False
    budate = unix("cat %s" % datefile)[1]
    today = time.strftime("%d-%m-%Y")
    if budate == today:
        log("Backup performed today already.")
        ret = True
    if force:
        log("Backup performed today already, but doing another one anyway.")
        ret = False
    return ret
Пример #5
0
def install():
    """install steps"""

    print "\nThis is the installer for myowncrashplan (version 0.2).\n"

    # 1. ask installer for location of backup dir
    print "Where should we store the backups ? A new folder will be added to the "
    print "specified location. Remember to consider how much space is required to "
    print "backup the target machine. Full path is required."
    finished = False
    while not finished:
        tgtdir = raw_input("[/opt]: ")
        if tgtdir == "":
            tgtdir = "/opt"
        #print "[%s]" % tgtdir
        if not os.path.exists(tgtdir):
            print "INFO: Folder %s does not exist." % tgtdir
            ask = raw_input("Do you want to create this folder. [y/n]: ")
            if ask == 'y' or ask == 'Y':
                finished = True
        else:
            finished = True
    if tgtdir.endswith('/'):
        tgtdir = tgtdir[:-1]
    if not DRYRUN:
        ret, out = unix("mkdir -p %s" % os.path.join(tgtdir, 'myowncrashplan'))

    # 2 ask for ipaddress of target machine
    finished = False
    while not finished:
        ipaddress = raw_input(
            "\nEnter IP Address of target machine. It must be online. [10.0.0.1]: "
        )
        if ipaddress == "":
            ipaddress = "10.0.0.1"
        ret, out = unix('ping -c 1 -W 3 -q %s > /dev/null' % ipaddress)
        #print s,o
        if ret == 0:
            finished = True
        else:
            print "ERROR: ping test failed. Try again."

    # 3 ask for absolute folders on target to be backed up
    finished = False
    src = []
    print "\nEnter locations on target machine to be backed up. Enter empty string to end."
    print "Use absolute paths."
    while not finished:
        path = raw_input("\nEnter a location: ")
        if path != "":
            if path.endswith('/'):
                path = path[:-1]
            src.append(path)
        else:
            finished = True

    count = 1
    formatted_src = ""
    for idx in src:
        formatted_src += "%d: %s\n" % (count, idx)
        count += 1
    #formatted_src = '"'+'"\n         "'.join(src)+'"\n         '

    print "\nWe need to install files in /usr/local/bin /etc/logrotate.d so we "
    print "need to get superuser access.\n"
    ans = ""
    while ans not in ['y', 'n', 'Y', 'N']:
        ans = raw_input("Do you have sudo configured? (Yes or No) [Yn] : ")

    if ans.lower() == 'y':
        sudo_cmd = 'sudo'
    else:
        sudo_cmd = 'su -c'

    # 4. move main script to /usr/local/bin/
    script_bin = "/usr/local/bin"
    #script_bin = "/home/judge/bin"
    if os.path.exists(script_bin):
        if not DRYRUN:
            ret, out = unix('%s cp -f myowncrashplan.py %s' %
                            (sudo_cmd, script_bin))
            # set permissions of file
            if ret != 0:
                print out
                print "ERROR: Could not copy file to %s" % script_bin
                sys.exit()
    else:
        print "ERROR: %s does not exist" % script_bin
        sys.exit()

    # 5. create ~/.myowncrashplanrc)
    rcfile = """
# .myowncrashplan.ini

[myocp]
# HOST_WIFI is the IP Address of the machine to be backed up
host_wifi: %s

# BACKUPDIR is the absolute location on the server to store the backups
backupdir: %s/myowncrashplan

# SOURCES is a list of locations on the laptop to be backed up
# should be absolute paths and each must NOT end in /
[sources]
%s

""" % (ipaddress, tgtdir, formatted_src)

    print "Created %s/.myowncrashplan.ini" % os.environ['HOME']
    if DRYRUN:
        print "-----------------------------------------\n%s-----------------------------------------" % rcfile
    else:
        open(os.path.join(os.environ['HOME'], '.myowncrashplan.ini'),
             'w').write(rcfile)

    # 6. install /etc/logrotate.d/myowncrashplan
    logrot = """
%s/myowncrashplan/myowncrashplan.log {
            size 50M
            copytruncate
            missingok
            rotate 8
            compress
            delaycompress
            notifempty
            create 640 %s %s
}
""" % (tgtdir, os.environ['USER'], os.environ['USER'])

    print "Created /etc/logrotate.d/myowncrashplan"
    if DRYRUN:
        print "-----------------------------------------\n%s-----------------------------------------" % logrot
    else:
        open('/tmp/myowncrashplan', 'w').write(logrot)
        if os.path.exists("/etc/logrotate.d"):
            if not DRYRUN:
                unix(
                    "%s cp /tmp/myowncrashplan /etc/logrotate.d/myowncrashplan"
                    % (sudo_cmd))
        else:
            print "ERROR: /etc/logrotate.d does not exist. May be logrotate has not been installed."
            print "       The output log file will not be rotated and will continue to grow."

    # 7. create rsync_excl
    excl = """
lost+found
.AppleDB
.AppleDesktop
.AppleDouble
Network Trash Folder
Temporary Items
:2e*
.DS_Store
._*
.Trashes
.fseventsd
.bzvol
Saved Application State
Library/Caches/Google/Chrome
"""

    print "Created %s" % (os.path.join(tgtdir, 'myowncrashplan', 'rsync_excl'))
    if DRYRUN:
        print "-----------------------------------------\n%s-----------------------------------------" % excl
    else:
        open(os.path.join(tgtdir, 'myowncrashplan', 'rsync_excl'),
             'w').write(excl)

    # 8. install cron job
    #print "[still need to create cron job]\n"

    cronjob = "*/60 * * * * %s/myowncrashplan.py" % script_bin
    ret, out = unix("crontab -l 2>/dev/null")
    if ret == 0:
        if out.find('myowncrashplan.py') != -1 or out.find(
                'myowncrashplan.sh') != -1:
            print "\nWARNING: Looks like a previous myowncrashplan cronjob exists."
            print "         Use crontab -e to remove or edit the job.\n"
        if out.find(cronjob) == -1:
            if not DRYRUN:
                ret, out = unix(
                    '(crontab -l 2>/dev/null; echo "%s") | crontab -' %
                    cronjob)
                if ret == 0:
                    print "Created a cron job to run the backup."
                else:
                    print "ERROR: Problems creating cron job."
            else:
                print "Adding new job to crontab file."
                print cronjob
Пример #6
0
while not FINISHED:
    TGTDIR = raw_input("[/opt]: ")
    if TGTDIR == "":
        TGTDIR = "/opt"
    #print "[%s]" % TGTDIR
    if not os.path.exists(TGTDIR):
        print "INFO: Folder %s does not exist." % TGTDIR
        ask = raw_input("Do you want to create this folder. [y/n]: ")
        if ask == 'y' or ask == 'Y':
            FINISHED = True
    else:
        FINISHED = True
if TGTDIR.endswith('/'):
    TGTDIR = TGTDIR[:-1]
if not DRYRUN:
    s, o = unix("mkdir -p %s" % os.path.join(TGTDIR, 'myowncrashplan'))

# 2 ask for IPADDRESS of target machine
FINISHED = False
while not FINISHED:
    IPADDRESS = raw_input(
        "\nEnter IP Address of target machine. It must be online. [10.0.0.1]: "
    )
    if IPADDRESS == "":
        IPADDRESS = "10.0.0.1"
    s, o = unix('ping -c 1 -W 3 -q %s > /dev/null' % IPADDRESS)
    #print s,o
    if s == 0:
        FINISHED = True
    else:
        print "ERROR: ping test failed. Try again."