scripts/backup/autobackup.sh

93 lines
3.1 KiB
Bash
Raw Normal View History

2023-02-04 06:33:08 +03:00
#!/bin/bash
now=$(date +"%s")
backup_dir="/backup/$now"
if [ $(whoami) != root ]; then
echo "You must run it as sudo."
exit 1
fi
log() {
echo "[$(date)] $*"
}
backup_db() {
sudo -iu postgres pg_dump -Fc "$1" > "${backup_dir}/${1}.dump"
}
backup_dir() {
tar cpf "${backup_dir}/${1}.tar" "$2"
}
checksum() {
client=$(md5sum "${backup_dir}/$1")
clientSUM=${client%' '*}
server=$(sudo -iu foxbackup ssh -p2442 42.42.42.2 "md5sum /mnt/foxbackups/backup1/$now/$1")
serverSUM=${server%' '*}
if [[ $clientSUM == $serverSUM ]]; then
echo "[$(date)] Hash-sum of $1 on the client and on the server are equal. "
else
echo "[$(date)] WARNING!!! HASH-SUM $1 ON THE CLIENT AND ON THE SERVER ARE NOT EQUAL!"
echo "WARNIG!!! HASH-SUM OF $1 ON THE CLIENT AND ON THE SERVER ARE NOT EQUAL!" >> /etc/motd
fi
}
startBackup() {
#Start
mkdir -p "${backup_dir}"
log "Starting backup"
#Backing up a directories
log "Backing up /opt..."
backup_dir opt /opt
log "/opt is backed up, starting to backup nextcloud..."
backup_dir nextcloud /var/www/nextcloud
log "Nextcloud is backed up, starting to backup game servers..."
backup_dir servers /home/servers
log "Servers are backed up, starting to backup mailbox."
backup_dir mail /var/mail
log "Mail are backed up, starting to backup peertube"
backup_dir peertube /var/www/peertube
log "Peertube are backed up, starting to backup DBs..."
#Backing up a DBs
log "Backing up synapse DB"
backup_db synapse
log "Synapse DB is backed up, backing up nextcloud DB..."
backup_db nextclouddb
log "Nextcloud DB is backed up, backing up pleroma DB..."
backup_db pleroma
log "Pleroma DB is backed up, backing up peerube DB..."
backup_db peertube_prod
log "Peertube DB is backed up, compiling all to one and sending to pomoika..."
#Sending to backup server
tar cvf - "${backup_dir}" | sudo -iu foxbackup ssh -p2442 foxbackup@42.42.42.2 "cd backup; tar xf -; /home/foxbackup/autobackup.sh"
#Check md5sum
echo "[$(date)] Done! All data on pomoika! Starting to check the md5sum..."
log "Checking opt sum"
checksum opt.tar
log "Checking peertube sum"
checksum peertube.tar
log "Checking nextcloud sum"
checksum nextcloud.tar
log "Checking servers sum"
checksum servers.tar
log "Checking mail sum"
checksum mail.tar
log "Checking mail_home sum"
checksum mail_home.tar
log "Checking synapse sum"
checksum synapse.dump
log "Checking nextclouddb sum"
checksum nextclouddb.dump
log "Checking pleroma sum"
checksum pleroma.dump
log "Checking peertube sum"
checksum peertube.dump
echo "Checked the md5sum. See above to errors."
}
startBackup
### to restore
#
#pg_restore -d postgres --clean --create "file backup"