skripting-section:bash:server-backup-skript

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
skripting-section:bash:server-backup-skript [2017/09/04 13:44] – ↷ Page moved from skripting-section:server-backup-skript to skripting-section:bash:server-backup-skript michaelskripting-section:bash:server-backup-skript [2019/04/04 16:18] (current) michael
Line 1: Line 1:
-====== Server Backup Skript ======+====== Automatisches - Server Backup Skript ======
 Das folgende Skript, wurde erstellt, um wichtige Dateien eines Applikationsservers in regelmässigen Abständen automatisiert zu sichern. Zur Installation, wird das Skript immer jeweils auf das zu sichernde System kopiert und dort wie unten beschrieben eingerichtet. Das folgende Skript, wurde erstellt, um wichtige Dateien eines Applikationsservers in regelmässigen Abständen automatisiert zu sichern. Zur Installation, wird das Skript immer jeweils auf das zu sichernde System kopiert und dort wie unten beschrieben eingerichtet.
  
Line 6: Line 6:
 Vor der Installation, muss sichergestellt werden, dass bereits ein funktionierender NFS-Server (Zur Speicherung des Backups) zur Verfügung steht. Die Server Adresse und die NFS-Freigabe muss anschliessend im Skript angepasst werden. Vor der Installation, muss sichergestellt werden, dass bereits ein funktionierender NFS-Server (Zur Speicherung des Backups) zur Verfügung steht. Die Server Adresse und die NFS-Freigabe muss anschliessend im Skript angepasst werden.
  
-<WRAP center round box 100%>+<WRAP center box 100%>
 <code> <code>
 # vim /root/server_backup_script.sh # vim /root/server_backup_script.sh
 </code> </code>
  
-<wrap em>[PASTE THE SCRIPT]</wrap>+''<wrap em>[PASTE THE SCRIPT SOURCECODE]</wrap>''
 </WRAP> </WRAP>
  
 Nun wird das Backupskript zur Ausführung berechtigt und anschliessend im **crontab** der zu sichernden Maschine eingetragen. In meinem Bespiel, wird hier immer **täglich** um **12:00** das Skript automatisiert ausgeführt. Nun wird das Backupskript zur Ausführung berechtigt und anschliessend im **crontab** der zu sichernden Maschine eingetragen. In meinem Bespiel, wird hier immer **täglich** um **12:00** das Skript automatisiert ausgeführt.
  
-<WRAP center round box 100%>+<WRAP center box 100%>
 <code> <code>
 # chmod +x /root/server_backup_script.sh # chmod +x /root/server_backup_script.sh
Line 22: Line 22:
 # vim /etc/crontab  # vim /etc/crontab
 </code> </code>
-<wrap em> +''<wrap em>0 12 * * * root /root/server_backup_script.sh</wrap>''
-0 12 * * * root /root/server_backup_script.sh</wrap>+
  
 </WRAP> </WRAP>
Line 36: Line 35:
 #!/bin/bash #!/bin/bash
 ################################################################################################ ################################################################################################
-#******************* Backup Linux Server Script by Michael Reber - v 1.**********************#+#******************* Backup Linux Server Script by Michael Reber - v 1.**********************#
 ################################################################################################ ################################################################################################
  
 ################################################################################################ ################################################################################################
 +
 ## Variable Definition & System Vorbereitungen: ## Variable Definition & System Vorbereitungen:
 backupdir=`hostname` backupdir=`hostname`
Line 47: Line 47:
 ## System dependency Check: ## System dependency Check:
 if [ -n "$(command -v apt-get)" ]; then if [ -n "$(command -v apt-get)" ]; then
- if [ $(dpkg-query -W -f='${Status}' nfs-common 2>/dev/null | grep -c "ok installed") -eq 0 ]; then +        if [ $(dpkg-query -W -f='${Status}' nfs-common 2>/dev/null | grep -c "ok installed") -eq 0 ]; then 
-   apt-get install nfs-common -y; +          apt-get install nfs-common -y; 
- fi+        fi
 else else
- if [ $(yum -q list installed nfs-utils &>/dev/null && echo "1" || echo "0") -eq 0 ]; then +        if [ $(yum -q list installed nfs-utils &>/dev/null && echo "1" || echo "0") -eq 0 ]; then 
-   yum install nfs-utils -y; +          yum install nfs-utils -y; 
-   systemctl start rpcbind && systemctl enable rpcbind; +          systemctl start rpcbind && systemctl enable rpcbind; 
- fi+        fi
 fi fi
 if [ ! -d "/mnt/server-backup" ]; then if [ ! -d "/mnt/server-backup" ]; then
- mkdir /mnt/server-backup+        mkdir /mnt/server-backup
 fi fi
 ################################################################################################ ################################################################################################
Line 64: Line 64:
  
 if [ ! -d "/mnt/server-backup/$backupdir" ]; then if [ ! -d "/mnt/server-backup/$backupdir" ]; then
- mkdir /mnt/server-backup/$backupdir +        mkdir /mnt/server-backup/$backupdir 
- echo "$today - Creating new backupdir: $backupdir for this server.." >> /var/log/blackSERV-backup.log+        echo "$today - Creating new backupdir: $backupdir for this server.." >> /var/log/blackSERV-backup.log
 fi fi
 ################################################################################################ ################################################################################################
 ## Start des System-Backups: ## Start des System-Backups:
 echo "$today - Starting with backup for server: $backupdir .." >> /var/log/blackSERV-backup.log echo "$today - Starting with backup for server: $backupdir .." >> /var/log/blackSERV-backup.log
- 
 mkdir -p /mnt/server-backup/$backupdir/$today/etc mkdir -p /mnt/server-backup/$backupdir/$today/etc
-cp /etc/fstab /mnt/server-backup/$backupdir/$today/etc/ +rsync -rlptDv --chmod=Du+rwx \ 
-cp /etc/crontab /mnt/server-backup/$backupdir/$today/etc/+/etc/fstab /etc/crontab \ 
 +/mnt/server-backup/$backupdir/$today/etc/
 cat ~/.bash_history | grep -v '#' >> /mnt/server-backup/$backupdir/$today/history.txt cat ~/.bash_history | grep -v '#' >> /mnt/server-backup/$backupdir/$today/history.txt
  
-# Backup Webserver Content..+# Backup Webserver Files & content:
 if [ -d "/var/www" ]; then if [ -d "/var/www" ]; then
- mkdir /mnt/server-backup/$backupdir/$today/webfiles +        mkdir /mnt/server-backup/$backupdir/$today/webfiles 
- cp -/var/www//mnt/server-backup/$backupdir/$today/webfiles/+        rsync -rlptDv --chmod=Du+rwx \ 
 +        /var/www/ 
 +        /mnt/server-backup/$backupdir/$today/webfiles/
 fi fi
  
-# Backup Webserver Configuration..+# Backup Webserver Configuration:
 if [ -d "/etc/apache2" ]; then if [ -d "/etc/apache2" ]; then
- mkdir /mnt/server-backup/$backupdir/$today/etc/apache2 +        mkdir /mnt/server-backup/$backupdir/$today/etc/apache2 
- cp -/etc/apache2//mnt/server-backup/$backupdir/$today/etc/apache2/+        rsync -rlptDv --chmod=Du+rwx \ 
 +        /etc/apache2/ 
 +        /mnt/server-backup/$backupdir/$today/etc/apache2/
 fi fi
 if [ -d "/etc/httpd" ]; then if [ -d "/etc/httpd" ]; then
- mkdir /mnt/server-backup/$backupdir/$today/etc/httpd +        mkdir /mnt/server-backup/$backupdir/$today/etc/httpd 
- cp -/etc/httpd/conf /etc/httpd/conf.d /etc/httpd/conf.modules.d /mnt/server-backup/$backupdir/$today/etc/httpd/+        rsync -rlptDv --chmod=Du+rwx \ 
 +        /etc/httpd/ 
 +        /mnt/server-backup/$backupdir/$today/etc/httpd/
 fi fi
  
 +# Backup PLEX spezific Files:
 +#https://support.plex.tv/articles/201154527-move-viewstate-ratings-from-one-install-to-another/
 +if [ -d "/var/lib/plexmediaserver" ]; then
 +        mkdir /mnt/server-backup/$backupdir/$today/PLEX_server_backup
 +        cp -a  "/var/lib/plexmediaserver/Library/Application Support/Plex Media Server/Plug-in Support/Databases/com.plexapp.plugins.library.db" "/tmp/com.plexapp.plugins.library.db"
 +        echo ".dump metadata_item_settings" | sqlite3 /tmp/com.plexapp.plugins.library.db | grep -v TABLE | grep -v INDEX > /mnt/server-backup/$backupdir/$today/PLEX_server_backup/watchedState.sql
 +
 +        rsync -rlptDv --chmod=Du+rwx \
 +        "/var/lib/plexmediaserver/Library/Application Support/Plex Media Server/Plug-in Support/Databases/" \
 +        /mnt/server-backup/$backupdir/$today/PLEX_server_backup/Databases/
 +        rsync -lptDv --chmod=Du+rwx \
 +        "/var/lib/plexmediaserver/Library/Application Support/Plex Media Server/Preferences.xml" \
 +        /mnt/server-backup/$backupdir/$today/PLEX_server_backup/Preferences.xml
 +fi
  
-# Backup /opt Verzeichnis+# Backup der Files von /opt:
 if [ -d "/opt" ]; then if [ -d "/opt" ]; then
- mkdir /mnt/server-backup/$backupdir/$today/opt +        mkdir /mnt/server-backup/$backupdir/$today/opt 
- cp -/opt//mnt/server-backup/$backupdir/$today/opt/+        rsync -rlptDv --chmod=Du+rwx --exclude 'eff.org'
 +        /opt/ 
 +        /mnt/server-backup/$backupdir/$today/opt/
 fi fi
  
 +#chmod -R 775 /mnt/server-backup/$backupdir/$today/*
 # Dump database into SQL file # Dump database into SQL file
  #mysqldump --user=$user --password=$password --host=$host $db_name > /mnt/server-backup/$backupdir/$today/$db_name-$today.sql  #mysqldump --user=$user --password=$password --host=$host $db_name > /mnt/server-backup/$backupdir/$today/$db_name-$today.sql
Line 105: Line 128:
 ## Löschen von 7 Tage alten Backups: ## Löschen von 7 Tage alten Backups:
 if [ -d "/mnt/server-backup/$backupdir/$OLDBACKUP" ]; then if [ -d "/mnt/server-backup/$backupdir/$OLDBACKUP" ]; then
- rm -fR /mnt/server-backup/$backupdir/$OLDBACKUP+        rm -Rf /mnt/server-backup/$backupdir/$OLDBACKUP
 fi fi
 ################################################################################################ ################################################################################################
  • skripting-section/bash/server-backup-skript.1504525499.txt.gz
  • Last modified: 2017/09/04 13:44
  • by michael