linux:backup_server_backup_nfs

Remote Server Backup NFS

Mithilfe des folgenden Scripts können Backups von einem Remote Server auf einen anderen angelegt werden. Hierzu wird NFS genutzt.

apt-get install nfs-common
mkdir /mnt/storage
mount -t nfs host:/pfad /mnt/storage

/etc/fstab:

host:/pfad       /mnt/storage       nfs       auto       0       0
#!/bin/sh
# Remote Server Backup Script v1.6 by PsyCore
 
# vars
 
	BCF=my.backup.tar
	BCF2=sql.backup.tgz
	NFSDir="/mnt/storage/backup_dir/"
 
	SQLUser=privileged_user
	SQLPass=passw0rd_privileged_user
 
	SUBJECT="my Backup Status"
	EMAIL="notification@yourdomain.tld"
	EMSG="/tmp/emsg.txt"
	ATTACH="/tmp/attach.txt"
	DATE=$(date +%u)
 
	EnableFileBackup=1
	EnableSQLBackup=1
	EnableCronBackup=1
	EnableCloudBackup=1
 
run_date=`date +'%d.%m.%Y %H:%M:%S'`
echo "Backup Status: " >$EMSG
echo "rsync  log" >$ATTACH
 
	if [ "$EnableSQLBackup" = "1" ]
	then
		echo "SQL Backup started " `date +'%d.%m.%Y %H:%M:%S'` >$EMSG
		#date +'%d.%m.%Y %H:%M:%S'
		mysqldump -u $SQLUser -p$SQLPass --all-databases > root_full.sql
		mysqldump -u $SQLUser -p$SQLPass --databases sql1 > sql1.sql
		mysqldump -u $SQLUser -p$SQLPass --databases sqlN > sqlN.sql
		tar cvfz sql.backup-$DATE.tgz *.sql
		cp -u sql.backup-$DATE.tgz /root/backup-database
		cp -u sql.backup-$DATE.tgz $NFSDir
	fi
 
# backup cron tasks
 
	if [ "$EnableCronBackup" = "1" ]
	then
		echo "Cron Backup started " `date +'%d.%m.%Y %H:%M:%S'` >>$EMSG
		#date +'%d.%m.%Y %H:%M:%S'
		crontab -l > crontab.txt
		tar cvfz crontab.tgz *.txt
	fi
 
	if [ "$EnableFileBackup" = "1" ]
	then	
		echo "File Backup started " `date +'%d.%m.%Y %H:%M:%S'` >>$EMSG
		#date +'%d.%m.%Y %H:%M:%S'
		tar cvfz webs.tgz /var/www/dir1 /var/www/dirN
	fi
 
	if [ "$EnableCloudBackup" = "1" ]
	then	
		echo "Cloud Backup started " `date +'%d.%m.%Y %H:%M:%S'` >>$EMSG
		#date +'%d.%m.%Y %H:%M:%S'
		rsync -a -v --exclude-from 'backup-exclude.txt' --stats --delete /usr/share/some-data $NFSDir >> $ATTACH
	fi
 
 
 
# archive all
 
	tar cvf $BCF *.tgz
 
# NFS Storage
 
cp $BCF $NFSDir
 
 
# cleanup
 
	rm -f *.sql
	rm -f *.tgz
	rm -f *.tar
	#rm -f *.txt
 
# grep "bytes sent" backup.log>>$EMSG
# grep "221 Goodbye" backup.log>>$EMSG
echo "Backup beendet: " `date +'%d.%m.%Y %H:%M:%S'` >> $EMSG
echo " ">>$EMSG
echo "remote storage: ">>$EMSG
echo "==========================">>$EMSG
ls -ashl $NFSDir>>$EMSG
echo "database archive: ">>$EMSG
echo "==========================">>$EMSG
ls -ashl /root/backup-database/>>$EMSG
 
mail -A $ATTACH -s "$SUBJECT" "$EMAIL" < $EMSG
rm $EMSG
#date +'%d.%m.%Y %H:%M:%S'
 
# EOF

Diese Datei beinhaltet die Ausnahmen, die nicht von rsync kopiert werden sollen.

xxl_sized_directory
this_dir_not

Um das Script zeitgesteuert zu starten muss ein Cronjob angelegt werden. In diesem Beispiel startet das Script jeden Montag um 12:00 Uhr. Die Ausgabe des Scripts wird in ein Logfile umgeleitet.

# m h  dom mon dow   command
0 12 * * 1 /home/user/backup.sh > /home/user/backup.log
  • linux/backup_server_backup_nfs.txt
  • Zuletzt geändert: 2024/01/13 23:19
  • von psycore