Check for improve script for webserver with mysql

Hello,
i’m new with restic.
i have few webserver with mysql (classical LAMP/LEMP) and i want backup my servers with restic.
since i have some account on google drive with unlimited space i want use it to store my backup.

so i write this script, but i’m not more clever, so i ask to review and improve my script. or use it only like a start point to write another script…

#!/bin/bash
set -o pipefail
### requisiti
### apt install restic swaks -y
### yum install restic swaks -y

# installare e configurare restic
# creare repo in $REMOTE_RCLONE $PATH_RCLONE
# creare file con password-file in

# nano /root/restic_password
# chmod 700 /root/restic_password
RESTIC_PW=/root/restic_password

DATA=$(date +"%Y-%m-%d--%H-%M")

# modificare parametri sotto
# path binary
MYSQLDUMP=/usr/bin/mysqldump
BZIP2=/usr/bin/bzip2
SWAKS=/usr/bin/swaks
IONICE=/usr/bin/ionice
NICE=/usr/bin/nice
RESTIC=/usr/bin/restic
RCLONE=/usr/bin/rclone

# parametri restic
HOSTNAME=<name-of-server>
REMOTE_RCLONE=<myremote>
PATH_RCLONE=/bck-server/restic
LOG=/tmp/$DATA-$RANDOM.txt
# da trovare con --- df -Th | grep "^/dev"
FILESYSTEM="/ /boot"

# parametri mysqldump
MYSQL_USER=root
MYSQL_PASSWORD=<my-root-password>
MYSQL_HOST=127.0.0.1
MYSQL_PORT=3306
MYSQL_BACKUP_DIR=/var/lib/mysql

# parametri mail e smtp
SMTP=<my-smtp>
PORTA=587
USER=<mymail@gmail.com>
PASSWORD=<password-for-specific-app>
DESTINATARIO=<mymail@gmail.com>

### non toccare dopo
SECONDS=0
echo "...---...---...--- Start backup" $DATA | tee -a $LOG
echo "...---...---...--- Executing Restic Database Backup" | tee -a $LOG
$IONICE -c3 $NICE -n19 $MYSQLDUMP --user=$MYSQL_USER --password=$MYSQL_PASSWORD -P $MYSQL_PORT -h $MYSQL_HOST --routines --triggers --events --quick --single-transaction --all-databases | $BZIP2 > $MYSQL_BACKUP_DIR/$DATA-restic-alldb-backup.sql.bz2
if [ $? -ne 0 ]
then
    echo "...---...---...--- Restic Database Backup Failed" | tee -a $LOG
	echo "...---...---...--- Stop backup" $DATA | tee -a $LOG
	$SWAKS --to $DESTINATARIO -s $SMTP:$PORTA -tls --from $USER -au $USER -ap $PASSWORD --header "Subject: Errore Restic backup databases "$HOSTNAME"" --body "Errore Restic backup databases "$HOSTNAME"" --attach $LOG
	rm -rf $LOG
    exit 2
else
    echo "...---...---...--- Restic Database Backup Successful" $DATA | tee -a $LOG
    sync;sync
	echo "...---...---...--- Restic inizio Backup" $DATA | tee -a $LOG
	$RESTIC -r rclone:$REMOTE_RCLONE:$PATH_RCLONE --password-file $RESTIC_PW --verbose unlock | tee -a $LOG
	$IONICE -c3 $NICE -n19 $RESTIC -r rclone:$REMOTE_RCLONE:$PATH_RCLONE --password-file $RESTIC_PW --verbose backup --one-file-system $FILESYSTEM | tee -a $LOG
	if [ $? -ne 0 ]
	then
	echo "...---...---...--- Restic Backup Failed" | tee -a $LOG
	echo "...---...---...--- Stop backup" $(date +"%Y-%m-%d--%H-%M") | tee -a $LOG
	$SWAKS --to $DESTINATARIO -s $SMTP:$PORTA -tls --from $USER -au $USER -ap $PASSWORD --header "Subject: Errore Restic backup "$HOSTNAME"" --body "Errore Restic backup "$HOSTNAME"" --attach $LOG
	rm -rf $MYSQL_BACKUP_DIR/$DATA-restic-alldb-backup.sql.bz2
	rm -rf $LOG
	exit 2
	else
	echo "...---...---...--- Restic Backup finito, inizio manutenzione" $(date +"%Y-%m-%d--%H-%M") | tee -a $LOG
	$RESTIC -r rclone:$REMOTE_RCLONE:$PATH_RCLONE --password-file $RESTIC_PW --verbose unlock | tee -a $LOG
	echo "...---...---...--- Restic inizio forget" $(date +"%Y-%m-%d--%H-%M") | tee -a $LOG
	$RESTIC -r rclone:$REMOTE_RCLONE:$PATH_RCLONE --password-file $RESTIC_PW --verbose forget --keep-hourly 24 --keep-daily 7 --keep-weekly 4 --keep-monthly 12 --keep-yearly 1 | tee -a $LOG
	echo "...---...---...--- Restic inizio prune" $(date +"%Y-%m-%d--%H-%M") | tee -a $LOG
	$RESTIC -r rclone:$REMOTE_RCLONE:$PATH_RCLONE --password-file $RESTIC_PW --verbose prune | tee -a $LOG
	echo "...---...---...--- Restic inizio check" $(date +"%Y-%m-%d--%H-%M") | tee -a $LOG
	$RESTIC -r rclone:$REMOTE_RCLONE:$PATH_RCLONE --password-file $RESTIC_PW --verbose check | tee -a $LOG
	echo "...---...---...--- Manutenzione finita" $(date +"%Y-%m-%d--%H-%M") | tee -a $LOG
	duration=$SECONDS
	echo "...---...---...--- Tempo totale script - $(($duration /3600)) hours $(($duration %3600/60)) minutes and $(($duration % 60)) seconds." | tee -a $LOG
	$SWAKS --to $DESTINATARIO -s $SMTP:$PORTA -tls --from $USER -au $USER -ap $PASSWORD --header "Subject: ok Restic backup "$HOSTNAME"" --body "ok Restic backup "$HOSTNAME"" --attach $LOG
	rm -rf $MYSQL_BACKUP_DIR/$DATA-restic-alldb-backup.sql.bz2
	rm -rf $LOG
	fi
fi
exit
# crontab -e
# # restic backup quotidiano
# 30 5,19 * * * bash -l -c 'bash /root/restic_script.bash' >/dev/null 2>&1

i use

set -o pipefail

because i read that mysqldump and gzip return always ok without this command.

i know is not necessary check and prune every day but is better run once a week or is the same?
i don’t have problem with bandwidth, is only to have a clean and light backup

if you is a valid script when i have ok can stay safe?
thank you
Alex

Personally I think it’s a bit too much code to look over. And I always recommend that you use english in code, comments and documentation - that way others who want to help you with it actually know what they’re reading. When it’s in a different language people are generally less inclined to even try reading the code etc.

In general I don’t think you need to prune every day or perhaps even every week. Since restic 0.12.0 prune is a lot faster, so you could do it if you want, but if you aren’t running short on disk space why not run it less often. Every week or every second week maybe? There’s not really any right or wrong there.

Check on the other hand is nice to run a bit more often if it doesn’t cost you anything, so why not run that every day or at least every week. You should also now and then run check with --read-data but unless you have the resources for it (it will download all of your repo as part of checking all the data) I would probably do that manually. But it depends on your setup in general.

I can’t say how “valid” your script is, I haven’t read through it.

sorry for delay.
ok thank you for reply. i will check and comment in enghlish, sorry but i copy paste without reread…

ok so prune and check is not necessary every time.

now i’m testing on a webserver with 120gb local disk used and rclone (google drive) as target.

i run every day (twice) and total time of script is about 15min…

so for my peace now i run prune adn check every time

thank you againg

1 Like