Added script for remote backups

This commit is contained in:
leca 2023-05-06 02:05:34 +03:00
parent 9aeed416c7
commit fdad332c9e
1 changed files with 79 additions and 0 deletions

View File

@ -0,0 +1,79 @@
#!/bin/bash
files=("/etc/nginx" "/etc/wireguard" "/etc/matrix-synapse" "/opt")
names=()
DBs=("synapse")
now=$(date +"%s")
week_day=$(date +"%A")
dir="/mnt/backups"
backup_dir="${dir}/${now}"
remoteUser="foxbackup"
localUser="foxbackup"
localPostgreUser="postgres"
host="42.42.42.3"
port=2244
if [ $UID != "0" ]; then
echo "You must run it as sudo."
exit 1
fi
remoteExecuce() {
sudo -iu $localUser ssh -p $port $remoteUser@$host "$*"
}
log() {
echo "[$(date)] $*" >> ./autobackup_log.txt
echo "[$(date)] $*"
}
backupDb() {
sudo -iu $localPostgreUser pg_dump -Fc "$1" | sudo -iu $localUser ssh -p $port $remoteUser@$host "pv > \"${backup_dir}/${1}.dump\""
}
backupDir() {
tar cpf - "${2}" | sudo -iu $localUser ssh -p $port $remoteUser@$host "pv > \"${backup_dir}/${1}.tar\""
}
startBackup() {
log "#===#Starting backup#===#"
#if [[ $week_day == "Sunday" ]]; then
# backup_dir="${dir}/${now}_full"
# files+=("/var/www")
# DBs+=("peertube_prod")
#fi
remoteExecuce mkdir -p "${backup_dir}"
for name in ${files[@]}; do
IFS="/"
temp="${name}"
read -rasplitIFS<<< "${temp}"
len=${#splitIFS[@]}
names+=(${splitIFS[$len-1]})
IFS=''
done
length=${#files[@]}
i=0
while [ $i -lt $length ]; do
log "Backing up dir ${files[$i]}"
echo $i
backupDir ${names[$i]} ${files[$i]}
i=$(( $i + 1 ))
done
for db in ${DBs[@]}; do
log "Backing up DB ${db}"
backupDb ${db}
done
log "All is backed up, cleaning up old backups and finishing"
remoteExecuce "${dir}/autobackup.sh"
log "All is done."
}
startBackup
#====how to restore====#
#pg_restore -d postgres --clean --create "file backup"