het script is zo goed als ik kon af.
het script draait, de bestanden worden goed gekopieerd en er worden mails verstuurd.
-------- Handleiding --------
STAP 1: Maak een shared folder met de naam Packages en geef de admin hier lees en schrijf rechten op.
STAP 2: Plaats de scripts in /opt/home/copyscript (/opt/home bestaat standaard niet dus deze dien je aan te maken)
2.1 Pas de variabelen aan in het copyfiles.sh bestand.
STAP 3: Pas de rechten aan op het copy script: chmod +x /opt/home/copyscript/copyfiles.sh
STAP 4: Edit de user settings voor php
/usr/syno/etc/php/user-setting.ini
voeg het volgende toe aan het eind van de (open_basedir) eerste regel
":/opt/home/copyscript/" en ":/volume1/packages"(quotes weg laten)
(M.b.v. wizjos website onderstaande info)
STAP 5: Voeg het script toe aan /etc/crontab
0 0 * * * root /opt/home/copyfiles.sh
LET OP! De lege ruimtes zijn TAB's!
UITLEG:
#minute hour mday month wday who command
| | | | | | |
| | | | | | +--------------programma of script
| | | | | +------------------------user die het start (root)
| | | | +--------------------------------dag van de week (0-7 of namen)
| | | | 0 en 7 staan voor zondag
| | | +----------------------------------------maand (1-12)
| | +------------------------------------------------dag van de maand (1-31 of namen)
| +-------------------------------------------------------uur (0-23)
+----------------------------------------------------------------minuut (0-59)
STAP 6: Pas het volgende bestand aan:
/usr/syno/etc/rc.d/S04crond.sh
Op regel 17 staat $CROND
Verander dat in $CROND -l 8 -L /var/log/cron.log
(Standaard worden er geen logs bijgehouden van cronjobs, door bovenstaande door te voeren gebeurd dit wel)
STAP 7: Herstart de cron daemon
/usr/syno/etc/rc.d/S04crond.sh stop
/usr/syno/etc/rc.d/S04crond.sh start
De backup zou nu elke avond om 00:00 moeten starten indien je mijn instellingen hebt overgenomen.
Start het script handmatig met commando:
vanuit de root / folder: ./opt/home/copyscript/copyfiles.sh
vanuit elke andere locatie: /opt/home/copyscript/copyfiles.sh
LET OP!
Het script wordt uitgevoerd door root.
Dit betekend dat je de backup bestanden ook alleen als root uit kan pakken/bekijken!
--------- copy files script (naam copyfiles.sh) ---------
#!/bin/sh
###############################################
# #
# backup script #
# #
# This script creates tarballs of selected #
# directories assigned as variables. #
# #
# #
# last modified: 2012/05/19 by rapTile #
SCRIPT_VERSION="1.7"
# #
# #
###############################################
######## Script Variables (static): ########
HOSTNAME=`hostname -s`
DATE=`date +%Y_%m_%d-%H_%M`
BACKUP_CONTAINER=/volume1/Packages
LOGFILE_CONTAINER=$BACKUP_CONTAINER/scriptLogs
LOG_FILE=$LOGFILE_CONTAINER/SynologyFiles.$DATE.log
BACKUPFILES=$BACKUP_CONTAINER/backupFileLists
BACKUPDAY=backupList.$DATE.txt
DEST=/volume1/Packages/latestBackup
OLD_BACKUPS=/volume1/Packages/oldBackups
BACKUP_DEVICE=$DEST/$HOSTNAME.$DATE.tar.gz
####### EXPORT BARIABLES #######
export HOSTNAME=`hostname -s`
export DATE=`date +%Y_%m_%d-%H_%M`
export BACKUP_CONTAINER=/volume1/Packages
export LOGFILE_CONTAINER=$BACKUP_CONTAINER/scriptLogs
export LOG_FILE=$LOGFILE_CONTAINER/SynologyFilesBackup.$DATE.log
export BACKUPFILES=$BACKUP_CONTAINER/backupFileLists
export BACKUPDAY=backupList.$DATE.txt
export DEST=/volume1/Packages/latestBackup
export OLD_BACKUPS=/volume1/Packages/oldBackups
export BACKUP_DEVICE=$DEST/$HOSTNAME.$DATE.tar.gz
export ADMIN=admin@domein.nl
######## Script Variables (dynamic): ########
#CRONTAB
CRONTAB=/etc/crontab
#RSA/DSA KEYS
SSH_KEYS=/root/.ssh/authorized_keys
#SSH CONFIG
SSH_CONFIG=/etc/ssh/sshd_config
#NZBGET
NZBGET_CONF=/volume1/@appstore/nzbget/var/nzbget.conf
NZBGET_SETTINGS=/volume1/@appstore/nzbget/var/settings.php
NZBGET_POST_PROCESS=/volume1/@appstore/nzbget/var/postprocess.conf
#SICKBEARD
SICKBEARD_CONFIG=/usr/local/var/sickbeard/config.ini
SICKBEARD_DB=/usr/local/var/sickbeard/sickbeard.db
#AUTOSUB
AUTOSUB_MAIL=/volume1/@appstore/AutoSub/autosubmail.py #(aangepaste ExamplePostProcess.py)
AUTOSUB_DB=/volume1/@appstore/AutoSub/database.db
AUTOSUB_CONFIG=/volume1/@appstore/AutoSub/config.properties
#COUCHPOTATO
COUCHPOTATO_CONFIG=/volume1/@appstore/couchpotato/var/config.ini
COUCHPOTATO_DB=/volume1/@appstore/couchpotato/var/data.db
#HEADPHONES
HEADPHONES_CONFIG=/volume1/@appstore/headphones/var/config.ini
HEADPHONES_DB=/volume1/@appstore/headphones/var/headphones.db
#SCRIPT AND COPYLOG MAILSCRIPT
COPYFILES_SCRIPT=/opt/home/copyscript/copyfiles.sh
COPYFILES_MAIL_SCRIPT=/opt/home/copyscript/copylog.php
#PHPINI
PHPINI=/usr/syno/etc/php/user-setting.ini
#CRONLOG
CRONLOG=/var/log/cron.log
######## SCRIPT ########
#Just to be sure the script doesn't fail ...:
if [ ! -d $LOGFILE_CONTAINER ]; then
echo "There is no $LOGFILE_CONTAINER container. I'll make one for you now..."
mkdir $LOGFILE_CONTAINER
fi
#Just to be sure the script doesn't fail before it gets a chance to run:
if [ ! -e $BACKUPFILES ]; then
echo "There is no $BACKUPFILES container. I'll make one for you now..." >> $LOG_FILE
echo "There is no $BACKUPFILES container. I'll make one for you now..."
mkdir $BACKUPFILES
fi
#Just to be sure the script doesn't fail:
if [ ! -e $OLD_BACKUPS ]; then
echo "There is no $OLD_BACKUPS container. I'll make one for you now..." >> $LOG_FILE
echo "There is no $OLD_BACKUPS container. I'll make one for you now..."
mkdir $OLD_BACKUPS
echo " " >>$LOG_FILE
echo " "
fi
echo "Ok, let's get started" > $LOG_FILE
echo "Ok, let's get started"
echo " " >>$LOG_FILE
echo " "
echo This begins $0 v.$SCRIPT_VERSION >> $LOG_FILE
echo This begins $0 v.$SCRIPT_VERSION
echo " " >> $LOG_FILE
echo " "
#First, let's move the previous backup to the archive container:
echo "moving the old backups to $OLD_BACKUPS" >> $LOG_FILE
echo "moving the old backups to $OLD_BACKUPS"
echo "mv $DEST/* $OLD_BACKUPS" >> $LOG_FILE
echo "mv $DEST/* $OLD_BACKUPS"
mv $DEST/*.tar.gz $OLD_BACKUPS
echo " " >>$LOG_FILE
echo " "
#And... while we're at it... we should clean that old container out so it doesn't fill
#up with junk. This will delete anything older than 7 days:
echo "Cleaning out the old backups, $OLD_BACKUPS, container" >> $LOG_FILE
echo "Cleaning out the old backups, $OLD_BACKUPS, container"
echo "/usr/bin/find $OLD_BACKUPS -type f -mtime +7 -print | xargs rm -rf >/dev/null 2>&1" >> $LOG_FILE
echo "/usr/bin/find $OLD_BACKUPS -type f -mtime +7 -print | xargs rm -rf >/dev/null 2>&1"
/usr/bin/find $OLD_BACKUPS -type f -mtime +7 -print | xargs rm -rf >/dev/null 2>&1
echo " " >>$LOG_FILE
echo " "
#And... while we're at it... we should clean the logFile container so it doesn't fill
#up with junk too. This will delete anything older than 7 days:
echo "Cleaning out the logfile container, $LOGFILE_CONTAINER" >> $LOG_FILE
echo "Cleaning out the logfile container, $LOGFILE_CONTAINER"
echo "/usr/bin/find $LOGFILE_CONTAINER -type f -mtime +7 -print | xargs rm -rf >/dev/null 2>&1" >> $LOG_FILE
echo "/usr/bin/find $LOGFILE_CONTAINER -type f -mtime +7 -print | xargs rm -rf >/dev/null 2>&1"
/usr/bin/find $LOGFILE_CONTAINER -type f -mtime +7 -print | xargs rm -rf >/dev/null 2>&1
echo " " >>$LOG_FILE
echo " "
#And... while we're at it... we should clean the backuplist container so it doesn't fill
#up with junk too. This will delete anything older than 7 days:
echo "Cleaning out the backuplist container, $BACKUPFILES" >> $LOG_FILE
echo "Cleaning out the backuplist container, $BACKUPFILES"
echo "/usr/bin/find $BACKUPFILES -type f -mtime +7 -print | xargs rm -rf >/dev/null 2>&1" >> $LOG_FILE
echo "/usr/bin/find $BACKUPFILES -type f -mtime +7 -print | xargs rm -rf >/dev/null 2>&1"
/usr/bin/find $BACKUPFILES -type f -mtime +7 -print | xargs rm -rf >/dev/null 2>&1
echo " " >>$LOG_FILE
echo " "
echo "Starting the backup . . . " >> $LOG_FILE
echo "Starting the backup . . . "
#Now... let's run the backup:
echo " . . . This is a full backup for $HOSTNAME" >> $LOG_FILE
echo " . . . This is a full backup for $HOSTNAME"
echo " " >> $LOG_FILE
echo " "
echo "Checking for the existence of the destination: $DEST" >> $LOG_FILE
echo "Checking for the existence of the destination: $DEST"
#This is only if there is a connection to an external device:
if [ ! -e $DEST ]; then
echo " Your device isn't connected or the $DEST folder doesn't exist." >> $LOG_FILE
echo " Your device isn't connected or the $DEST folder doesn't exist."
echo " I'll try to fix that for you." >> $LOG_FILE
echo " I'll try to fix that for you."
echo " Making the $DEST directory" >> $LOG_FILE
echo " Making the $DEST directory"
mkdir -p $DEST
echo " Checking for the existence of the destination: $DEST - Again" >> $LOG_FILE
echo " Checking for the existence of the destination: $DEST - Again"
if [ ! -e $DEST ]; then
echo " Sorry... I can't go on like this." >> $LOG_FILE
echo " Sorry... I can't go on like this."
echo "" >> $LOG_FILE
echo ""
export SUBJECT="$HOSTNAME's $0 script failed. There was no $DEST directory found. "
echo "$SUBJECT"
echo "$SUBJECT" >> $LOG_FILE
cp -f $LOG_FILE .
/usr/bin/php -f /opt/home/copyscript/copylog.php
exit 1
fi
else
echo " Your destination is available: $DEST exists " >> $LOG_FILE
echo " Your destination is available: $DEST exists "
echo " " >> $LOG_FILE
echo " "
fi
if [ ! -e $BACKUPFILES ]; then
echo "The BACKUPFILES, $BACKUPFILES, container doesn't exist..." >>LOG_FILE
echo "The BACKUPFILES, $BACKUPFILES, container doesn't exist..." >>LOG_FILE
export SUBJECT="$HOSTNAME's $0 script failed. There was no $BACKUPFILES directory found. "
/usr/bin/php -f /opt/home/copyscript/copylog.php
fi
###### CRONTAB ######
if [ $CRONTAB ]; then
echo "find $CRONTAB -type f -print > $BACKUPFILES/$BACKUPDAY" >> $LOG_FILE
echo "find $CRONTAB -type f -print > $BACKUPFILES/$BACKUPDAY"
find $CRONTAB -type f -print > $BACKUPFILES/$BACKUPDAY
echo " " >> $LOG_FILE
echo " "
fi
###### SSH KEYS ######
if [ $SSH_KEYS ]; then
echo "find $SSH_KEYS -type f -print >> $BACKUPFILES/$BACKUPDAY" >> $LOG_FILE
echo "find $SSH_KEYS -type f -print >> $BACKUPFILES/$BACKUPDAY"
find $SSH_KEYS -type f -print >> $BACKUPFILES/$BACKUPDAY
echo " " >> $LOG_FILE
echo " "
fi
###### SSH CONFIG ######
if [ $SSH_CONFIG ]; then
echo "find $SSH_CONFIG -type f -print >> $BACKUPFILES/$BACKUPDAY" >> $LOG_FILE
echo "find $SSH_CONFIG -type f -print >> $BACKUPFILES/$BACKUPDAY"
find $SSH_CONFIG -type f -print >> $BACKUPFILES/$BACKUPDAY
echo " " >> $LOG_FILE
echo " "
fi
###### NZBGET ######
if [ $NZBGET_CONF ]; then
echo "find $NZBGET_CONF -type f -print >> $BACKUPFILES/$BACKUPDAY" >> $LOG_FILE
echo "find $NZBGET_CONF -type f -print >> $BACKUPFILES/$BACKUPDAY"
find $NZBGET_CONF -type f -print >> $BACKUPFILES/$BACKUPDAY
echo " " >> $LOG_FILE
echo " "
fi
if [ $NZBGET_SETTINGS ]; then
echo "find $NZBGET_SETTINGS -type f -print >> $BACKUPFILES/$BACKUPDAY" >> $LOG_FILE
echo "find $NZBGET_SETTINGS -type f -print >> $BACKUPFILES/$BACKUPDAY"
find $NZBGET_SETTINGS -type f -print >> $BACKUPFILES/$BACKUPDAY
echo " " >> $LOG_FILE
echo " "
fi
###### SICKBEARD ######
if [ $SICKBEARD_CONFIG ]; then
echo "find $SICKBEARD_CONFIG -type f -print >> $BACKUPFILES/$BACKUPDAY" >> $LOG_FILE
echo "find $SICKBEARD_CONFIG -type f -print >> $BACKUPFILES/$BACKUPDAY"
find $SICKBEARD_CONFIG -type f -print >> $BACKUPFILES/$BACKUPDAY
echo " " >> $LOG_FILE
echo " "
fi
if [ $SICKBEARD_DB ]; then
echo "find $SICKBEARD_DB -type f -print >> $BACKUPFILES/$BACKUPDAY" >> $LOG_FILE
echo "find $SICKBEARD_DB -type f -print >> $BACKUPFILES/$BACKUPDAY"
find $SICKBEARD_DB -type f -print >> $BACKUPFILES/$BACKUPDAY
echo " " >> $LOG_FILE
echo " "
fi
###### AUTOSUB ######
if [ $AUTOSUB_MAIL ]; then
echo "find $AUTOSUB_MAIL -type f -print >> $BACKUPFILES/$BACKUPDAY" >> $LOG_FILE
echo "find $AUTOSUB_MAIL -type f -print >> $BACKUPFILES/$BACKUPDAY"
find $AUTOSUB_MAIL -type f -print >> $BACKUPFILES/$BACKUPDAY
echo " " >> $LOG_FILE
echo " "
fi
if [ $AUTOSUB_DB ]; then
echo "find $AUTOSUB_DB -type f -print >> $BACKUPFILES/$BACKUPDAY" >> $LOG_FILE
echo "find $AUTOSUB_DB -type f -print >> $BACKUPFILES/$BACKUPDAY"
find $AUTOSUB_DB -type f -print >> $BACKUPFILES/$BACKUPDAY
echo " " >> $LOG_FILE
echo " "
fi
if [ $AUTOSUB_CONFIG ]; then
echo "find $AUTOSUB_CONFIG -type f -print >> $BACKUPFILES/$BACKUPDAY" >> $LOG_FILE
echo "find $AUTOSUB_CONFIG -type f -print >> $BACKUPFILES/$BACKUPDAY"
find $AUTOSUB_CONFIG -type f -print >> $BACKUPFILES/$BACKUPDAY
echo " " >> $LOG_FILE
echo " "
fi
###### COUCHPOTATO ######
if [ $COUCHPOTATO_CONFIG ]; then
echo "find $COUCHPOTATO_CONFIG -type f -print >> $BACKUPFILES/$BACKUPDAY" >> $LOG_FILE
echo "find $COUCHPOTATO_CONFIG -type f -print >> $BACKUPFILES/$BACKUPDAY"
find $COUCHPOTATO_CONFIG -type f -print >> $BACKUPFILES/$BACKUPDAY
echo " " >> $LOG_FILE
echo " "
fi
if [ $COUCHPOTATO_DB ]; then
echo "find $COUCHPOTATO_DB -type f -print >> $BACKUPFILES/$BACKUPDAY" >> $LOG_FILE
echo "find $COUCHPOTATO_DB -type f -print >> $BACKUPFILES/$BACKUPDAY"
find $COUCHPOTATO_DB -type f -print >> $BACKUPFILES/$BACKUPDAY
echo " " >> $LOG_FILE
echo " "
fi
###### HEADPHONES ######
if [ $HEADPHONES_CONFIG ]; then
echo "find $HEADPHONES_CONFIG -type f -print >> $BACKUPFILES/$BACKUPDAY" >> $LOG_FILE
echo "find $HEADPHONES_CONFIG -type f -print >> $BACKUPFILES/$BACKUPDAY"
find $HEADPHONES_CONFIG -type f -print >> $BACKUPFILES/$BACKUPDAY
echo " " >> $LOG_FILE
echo " "
fi
if [ $HEADPHONES_DB ]; then
echo "find $HEADPHONES_DB -type f -print >> $BACKUPFILES/$BACKUPDAY" >> $LOG_FILE
echo "find $HEADPHONES_DB -type f -print >> $BACKUPFILES/$BACKUPDAY"
find $HEADPHONES_DB -type f -print >> $BACKUPFILES/$BACKUPDAY
echo " " >> $LOG_FILE
echo " "
fi
###### COPY AND MAIL SCRIPT ######
#SCRIPT AND COPYLOG MAILSCRIPT
COPYFILES_SCRIPT=/opt/home/copyscript/copyfiles.sh
COPYFILES_MAIL_SCRIPT=/opt/home/copyscript/copylog.php
if [ $COPYFILES_SCRIPT ]; then
echo "find $COPYFILES_SCRIPT -type f -print >> $BACKUPFILES/$BACKUPDAY" >> $LOG_FILE
echo "find $COPYFILES_SCRIPT -type f -print >> $BACKUPFILES/$BACKUPDAY"
find $COPYFILES_SCRIPT -type f -print >> $BACKUPFILES/$BACKUPDAY
echo " " >> $LOG_FILE
echo " "
fi
if [ $COPYFILES_MAIL_SCRIPT ]; then
echo "find $COPYFILES_MAIL_SCRIPT -type f -print >> $BACKUPFILES/$BACKUPDAY" >> $LOG_FILE
echo "find $COPYFILES_MAIL_SCRIPT -type f -print >> $BACKUPFILES/$BACKUPDAY"
find $COPYFILES_MAIL_SCRIPT -type f -print >> $BACKUPFILES/$BACKUPDAY
echo " " >> $LOG_FILE
echo " "
fi
###### PHPINI ######
if [ $PHPINI ]; then
echo "find $PHPINI -type f -print >> $BACKUPFILES/$BACKUPDAY" >> $LOG_FILE
echo "find $PHPINI -type f -print >> $BACKUPFILES/$BACKUPDAY"
find $PHPINI -type f -print >> $BACKUPFILES/$BACKUPDAY
echo " " >> $LOG_FILE
echo " "
fi
###### CRON LOGFILE ######
if [ $CRONLOG ]; then
echo "find $CRONLOG -type f -print >> $BACKUPFILES/$BACKUPDAY" >> $LOG_FILE
echo "find CRONLOGPHPINI -type f -print >> $BACKUPFILES/$BACKUPDAY"
find $CRONLOG -type f -print >> $BACKUPFILES/$BACKUPDAY
echo " " >> $LOG_FILE
echo " "
fi
###### DONE ######
echo "Now, let's make a tarball out of this: " >> $LOG_FILE
echo "Now, let's make a tarball out of this: "
echo "tar -cvz -T $BACKUPFILES/$BACKUPDAY -f $BACKUP_DEVICE" >> $LOG_FILE
echo "tar -cvz -T $BACKUPFILES/$BACKUPDAY -f $BACKUP_DEVICE"
echo " " >> $LOG_FILE
echo " "
tar -cvz -T $BACKUPFILES/$BACKUPDAY -f $BACKUP_DEVICE
if [ ! -f $BACKUP_DEVICE ]; then
echo "Something went wrong. " >> $LOG_FILE
echo "Something went wrong. "
echo "The BACKUP_DEVICE, $BACKUP_DEVICE is missing " >> $LOG_FILE
echo "The BACKUP_DEVICE, $BACKUP_DEVICE is missing "
echo "Because it doesn't exist, we're emailing $ADMIN " >> $LOG_FILE
echo "Because it doesn't exist, we're emailing $ADMIN "
export SUBJECT="$HOSTNAME's $0 script failed. "
/usr/bin/php -f /opt/home/copyscript/copylog.php
exit 1
else
echo " " >> $LOG_FILE
echo " "
echo " The file, $BACKUP_DEVICE, exists. So..." >> $LOG_FILE
echo " The file, $BACKUP_DEVICE, exists. So... "
echo " ... it appears that the backup went smoothly." >>$LOG_FILE
echo " ... it appears that the backup went smoothly."
fi
echo " " >> $LOG_FILE
echo " "
echo " Here's what we've done: " >> $LOG_FILE
echo " Here's what we've done: "
echo " " >> $LOG_FILE
echo " "
echo " ls -alh $BACKUP_DEVICE" >> $LOG_FILE
echo " ls -alh $BACKUP_DEVICE"
echo " " >> $LOG_FILE
echo " "
ls -alh $BACKUP_DEVICE >> $LOG_FILE
ls -alh $BACKUP_DEVICE
echo " " >> $LOG_FILE
echo " "
echo "Done. Emailing the ADMIN = $ADMIN" >>$LOG_FILE
echo "Done. Emailing the ADMIN = $ADMIN"
export SUBJECT="$0 script completed on $HOSTNAME on $BACKUPDAY"
cp -f $LOG_FILE /opt/home/copyscript/.
/usr/bin/php -f /opt/home/copyscript/copylog.php
chmod -R 777 $BACKUP_CONTAINER
rm -f /opt/home/copyscript/SynologyFilesBackup/*
exit 0
-------- Mailscript (naam copylog.php) --------
<?php
date_default_timezone_set('GMT+1');
//$date = date('l jS of F Y');
$HOSTNAME = getenv('HOSTNAME');
$DATE = getenv('DATE');
$BACKUP_CONTAINER = getenv('BACKUP_CONTAINER');
$LOGFILE_CONTAINER = getenv('LOGFILE_CONTAINER');
$LOG_FILE = getenv('LOG_FILE');
$BACKUPFILES = getenv('BACKUPFILES');
$BACKUPDAY = getenv('BACKUPDAY');
$DEST = getenv('DEST');
$OLD_BACKUPS = getenv('OLD_BACKUPS');
$BACKUP_DEVICE = getenv('BACKUP_DEVICE');
$ADMIN = getenv('ADMIN');
$SUBJECT = getenv('SUBJECT');
$myFile = "$LOG_FILE";
$mail_log_file = basename($myFile);
$theData = file_get_contents($myFile);
mail("$ADMIN", 'BACKUPLOG '."$DATE"." $SUBJECT", "$theData");
?>
Nu moet ik dus alleen nog weten welke bestanden in voor bovenstaande packages tekort kom qua backup om het script compleet te maken.