/home/drscream

  • home
  • blog
  • gallery
  • about
  • Kleines einfaches Backup (Bash-)Script

    Es gibt sicher einiges effektivere und bessere Scripte um ein System und MySQL Backup zu machen. Das ist jetzt auch nur die erste Version des ganzen und evtl. kann es jemand gebrauchen :)

    #####################################################################
    # srv-backup.sh - backup the system and the databases               #
    # @author:  drscream@cyber-tec.org                                  #
    # @version: Thu Oct  4 11:35:39 CEST 2007                           #
    #####################################################################
    
    # CONFIGURATION
    #####################################################################
    # crontab example:
    # 21 4    * * * /opt/av-srv/av-srv-backup.sh mysql
    #  2 3 1,15 * * /opt/av-srv/av-srv-backup.sh system
    #  2 4    * * * /opt/av-srv/av-srv-backup.sh webroot
    #  3 6    * * * /opt/av-srv/av-srv-backup.sh pgsql
    
    ## log information ##################################################
    log_file="/local/backup/backup.log"
    datetime=$(date +%Y%m%d-%H%M%S)
    fqdn=$(hostname -f)
    
    ## select what to backup ############################################
    # => 0/1:          deactivate or activate the backup
    # => /srv/xxx:     path to save the backup
    # => asdf.tar.bz2: file to save the backup
    # => sysdir.txt:   text file with the directories to backup
    # => rsync:        rsync is set the file will rsynced to the backupsrv
    system=(1 '/local/backup/system' 'system.$fqdn.$datetime.tar.gz' './systemdir.txt' 'rsync')
    
    # => 0/1:          deactivate or activate the backup
    # => /srv/xxx:     path to save the backup
    # => asdf.tar.bz2: file to save the backup
    # => /web:         directory to backup
    # => rsync:        rsync is set the file will rsynced to the backupsrv
    webroot=(1 '/local/backup/webroot' 'web.$fqdn.$datetime.tar.gz' '/web' 'rsync')
    
    # => 0/1:          deactivate or activate the backup
    # => /srv/xxx:     path to save the backup
    # => user:         sql user (root)
    # => password:     sql password
    # => rsync:        rsync is set the file will rsynced to the backupsrv
    mysql=(1 '/local/backup/mysql' 'root' 'qwe123' 'rsync')
    
    # => 0/1:          deactivate or activate the backup
    # => /srv/xxx:     path to save the backup
    # => asdf.tar.bz2: file to save the backup
    # => user:         sql user (root)
    # => rsync:        rsync is set the file will rsynced to the backupsrv
    pgsql=(0 '/local/backup/pgsql' 'pgsql.$fqdn.$datetime.sql.gz' 'postgres' 'rsync')
    
    # => 0/1:                       rsync to the specified server
    # => username:      username for rsync
    # => hostname:      specified backup server host
    # => /path:                     path to save the backup ($fqdn is added)
    rsync=(1 'username' 'hostname' '/backup')
    rsync_args="--archive"
    
    ## delete the files after  days #################################
    delete_files=20
    
    ## global variables #################################################
    export LANG="en_US.utf8"
    PARAM=$1
    
    # FUNCTIONS
    #####################################################################
    function delete() {
            find $1 -type f -mtime +$delete_files | xargs rm 2>/dev/null
    }
    
    function log() {
            echo $1 >> $log_file
    }
    
    function backuprsync() {
            if [[ ${rsync[0]} == 1 && ${1} == "rsync" ]]; then
            log "($datetime) rsync to server ---------------------------"
    
                    rsync ${rsync_args} $2 ${rsync[1]}@${rsync[2]}:${rsync[3]}/${fqdn} >> $log_file
    
                    log "end rsync to server -----------------------------------"
            fi
    }
    
    # MAIN
    #####################################################################
    
    ## system ###########################################################
    if [[ ${system[0]} == 1 && ${PARAM} == "system" ]]; then
            # cleanup system backup
            delete ${system[1]}
    
            # start system backup
            log "($datetime) systembackup: ---------------------------------"
            log "backup following files:"
    
            dir_backup=$(cat ${system[3]})
            log "$dir_backup"
    
            # compress the files
            cd /
            tar cvpzf "${system[1]}/${system[2]}" $dir_backup
    
            # rsync if it is set
            backuprsync ${system[4]} ${system[1]}/${system[2]}
    
            log "end systembackup ------------------------------------------"
    fi
    
    ## webroot ##########################################################
    if [[ ${webroot[0]} == 1 && ${PARAM} == "webroot" ]]; then
        # cleanup webroot backup
        delete ${webroot[1]}
    
        # start webroot backup
        log "($datetime) webrootbackup: --------------------------------"
        log "backup following dir:"
    
        dir_backup=$(ls -la ${webroot[3]})
        log "$dir_backup"
    
        # compress the files
        cd ${webroot[3]}
        tar cvpzf "${webroot[1]}/${webroot[2]}" *
    
        # rsync if it is set
            backuprsync ${webroot[4]} ${webroot[1]}/${webroot[2]}
    
        log "end webrootbackup -----------------------------------------"
    fi
    
    ## mysql ############################################################
    if [[ ${mysql[0]} == 1 && ${PARAM} == "mysql" ]]; then
            # cleanup mysql backup
            delete ${mysql[1]}
    
            # start mysql backup
            log "($datetime) mysqlbackup -----------------------------------"
    
            mysqlshow -u ${mysql[2]} --password=${mysql[3]} |  tr -d '|' |  tr -d '+' |  tr -d ' ' | tr -d '-' > /tmp/sql-backup-list.tmp
    
            list=/tmp/sql-backup-list.tmp
            ig=3
            xr_lines=$(wc -l $list | awk '{ print $1 }')
            while [ "$ig" -le $xr_lines ];  do
                    ig=$(expr $ig + 1)
                    item=$(awk "NR == $ig {print}" $list)
    
                    mysqldump --add-drop-table --allow-keywords -q -a -c -u ${mysql[2]} --password=${mysql[3]} $item | gzip > ${mysql[1]}/$item.sql.gz
    
                    echo $item >> $dir_backup
            done
    
            # Remove a temp file
            rm -f /tmp/sql-backup-list.tmp
    
        # rsync if it is set
            backuprsync ${mysql[4]} ${mysql[1]}
    
            log "end mysqlbackup -------------------------------------------"
    fi
    
    ## pgsql ############################################################
    if [[ ${pgsql[0]} == 1 && ${PARAM} == "pgsql" ]]; then
            # cleanup pgsql backup
            delete ${pgsql[1]}
    
            # start pgsql backup
            log "($datetime) pgsqlbackup -----------------------------------"
    
            pg_dumpall -U${pgsql[3]} | gzip > "${pgsql[1]}/${pgsql[2]}.gz"
    
        # rsync if it is set
            backuprsync ${pgsql[4]} ${pgsql[1]}
    
            log "end pgsqlbackup -------------------------------------------"
    fi

    Server Backup Script Download

    Posted

    October 6, 2007

    Tags

    Backup, bash, bsd, linux, sql

  • MySQLDump vorher selektieren

    Das ist zwar gerade das erste mal das ich das brauche aber ich hab auch lange genug dafuer gesucht. Ich wollte einen MySQLDump von einer Tabelle machen, jedoch wollte ich da einige Inhalte nicht in meinem Dump haben. Und so funktioniert das ganze:

    mysqldump -u root -p lxo customer --where="email IS NOT NULL"

    Posted

    July 1, 2007

    Tags

    dump, sql

  • Blob aus MySQL DB auslesen und in eine Datei schreiben

    Nachdem ich mir das einfach nicht merken kann muss ich das nun mal in meinem Blog verewigen.Und so liest man einen Blob aus einer MySQL DB aus:SELECT filedata INTO DUMPFILE '/tmp/test.file' FROM `filedb` WHERE id = 466727

    Posted

    June 28, 2007

    Tags

    blob, dump, sql

  • MySQL, InnoDB und Error 1114

    Beim Import einer 500 MB SQL-Datei in eine MySQL Datenbank ist der folgende Fehler aufgetreten:

    ERROR 1114 (HY000) at line 582: The table 'company' is full

    Das bedeutet, dass die InnoDB Daten-Dateien voll sind und nicht automatisch, bzw. nur bis zu einem bestimmten Wert erweitert werden (autoextend).
    Loesung: In der MySQL-Config-Datei “my.cnf” befindet sich die folgende Einstellung fuer InnoDB Daten-Dateien:

    innodb_data_file_path = ibdata1:10M:autoextend:max:128M

    Es bedeutet, dass eine Daten-Datei 10MB gross ist und die Tabelle / Datenbank bis maximal 128MB erweitert wird. Das kann man aendern in:

    innodb_data_file_path = ibdata1:100M:autoextend

    Jetzt ist eine Daten-Datei 100MB gross und es gibt keine Beschraenkung beim automatischen Erweitern.

    Posted

    June 23, 2007

    Tags

    bsd, ibdata, innodb, linux, macosx, sql

    1 comment

  • MySQL: Root-Passwort zuruecksetzen

    Es kann ja mal passieren das man das root - Passwort fuer den MySQL Server vergisst, es sollte zwar nicht passieren aber es kann ;) Es gibt eine recht einfach moeglichkeit dieses wieder zurueckzusetzten.Man startet den MySQL - Daemon einfach mit –skip-grant-tables. Stellt dann ueber die Shell eine Verbindung zum MySQL - Daemon her:

    shell> mysql -u root

    Nun kann man ganz einfach das Passwort neu vergeben:

    mysql> UPDATE mysql.user SET Password=PASSWORD('NeuesPasswort') WHERE User='root';
    mysql> FLUSH PRIVILEGES;

    Tada und schon hat der root ein neues MySQL Passwort :)

    Posted

    May 23, 2007

    Tags

    bsd, linux, password, sql, windows

  • SQL Problem ‘mysql.host’

    Es gibt manchmal unter manchen Distributionen mit MySQL einige kleinere Probleme z.b.:
    http://paste.frubar.net/997
    http://paste.frubar.net/998

    Loesung ist hierbei einfach die MySQL Rechtetabelle zu installieren und chown Rechte fuer den MySQL Ordner zu setzen:

    mysql_install_db
    chown -R mysql:mysql /var/lib/mysql
    cd /usr ; /usr/bin/mysqld_safe &
    /usr/bin/mysqladmin -u root password 'yournewpass'

    Posted

    April 11, 2006

    Tags

    bash, linux, sql