#!/bin/bash date="$(date +"%m%d%H")" echo "Start Backup" > /home/lucas/.LOG/log_backup_$date.log find /home/lucas/.LOG/log_backup_* -mtime +12 -type f -delete STARTTIME=$(date +"%T") echo "Starttime: $STARTTIME" sleep 2 #BACKUP=true BACKUP_DATA=false BACKUP_CAL=true BACKUP_THESIS=false if [ "$1" == "-thesis" ]; then BACKUP_DATA=false BACKUP_CAL=false BACKUP_THESIS=true echo "Backup thesis only" fi if $BACKUP_DATA; then old_bak="$(find /backup/backup_* -mtime +83 -type f)" find /backup/backup_* -mtime +83 -type f -delete old_doc_bak="$(find /backup/backup_documents_* -mtime +31 -type f)" find /backup/backup_documents_* -mtime +31 -type f -delete if [ -d /nextcloud/nextcloud-data/lucas/files/Dokumente ]; then { tar -zcvf /backup/backup_documents_lucas$(date -d "today" +"%m%d").tgz /nextcloud/nextcloud-data/lucas/files/Dokumente ; echo "Lucas Documents: $?" >> /home/lucas/.LOG/log_backup_$date.log ; }& PID_DOC_LUC=$! else PID_DOC_LUC=0 fi if [ -d /nextcloud/nextcloud-data/mimi/files/Dokumente ]; then { tar -zcvf /backup/backup_documents_mimi$(date -d "today" +"%m%d").tgz /nextcloud/nextcloud-data/mimi/files/Dokumente ; echo "Mimi Documents: $?" >> /home/lucas/.LOG/log_backup_$date.log ; } & PID_DOC_MIMI=$! else PID_DOC_MIMI=0 fi PIC_Overwrite=false if [ $(find /backup/backup_pictures_* -type f | wc -l) -lt 1 ] || [ $(find /backup/backup_pictures_* -mtime -13 -type f | wc -l) -lt 1 ] || $PIC_Overwrite; then printf "Backup Pictures because it's the %s\n " "$(date -d 'today' +'%d.%m')" >> /home/lucas/.LOG/log_backup_$date.log printf "Today: %s\n" "$(date -d "today" +"%d")" old_pic_bak="Remove old Pictures: $(find /backup/backup_pictures_* -mtime +50 -type f)" find /backup/backup_pictures_* -mtime +30 -type f -delete { tar -zcvf /backup/backup_pictures_$(date -d "today" +"%m%d").tgz /nextcloud/nextcloud-data/lucas/files/Media/Pictures ; echo "Lucas Pictures: $?" >> /home/lucas/.LOG/log_backup_$date.log ; } & PID_PIC=$! printf "Backup Music because it's the %s\n " "$(date -d 'today' +'%d.%m')" >> /home/lucas/.LOG/log_backup_$date.log printf "Today: %s\n" "$(date -d "today" +"%d")" old_music_bak="Remove old Pictures: $(find /backup/backup_pictures_* -mtime +50 -type f)" find /backup/backup_music_* -mtime +30 -type f -delete { tar -zcvf /backup/backup_music_$(date -d "today" +"%m%d").tgz /nextcloud/nextcloud-data/lucas/files/Media/Music/Musik ; echo "Lucas Music: $?" >> /home/lucas/.LOG/log_backup_$date.log ; } & PID_MUSIC=$! pics=true else echo "No Picture and Music Backup planed" >> /home/lucas/.LOG/log_backup_$date.log fi wait $PID_DOC_MIMI echo "Documents Mimi finished" >> /home/lucas/.LOG/log_backup_$date.log wait $PID_DOC_LUC echo "Documents Lucas finished" >> /home/lucas/.LOG/log_backup_$date.log if [ $pics ]; then wait $PID_PIC echo "Picture Backup Finished" >> /home/lucas/.LOG/log_backup_$date.log wait $PID_MUSIC echo "Music Backup Finished" >> /home/lucas/.LOG/log_backup_$date.log fi echo "Backup Finished" >> /home/lucas/.LOG/log_backup_$date.log fi ENDTIME=$(date +%T) if $BACKUP_CAL; then old_cal_bak="$(find /backup/backup_calender_* -mtime +31 -type f)" find /backup/backup_calender_* -mtime +31 -type f -delete backup_cal_OC="/backup/backup_calender_NC_$(date -d "today" +"%m%d").ics" backup_cal_geb="/backup/backup_calender_geb_$(date -d "today" +"%m%d").ics" backup_cal_home="/backup/backup_calender_home_$(date -d "today" +"%m%d").ics" #export WGETRC="/home/lucas/Documents/Dokumente/Projekte/Server/www/wgetrc" wget --no-check-certificate -O "$backup_cal_OC" "https://someoneslinux.ddns.net/remote.php/dav/calendars/lucas/nextcloud?export" if [ $? -eq 0 ]; then CAL_RET1="nextCloud Calender backup Successfull"; fi wget --no-check-certificate -O "$backup_cal_geb" "https://someoneslinux.ddns.net/remote.php/dav/calendars/lucas/geburtstage?export" if [ $? -eq 0 ]; then CAL_RET2="Geburtstags Calender backup Successfull"; fi wget --no-check-certificate -O "$backup_cal_home" "https://someoneslinux.ddns.net/remote.php/dav/calendars/lucas/haushalt?export" if [ $? -eq 0 ]; then CAL_RET3="Haushalts Calender backup Successfull"; fi old_cont_bak="$(find /backup/backup_contacts_* -mtime +31 -type f)" find /backup/backup_calender_* -mtime +31 -type f -delete backup_contacts_phone="/backup/backup_contacts_ContactsPhone_$(date -d "today" +"%m%d").vcf" backup_email_addr="/backup/backup_contacts_EmailAddressen_$(date -d "today" +"%m%d").vcf" wget --no-check-certificate -O "$backup_contacts_phone" "https://someoneslinux.ddns.net/remote.php/dav/addressbooks/users/lucas/phonecontacts-1?export" if [ $? -eq 0 ]; then CON_RET1="nextCloud Contacts Phone backup Successfull"; fi wget --no-check-certificate -O "$backup_email_addr" "https://someoneslinux.ddns.net/remote.php/dav/addressbooks/users/lucas/emailadressen-1?export" if [ $? -eq 0 ]; then CON_RET2="NextCloud Contacts Email backup Successfull"; fi fi if $BACKUP_THESIS;then old_thesis_bak="$(find /backup/backup_thesis_* -mtime +31 -type f)" if [ -d /nextcloud/nextcloud-data/lucas/files/Dokumente/Studium/Master/Thesis/ ]; then { tar -zcvf /backup/backup_thesis_lucas$(date -d "today" +"%y%m%d_%H%M").tgz /nextcloud/nextcloud-data/lucas/files/Dokumente/Studium/Master/Thesis/ ; echo "Lucas Thesis: $?" >> /home/lucas/.LOG/log_backup_$date.log ; }& PID_THESIS_LUC=$! else PID_THESIS_LUC=0 fi wait $PID_THESIS_LUC echo "Thesis finished" >> /home/lucas/.LOG/log_backup_$date.log if [ ! -d /backup/bkp ] ; then mkdir /backup/bkp; fi last=-1; find /backup/ -type f -name '*thesis*' -printf '%f\0' | sort -nz | while read -d '' i; do s=$(stat -c '%s' "/backup/$i"); [[ $s = $last ]] && rm "/backup/$i"; last=$s; done #find /backup/ -type f -printf "%s %p\n" | grep "thesis" | sort -n | uniq -d --check-chars=20 | awk '{ print $2 }' | xargs mv -t /backup/bkp/ find /backup/ -maxdepth 1 -type f -name "*thesis*" -mtime +31 -type f -delete #mv /backup/bkp/* /backup fi if [ "$old_bak" == "" ]; then old_bak="Nothing older than 83 Days removed" fi if [ "$old_thesis_bak" == "" ]; then old_thesis_bak="No Thesis Documents removed" fi if [ "$old_doc_bak" == "" ]; then old_doc_bak="No Documents removed" fi if [ "$old_pic_bak" == "" ]; then old_pic_bak="No Pictures removed" fi if [ "$old_music_bak" == "" ]; then old_music_bak="No Pictures removed" fi if [ "$old_cal_bak" == "" ]; then old_cal_bak="No Calenders removed" fi if [ "$old_cont_bak" == "" ]; then old_cont_bak="No Contacts removed" fi IFS=$'\n'; set -f data_dev=($(df -h | grep "/dev/sd[a-f]" | sed 's/ \+/ /g' | cut -f1 -d' ')) unset IFS; set +f osspace="" Out=""; for ((i=0;i<${#data_dev[@]};i++)); do device="$(df -h "${data_dev[$i]}" | sed 's/ \+/ /g' | tail -1 | cut -f6 -d' ')" if [ "$device" != "/" ]; then device="/$(echo $device | rev | cut -f1 -d'/' |rev):" else device="/root:" fi osspace="$(df -h "${data_dev[$i]}" | tr -d '%' | awk 'NR==2 { printf "Total: %sB, Used: %sB, Free: %sB ... %spercent used\n",$2,$3,$4,$5; }')" tab_num=$((3 - ${#device} / 8)) Out="$Out${device}" for ((j=0;j<$tab_num;j++));do Out="$Out\t"; done Out="$Out$osspace\n" done if [[ $BACKUP_DATA == true || $BACKUP_CAL == true ]]; then mail -s "Automated Servermessage - Backup" lucas-f@live.at << MSG_BODY_HERE Remove Old Files: $old_doc_bak Remove Really Old Files: $old_bak Document Backup: $(cat /home/lucas/.LOG/log_backup_$date.log | grep Documents) Picture Backup: $(if [ $pics ]; then printf "%s %s\n%s %s\n" "$old_pic_bak" "$(cat /home/lucas/.LOG/log_backup_$date.log | grep Pictures)" "$old_music_bak" "$(cat /home/lucas/.LOG/log_backup_$date.log | grep Music)"; else printf "No Picture and Music Backup necessary."; fi) Remove Old Calenders: $old_cal_bak Calender Backup: $CAL_RET1 $CAL_RET2 $CAL_RET3 Remove Old Contacts: $old_cont_bak Contacts Backup: $CON_RET1 $CON_RET2 Directory: $(printf "$Out") $(ls -lh /backup/) Start: $STARTTIME End: $ENDTIME MSG_BODY_HERE fi echo "Endtime: $ENDTIME"