proxmox-backup/templates/brain_backup.j2
2024-12-24 15:33:59 +01:00

112 lines
3.9 KiB
Django/Jinja

# {{ ansible_managed }}
#!/bin/bash
[ ! -f /usr/bin/proxmox-backup-client ] && echo "proxmox-backup-client is not installed: exiting." && exit 1
today=`date +%Y-%m-%d`
backup_dir={{ backup_dir }}
backup_local_retention={{ backup_local_retention }}
### PBS ####
PBS_RATE={{ pbs_rate | default('60000000') }}
export PBS_FINGERPRINT="{{ pbs_fingerprint }}"
export PBS_PASSWORD="{{ pbs_password }}"
export PBS_USER="{{ pbs_user }}"
export PBS_SERVER="{{ pbs_server }}"
export PBS_DATASTORE="{{ pbs_datastore }}"
export PBS_REPOSITORY="${PBS_USER}@${PBS_SERVER}:${PBS_DATASTORE}"
export PBS_LOG="{{ PBS_LOG_LEVEL | default('error') }}"
## PBS login ##
/usr/bin/proxmox-backup-client login
if [ $? -ne 0 ]; then
echo "Error on PBS login - exiting."
exit 1
fi
## end of PBS login ##
## system ##
mkdir -p $backup_dir/$today/system
/usr/bin/tar cfz $backup_dir/$today/system/crontab.tgz -C /var/spool/cron/ crontabs/
/usr/bin/dpkg -l > $backup_dir/$today/system/dpkg.txt
[ -f /sbin/iptables ] && /sbin/iptables -L -n > $backup_dir/$today/system/firewall.txt
[ -f /sbin/iptables ] && /sbin/iptables -L -n -t nat > $backup_dir/$today/system/firewall_nat.txt
[ -f /sbin/ip6tables ] && /sbin/ip6tables -L -n > $backup_dir/$today/system/firewall6.txt
[ -f /sbin/ip6tables ] && /sbin/ip6tables -L -n -t nat > $backup_dir/$today/system/firewall6_nat.txt
[ -f /usr/bin/pstree ] && /usr/bin/pstree > $backup_dir/$today/system/pstree.txt
/bin/ps faux > $backup_dir/$today/system/ps.txt
/bin/systemctl list-units > $backup_dir/$today/system/systemctl_unit.txt
/usr/bin/getent passwd > $backup_dir/$today/system/users.txt
[ -f /usr/bin/pvs ] && /usr/sbin/pvs > $backup_dir/$today/system/pvs.txt
[ -f /usr/bin/vgs ] && /usr/sbin/vgs > $backup_dir/$today/system/vgs.txt
[ -f /usr/bin/lvs ] &&/ usr/sbin/lvs > $backup_dir/$today/system/lvs.txt
for dev in /sys/block/*; do
dev=$(basename $dev)
if test -b /dev/$dev && file -s /dev/$dev | egrep -q 'partition table|boot sector'; then
/usr/sbin/sfdisk -d /dev/$dev >> $backup_dir/$today/system/partition-table_$dev.txt
fi
done
## end of system ##
{% if backup_mysql_enabled is sameas true %}
## mysql ##
mysql_databases=`/usr/bin/mysql -e "SHOW DATABASES;" | grep -Ev "(Database|information_schema|performance_schema)"`
mkdir -p $backup_dir/$today/mysql
for db in $mysql_databases;
do mysqldump -e -q -Q --lock-tables $db | gzip -c > $backup_dir/$today/mysql/dump_$db.sql.gz;
done
## end of mysql ##
{% endif %}
{% if backup_pgsql_enabled is sameas true %}
## postgresql ##
pg_port={{ backup_pg_port | default('5432') }}
pg_databases=`sudo su - postgres -c "/usr/bin/psql -p $pg_port -t -c 'SELECT datname FROM pg_database'"|grep -v "template0"`
mkdir -p $backup_dir/$today/postgresql
chown -R postgres: $backup_dir/$today/postgresql
for db in $pg_databases;
do sudo su - postgres -c "/usr/bin/pg_dump -p $pg_port -Z9 -Fc -b -f $backup_dir/$today/postgresql/dump_$db.sql $db";
done
sudo su - postgres -c "/usr/bin/pg_dumpall --roles-only > $backup_dir/$today/postgresql/role.sql"
## end of postresql ##
{% endif %}
# purge old backups
find $backup_dir -type d -ctime +$backup_local_retention -exec rm -rf {} \;
## send to PBS ##
/usr/bin/proxmox-backup-client backup slash.pxar:/ \
--backup-id {{ backup_host_id | default({{ ansible_hostname }}) }} \
{% if backup_path_include is defined %}
{% for item in backup_path_include %}
{% if item.fs_mount is defined %}--include-dev {{ item.path }} \{% endif %}
{% endfor %}
{% endif %}
{% if backup_exclude is defined %}
{% for item in backup_exclude %}
--exclude {{ item }} \
{% endfor %}
{% endif %}
{% if backup_pgsql_enabled is sameas true %}
postgresql.pxar:$backup_dir/$today/postgresql \
{% endif %}
{% if backup_mysql_enabled is sameas true %}
mysql.pxar:$backup_dir/$today/mysql \
{% endif %}
--rate ${PBS_RATE}
## end of send to PBS ##
## PBS logout ##
# not working at this time, script end with "Error: $XDG_RUNTIME_DIR must be set"
#/usr/bin/proxmox-backup-client logout
## end of PBS logout ##