cleanup, some more elegant syntax, include control of restic on yunohost
This commit is contained in:
		| @@ -4,35 +4,39 @@ | ||||
|  | ||||
| TARGET=/yunohost-backups | ||||
| HOST=yunohost | ||||
| TODAY_DATE=`date '+%F'` | ||||
| TODAY_DATE=$(date '+%F') | ||||
| LOGFILE=${TARGET}/$TODAY_DATE.$$.log | ||||
| ERR=0 | ||||
| NUMRETAINBACKUPS=10 | ||||
|  | ||||
| echo "yunohost backup begin:" >$LOGFILE | ||||
| date >>$LOGFILE | ||||
| echo "yunohost backup begin $(date):" >$LOGFILE | ||||
| #date >>$LOGFILE | ||||
| # delete max. 3 old backups per run | ||||
| for i in 1 2 3 | ||||
| do | ||||
| 	NUMBACKUPS=`ssh ${HOST} sudo yunohost backup list | grep -- '-' | wc -l` | ||||
| 	ERR=$? | ||||
| 	if [ ${ERR} -ne 0 ] | ||||
| 	then | ||||
|         	echo yunohost could not list backups >>$LOGFILE | ||||
| 		date >>$LOGFILE | ||||
| 		exit 1 | ||||
| 	fi | ||||
| 	NUMBACKUPS=$(ssh ${HOST} sudo yunohost backup list | grep -- '-' | wc -l) || { echo yunohost could not list backups $(date) >>$LOGFILE ; exit 1; } | ||||
| #	ERR=$? | ||||
| #	if [ ${ERR} -ne 0 ] | ||||
| #	then | ||||
| #        	echo yunohost could not list backups >>$LOGFILE | ||||
| #		date >>$LOGFILE | ||||
| #		exit 1 | ||||
| #	fi | ||||
|  | ||||
| 	if [ ${NUMBACKUPS} -gt ${NUMRETAINBACKUPS} ] | ||||
| 	then | ||||
| 		BACKTODEL=`ssh ${HOST} sudo yunohost backup list | grep -- '-' | head -1 | cut -d" " -f4` | ||||
| 		echo 'the following backup will be deleted:'  >>$LOGFILE 2>&1 | ||||
| 		ssh ${HOST} sudo yunohost backup info ${BACKTODEL} >>$LOGFILE 2>&1 | ||||
| 		ssh ${HOST} "sudo yunohost backup delete ${BACKTODEL}" >>$LOGFILE 2>&1 | ||||
| 		BACKTODEL=$(ssh ${HOST} sudo yunohost backup list | grep -- '-' | head -1 | cut -d" " -f4) | ||||
| 		echo 'the following backup will be deleted on ${HOST}:'  &>>$LOGFILE  | ||||
| 		ssh ${HOST} sudo yunohost backup info ${BACKTODEL} &>>$LOGFILE  | ||||
| 		ssh ${HOST} "sudo yunohost backup delete ${BACKTODEL}" &>>$LOGFILE  | ||||
| 	fi | ||||
| done | ||||
|  | ||||
| ssh ${HOST} sudo yunohost backup create >>$LOGFILE 2>&1  | ||||
| # It's hightly recommended to make your backup when the service is stopped. Please stop gitea service with this command before to run the backup 'systemctl stop gitea.service' | ||||
|  | ||||
| echo "stopping gitea service on ${HOST}"  &>>$LOGFILE  | ||||
| ssh ${HOST} sudo systemctl stop gitea.service &>>$LOGFILE  | ||||
| ssh ${HOST} sudo yunohost backup create &>>$LOGFILE  | ||||
| ERR=$? | ||||
| if [ ${ERR} == 0 ] | ||||
| then | ||||
| @@ -40,13 +44,24 @@ then | ||||
| else | ||||
| 	echo yunohost backup did NOT complete >>$LOGFILE | ||||
| fi | ||||
|  | ||||
| echo "starting gitea service on ${HOST}"  &>>$LOGFILE  | ||||
| ssh ${HOST} sudo systemctl start gitea.service &>>$LOGFILE  | ||||
|  | ||||
| echo "yunohost backup ended:" >>$LOGFILE | ||||
| date >>$LOGFILE | ||||
| BACKUPFILE=`ssh ${HOST} sudo yunohost backup list --with-info -H  | grep -A 3 $TODAY_DATE |  grep 'path:' | cut -d":" -f2 | tr -d " " | tail -1` | ||||
| scp ${HOST}:${BACKUPFILE} ${TARGET} && echo ${BACKUPFILE} successfully copied to ${TARGET}! >>$LOGFILE 2>&1 | ||||
| BACKUPFILE=$(ssh ${HOST} sudo yunohost backup list --with-info -H  | grep -A 3 $TODAY_DATE |  grep 'path:' | cut -d":" -f2 | tr -d " " | tail -1) | ||||
| scp ${HOST}:${BACKUPFILE} ${TARGET} && echo "${BACKUPFILE} successfully copied to ${TARGET}!" &>>$LOGFILE || echo "error when trying to scp ${BACKUPFILE} to ${TARGET}!" &>>$LOGFILE  | ||||
| ls -lisah ${TARGET}/`basename ${BACKUPFILE}` >>$LOGFILE | ||||
| gzip -9 ${TARGET}/`basename ${BACKUPFILE}` >>$LOGFILE 2>&1 | ||||
|  | ||||
| gzip -9 ${TARGET}/`basename ${BACKUPFILE}` &>>$LOGFILE | ||||
| ls -lisah ${TARGET}/`basename ${BACKUPFILE}`.gz &>>$LOGFILE  | ||||
| source /root/.bashrc | ||||
| echo restic backup on ${HOST} at $(date) &>>${LOGFILE}  | ||||
| #ssh ${HOST} sudo { source /root/.bashrc ; /usr/bin/restic backup /home/yunohost.backup/archives ; } &>>$LOGFILE | ||||
| ssh ${HOST} "sudo /root/bin/restic.sh" &>>$LOGFILE | ||||
| restic check --read-data-subset 20% &>>$LOGFILE | ||||
| restic stats &>>$LOGFILE | ||||
| rclone about onedrive_b2: &>>$LOGFILE | ||||
|  | ||||
| # algo to create the "keep"-array copied from https://serverfault.com/questions/575163/how-to-keep-daily-backups-for-a-week-weekly-for-a-month-monthly-for-a-year-a | ||||
| # it keeps 8 daily backups and 5 weekly (every sunday) backups | ||||
| @@ -75,8 +90,8 @@ for i in {0..5}; do | ||||
|         ((keep[$(date +%Y%m%d -d "sunday-$DW weeks")]++)) | ||||
| done | ||||
| #echo ${!keep[@]} | ||||
| echo space in filesys of ${TARGET} before pruning backups >>$LOGFILE 2>&1 | ||||
| df -h ${TARGET} >>$LOGFILE 2>&1 | ||||
| echo space in filesys of ${TARGET} before pruning backups &>>$LOGFILE | ||||
| df -h ${TARGET} &>>$LOGFILE | ||||
| for i in *.tar.gz | ||||
| do  | ||||
| 	DATE=`echo $i | cut -c1-8`  | ||||
| @@ -84,13 +99,14 @@ do | ||||
| 	if [ $? -ne 0 ] | ||||
| 	then	 | ||||
| 		echo pwd `pwd` | ||||
| 		echo deleting $DATE  >>$LOGFILE 2>&1 | ||||
| 		rm ${DATE}*.tar.* >>$LOGFILE 2>&1 | ||||
| 		rm ${DATE}*.log.* >>$LOGFILE 2>&1 | ||||
| 		echo deleting $DATE  &>>$LOGFILE | ||||
| 		rm ${DATE}*.tar.* &>>$LOGFILE | ||||
| 		rm ${DATE}*.log.* &>>$LOGFILE | ||||
| 	fi | ||||
| done | ||||
| echo space in filesys of ${TARGET} after pruning backups >>$LOGFILE 2>&1 | ||||
| df -h ${TARGET} >>$LOGFILE 2>&1 | ||||
| echo space in filesys of ${TARGET} after pruning backups &>>$LOGFILE | ||||
| df -h ${TARGET} &>>$LOGFILE | ||||
|  | ||||
| echo "$0 on `hostname` ended:" >>$LOGFILE | ||||
| date >>$LOGFILE | ||||
| # Achtung, hier wird es kompliziert :) nach diesem Kommando keine Umleitung in $LOGFILE mehr | ||||
|   | ||||
		Reference in New Issue
	
	Block a user