summaryrefslogtreecommitdiffstats
path: root/abs/core/LinHES-system/lh_system_backup_job
blob: d2c80ab7f73434bde3f2462a143d4cb3f330fb28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/bin/bash
#process that uses this system backup script
# - myth_mtc.py
# - supplemental web, process.py  backup
MYTH_RUN_STATUS="1"
. /etc/profile
. /etc/systemconfig
BACKUPDIR=/data/storage/disk0/backup/system_backups
DELETE_DAYS=21
DATE=`date +%F_%H-%M`
#
function backup(){
    echo "#########################################################"
    echo "Starting  backup	"
    mkdir -p $BACKUPDIR/$DATE

    #backup db
    pacman -Q mysql 2>/dev/null
    if [ $? = 0 ]
    then
        mysqldump mythconverg > $BACKUPDIR/$DATE/mythconverg
        mysqldump ncid > $BACKUPDIR/$DATE/ncid
    fi

    #backup etc
    cp -rp /etc  $BACKUPDIR/$DATE/etc
    cp -rp /var/lib/oss $BACKUPDIR/$DATE/oss

    #backup func keys
    cp -rp /etc/pki $BACKUPDIR/$DATA/pki


    #make_zip file
    cd $BACKUPDIR
    tar -zcvf $BACKUPDIR/backup.$DATE.tgz  $DATE

    if [ -d $BACKUPDIR/$DATE ]
    then
        rm -rf $BACKUPDIR/$DATE
    fi

    if [ -f /home/xymon/server/ext/hbnotes.py ]
    then
        /home/xymon/server/ext/hbnotes.py
        chown nobody:nobody /data/srv/httpd/htdocs/hobbit/notes/* 2> /dev/null >/dev/null
    fi
    echo "#########################################################"
}

function remove_old_backups(){
    #remove old backups
    find $BACKUPDIR/backup*.tgz -type f -mtime +$DELETE_DAYS -delete
}


function remote_backup(){
    #Remote copy
    if [ x$RemoteBackup = x1 ]
    then
        localRemoteCheck=`echo $RemoteBackupDir | cut -d: -f1`
        if [  x$localRemoteCheck =  xdir ]
        then
            localRemotedir=`echo $RemoteBackupDir | cut -d: -f2`
            echo "copying $BACKUPDIR/backup.$DATE.tgz to  $localRemotedir  "
            cp $BACKUPDIR/backup.$DATE.tgz  $localRemotedir
        else
            /usr/bin/func  ${RemoteBackupDir}  ping| grep -q "FAILED"
            rc=$?
            if [ $rc = 0 ]
            then
                #this is here to mark failed copy of the backup.
                #There is a cron.hourly job that will attempt to retransfer the file
                echo "Remote backup failed to ${RemoteBackupDir}"
                echo backup.$DATE.tgz >> $BACKUPDIR/remote_backup_failed.txt
            else
                echo "copying $BACKUPDIR/backup.$DATE.tgz to  ${RemoteBackupDir}:$BACKUPDIR/MBE_$DATE.tgz"
                /usr/bin/func  ${RemoteBackupDir} copyfile  -f  $BACKUPDIR/backup.$DATE.tgz  --remotepath $BACKUPDIR/MBE_$DATE.tgz
            fi
        fi
    fi
}

function remote_transfer(){
    transfer_file=${1}
    echo $transfer_file
    /usr/bin/func  ${RemoteBackupDir}  ping| grep -q "FAILED"
    rc=$?
    if [ $rc = 0 ]
    then
        #this is here to mark failed copy of the backup.
        #There is a cron.hourly job that will attempt to retransfer the file
        echo "Remote backup failed to ${RemoteBackupDir}"
        echo $transfer_file >> $BACKUPDIR/remote_backup_failed.txt
    else
        echo "copying $BACKUPDIR/$transfer_file to  ${RemoteBackupDir}:$BACKUPDIR/MBE_$transfer_file"
        /usr/bin/func  ${RemoteBackupDir} copyfile  -f  $BACKUPDIR/$transfer_file  --remotepath $BACKUPDIR/MBE_$transfer_file
    fi
}

function add_link(){
    if [ -f $BACKUPDIR/remote_backup_failed.txt ]
    then
        RETRYFILE="/etc/cron.hourly/lh_backup_retry.sh"
        echo "#!/bin/bash" > $RETRYFILE
        echo "#This file was autogenerated and removed by lh_system_backup_job" >> $RETRYFILE
        echo "MYTH_RUN_STATUS=1">> $RETRYFILE
        echo ". /etc/profile">> $RETRYFILE
        echo "lh_system_backup_job retry">> $RETRYFILE
        chmod 755 $RETRYFILE
    fi
}

function remove_link(){
    RETRYFILE="/etc/cron.hourly/lh_backup_retry.sh"
    if [ ! -f $BACKUPDIR/remote_backup_failed.txt ]
    then
        rm -f $RETRYFILE
    fi
}
#------------------------------------
if [ "x$1" = "x" ]
then
    remove_old_backups
    backup
    remote_backup
    add_link
else
    #this is where we attempt to transfer again as part of the cronjob
    if [ -f $BACKUPDIR/remote_backup_failed.txt ]
    then
        mv -f $BACKUPDIR/remote_backup_failed.txt /tmp
        while read line
        do
            echo $line
            remote_transfer $line
        done < /tmp/remote_backup_failed.txt
        rm -f /tmp/remote_backup_failed.txt
    fi
    remove_link
fi