Zum Inhalt

Backup-Script für Daten und Dateien#

Folgendes Backup Script dient als Beispiel wie eine einfache Datensicherung eingerichtet werden kann. Es ersetzt keine professionelle Lösung!

Auf eigene Gefahr

Dieses Script löscht Dateien! Bitte auf eigene Gefahr anwenden! synetics übernimmt weder Garantie noch Support und schließt eine Haftung für Folgeschäden aus.

Backupscript

  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
144
145
146
147
148
149
150
151
152
#!/bin/bash

#### System requirements
#### The following tools need to be installed:
###
### mysql - for the MySQL database backup
### tar - for the file backup
### ncftp - for the FTP file copy
### samba-common - For the CIFS file copy
### cifs-utils - For the CIFS file copy
###
### Debian install example:
### apt-get install samba-common cifs-utils ncftp mysql-client tar

## Global configuration options
glob_machine="idoitserver"                  #Machine name to label backup files
2015-12-12_1755)
glob_path="/tmp/backup/${glob_machine}"     #Specify local backup path
glob_delete=0                               #Delete local files immediately after CIFS or FTP copy?

## Database backup configuration
backup_database=1                           #Database backup enabled?
db_user=""                                  #MySQL username
db_pass=""                                  #MySQL password
db_host="127.0.0.1"                         #MySQL host
db_names="idoit_system idoit_data"          #Database names to backup separated with space

## File backup configuration
backup_file=1                               #File backup enabled?
file_files="/var/www"                       #File paths to backup separated with space

## FTP configuration
copy_ftp=0                                  #Copy files to FTP server?
ftp_user=""                                 #FTP username
ftp_pass=""                                 #FTP password
ftp_host=""                                 #FTP servername or IP address
ftp_path="/backup/path"                     #Backup path on FTP server

## CIFS configuration
copy_cifs=0                                 #Copy files to CIFS Share?
cifs_user=""                                #CIFS username
cifs_pass=""                                #CIFS pass
cifs_path="//cifs/file/share/path"          #CIFS backup path
cifs_mount="/tmp/backup/cifs_mnt"           #CIFS share mount point
cifs_domain=""                              #CIFS domain name

## Old files deletion
delete_file=0                               #Delete old local backups?
delete_file_age=7                           #Delete local backups older than x days
delete_cifs=0                               #Delete old cifs backups?
delete_cifs_age=7                           #Delete cifs backups older than x days

#-------------------------- Stop editing here --------------------------
mkdir -p ${glob_path}
umask 177
#Mount CIFS share
if [ $copy_cifs -eq 1 ];
then
    echo "Mounting CIFS share ${cifs_path} to ${cifs_mount}"
    mkdir -p ${cifs_mount}
{cifs_pass}
fi
#Database backup
if [ $backup_database -eq 1 ];
then
    for i in ${db_names}
    do
        if [ -n "$i" ];
        then
            tmp_sqlfile=${glob_path}/${glob_machine}__${i}__${glob_date}.sql
            echo "Dumping MYSQL database ${i} to file ${tmp_sqlfile}"
{tmp_sqlfile}

            if [ $copy_ftp -eq 1 ];
            then
                echo "Copying ${tmp_sqlfile} to FTP server ${ftp_host}"
                /usr/bin/ncftpput -u ${ftp_user} -p ${ftp_pass} ${ftp_host} ${ftp_path} ${tmp_sqlfile}
            fi

            if [ $copy_cifs -eq 1 ];
            then
                echo "Copying ${tmp_sqlfile} to CIFS share ${cifs_path}"
                cp -f ${tmp_sqlfile} ${cifs_mount}
            fi

            if [ $glob_delete -eq 1 ];
            then
                echo "Immediately deleting file ${tmp_sqlfile}"
                rm -f ${tmp_sqlfile}
            fi
        fi
    done
fi
#File backup
if [ $backup_file -eq 1 ];
then
    j=1
    for i in ${file_files}
    do
        if [ -n "$i" ];
        then
            tmp_filesfile=${glob_path}/${glob_machine}__${j}__${glob_date}.tar.gz
            echo "Packing files ${i} to ${tmp_filesfile}"
            tar czf ${tmp_filesfile} $i
            j=$((j + 1))

            if [ $copy_ftp -eq 1 ];
            then
                echo "Copying ${tmp_filesfile} to FTp server ${ftp_host}"
{tmp_filesfile}
            fi

            if [ $copy_cifs -eq 1 ];
            then
                echo "Copying ${tmp_filesfile} to CIFS share ${cifs_path}"
                cp -f ${tmp_filesfile} ${cifs_mount}
            fi

            if [ $glob_delete -eq 1 ];
            then
                echo "Immediately deleting file ${tmp_filesfile}"
                rm -f ${tmp_filesfile}
            fi

        fi
    done
fi
#old backup deletion
if [ $delete_file -eq 1 ];
then
    echo "Deleting files older than ${delete_file_age} days in ${glob_path}"
    find ${glob_path} -maxdepth 1 -mtime +${delete_file_age} -exec rm {} \;
fi
if [ $copy_cifs -eq 1 ];
then
    if [ $delete_cifs -eq 1 ];
    then
        echo "Deleting files older than ${delete_cifs_age} days on CIFS share ${cifs_mount}"
        find ${cifs_mount} -maxdepth 1 -mtime +${delete_cifs_age} -exec rm {} \;
    fi
fi
#Not implemented so far
#if [ $copy_ftp -eq 1 ];
#then
#
#fi
#Umount CIFS share
if [ $copy_cifs -eq 1 ];
then
    echo "umounting CIFS share ${cifs_mount}"
    umount ${cifs_mount}
fi