#!/bin/bash #Autobackup script for mastodo.ru by leca@foxarmy.org #The directory where all of backups are stored backup_dir="/backups/" #Directories or files to make a back up of directories=("/home/mastodon" "/var/lib/redis/dump.rdb") #Empty array for writing names of archives e.g. /home/mastodon -> mastodon.tar names=() #PostgreSQL databases to make a back up of databases=("mastodon_production") #Determining a unix time to create backup dir. in /backups date=$(date +"%s") #This program must be ran with root privileges because its sometimes reading sensetive information and have to has access to database if [ $UID != "0" ]; then echo "You must run this script as root." exit 1; fi #Creating necessary directories mkdir -p "${backup_dir}${date}" #the main body of backup #loop that takes every directory from an array and backing it up for name in ${directories[@]}; do IFS="/" temp="${name}" read -rasplitIFS<<< "${temp}" len=${#splitIFS[@]} names+=(${splitIFS[$len-1]}) IFS='' done #Loop that goes through names and backing them up length=${#directories[@]} i=0 while [ $i -lt $length ]; do tar cf "${backup_dir}${date}/${names[i]}.tar" ${directories[i]} i=$(( $i + 1 )) done #Loop that goes through databases and backing them up for db in ${databases[@]}; do sudo -iu postgres pg_dump -Fc "${db}" > "${backup_dir}${date}/${db}.dump" done #Executing a script that manages backups cd "${backup_dir}" ./backup_manager.sh #NOTE: to restore a postgresql database from its dump file: pg_restore -d postgres --clean --create "backup.dump"