-
Notifications
You must be signed in to change notification settings - Fork 0
/
prox_config_backup.sh
153 lines (121 loc) · 4.31 KB
/
prox_config_backup.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/bin/bash
# Version 0.2.2 - BETA ! !
# Date 02.20.2020
# Author DerDanilo
# Contributors aboutte, xmirakulix, bootsie123
# set vars
# always exit on error
set -e
# permanent backups directory
# default value can be overridden by setting environment variable before running prox_config_backup.sh
# example: export BACK_DIR="/mnt/pve/media/backup
_bdir=${BACK_DIR:-/mnt/backup}
# number of backups to keep before overriding the oldest one
MAX_BACKUPS=5
# temporary storage directory
_tdir=${TMP_DIR:-/var/tmp}
_tdir=$(mktemp -d $_tdir/proxmox-XXXXXXXX)
function clean_up {
echo "Cleaning up"
rm -rf $_tdir
}
# register the cleanup function to be called on the EXIT signal
trap clean_up EXIT
# Don't change if not required
_now=$(date +%Y-%m-%d.%H.%M.%S)
_HOSTNAME=$(hostname -f)
_filename1="$_tdir/proxmoxetc.$_now.tar"
_filename2="$_tdir/proxmoxpve.$_now.tar"
_filename3="$_tdir/proxmoxroot.$_now.tar"
_filename4="$_tdir/proxmoxcron.$_now.tar"
_filename5="$_tdir/proxmoxvbios.$_now.tar"
_filename6="$_tdir/proxmoxpackages.$_now.list"
_filename7="$_tdir/proxmoxreport.$_now.txt"
_filename_final="$_tdir/proxmox_backup_"$_HOSTNAME"_"$_now".tar.gz"
##########
function description {
clear
cat <<EOF
Proxmox Server Config Backup
Hostname: "$_HOSTNAME"
Timestamp: "$_now"
Files to be saved:
"/etc/*, /var/lib/pve-cluster/*, /root/*, /var/spool/cron/*, /usr/share/kvm/*.vbios"
Backup target:
"$_bdir"
-----------------------------------------------------------------
This script is supposed to backup your node config and not VM
or LXC container data. To backup your instances please use the
built in backup feature or a backup solution that runs within
your instances.
For questions or suggestions please contact me at
https://github.com/DerDanilo/proxmox-stuff
-----------------------------------------------------------------
Hit return to proceed or CTRL-C to abort.
EOF
read dummy
clear
}
function are-we-root-abort-if-not {
if [[ ${EUID} -ne 0 ]] ; then
echo "Aborting because you are not root" ; exit 1
fi
}
function check-num-backups {
if [[ $(ls ${_bdir}/*${_HOSTNAME}*.tar.gz -l | grep ^- | wc -l) -ge $MAX_BACKUPS ]]; then
local oldbackup="$(basename $(ls ${_bdir}/*${_HOSTNAME}*.tar.gz -t | tail -1))"
echo "${_bdir}/${oldbackup}"
rm "${_bdir}/${oldbackup}"
fi
}
function copyfilesystem {
echo "Tar files"
# copy key system files
tar --warning='no-file-ignored' -cvPf "$_filename1" /etc/.
tar --warning='no-file-ignored' -cvPf "$_filename2" /var/lib/pve-cluster/.
tar --warning='no-file-ignored' -cvPf "$_filename3" /root/.
tar --warning='no-file-ignored' -cvPf "$_filename4" /var/spool/cron/.
if [ "$(ls /usr/share/kvm/*.vbios 2>/dev/null)" != "" ] ; then
echo backing up custom video bios...
tar --warning='no-file-ignored' -cvPf "$_filename5" /usr/share/kvm/*.vbios
fi
echo backing up custom video bios...
tar --warning='no-file-ignored' -cvPf "$_filename5" /usr/share/kvm/*.vbios
fi
# copy installed packages list
echo "Copying installed packages list from APT"
apt-mark showmanual | tee "$_filename6"
# copy pvereport output
echo "Copying pvereport output"
pvereport | tee "$_filename7"
}
function compressandarchive {
echo "Compressing files"
# archive the copied system files
tar -cvzPf "$_filename_final" $_tdir/*.{tar,list,txt}
# copy config archive to backup folder
# this may be replaced by scp command to place in remote location
cp $_filename_final $_bdir/
}
function stopservices {
# stop host services
for i in pve-cluster pvedaemon vz qemu-server; do systemctl stop $i ; done
# give them a moment to finish
sleep 10s
}
function startservices {
# restart services
for i in qemu-server vz pvedaemon pve-cluster; do systemctl start $i ; done
# Make sure that all VMs + LXC containers are running
qm startall
}
##########
description
are-we-root-abort-if-not
check-num-backups
# We don't need to stop services, but you can do that if you wish
#stopservices
copyfilesystem
# We don't need to start services if we did not stop them
#startservices
compressandarchive