Skip to content

Commit

Permalink
sauvegardes proxmox
Browse files Browse the repository at this point in the history
fix #2437
  • Loading branch information
dgeo committed Sep 17, 2024
1 parent 097e36b commit fdc3032
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 4 deletions.
22 changes: 22 additions & 0 deletions lib/save_one.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ RSYNC_PORT=${RSYNC_PORT:-42873}
RSYNC_DIRECT="NO"
SAV_JAILS=${SAV_JAILS:-NO}
SAV_DOWN_JAILS=${SAV_DOWN_JAILS:-NO}
SAV_PROXMOX=${SAV_PROXMOX:-NO}
CANSKIP=${CANSKIP:-0}

. $mydir/lib/savscript.inc.sh
Expand Down Expand Up @@ -77,6 +78,27 @@ if init_srv $DEST; then
now_exclude_zfs ${IORIGIN%%/releases*}/releases
now_exclude_zfs ${IORIGIN%%/releases*}/download
fi

# PROXMOX part
if [ "$SAV_PROXMOX" = "YES" ] && [ -n "$PVECLUSTER" ]; then
PVEZFSDEST="${SAVZFSBASE}/$PVECLUSTER"
PVEDESTDIR="$(zfs list -Homountpoint $PVEZFSDEST 2>/dev/null || ( zfs create $PVEZFSDEST && zfs list -Homountpoint $PVEZFSDEST ) )"
for lxc in $(echo "$PVELXCS"); do
syslogue "debug" "($NAME) get PVE LXC $lxc"
get_proxmox_lxc $lxc
[ "$SNAP_AFTER" = "YES" ] && justdoit snapshot_dest $PVEZFSDEST/$lxc
done
for qemu in $(echo "$PVEQMS"); do
syslogue "debug" "($NAME) get PVE QEMU $qemu"
get_proxmox_qemu $qemu
[ "$SNAP_AFTER" = "YES" ] && justdoit snapshot_dest $PVEZFSDEST/$qemu
done
for storage in $(echo "$PVESTORAGES" | grep ' [^$]'); do
syslogue "debug" "($NAME) now exclude ${storage#*|}"
now_exclude_zfs ${storage#* }
done
fi

# FULL ZFS SCENARIO
if [ "$FULLZFS" = "YES" ]; then
# tout est en ZFS: cool :)
Expand Down
63 changes: 60 additions & 3 deletions lib/savscript.inc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,15 @@ srvinfos=$DESTDIR.infos
# - SYSVER (uname -r)
# - FSLIST (liste de fstype:/montage)
# - RSYNC_SRV_PID si un demon rsync a pu etre lance sur $RSYNC_PORT
# - ZFSFSES (zfs filesystems)
# - ZPOOLS (pools zfs)
# - ZFSSLASH (racine ZFS si 'legacy')
# si FreeBSD:
# - JAILS (liste de repertoires)
# - ZPOOLS (pools zfs)
# - ZFSSLASH (racine ZFS si 'legacy')
# - ZFSFSES (zfs filesystems)
# si Linux/Proxmox:
# - PVESTORAGES (liste des storages *locaux*)
# - PVELXCS (liste des containers LXC avec leurs disques)
# - PVEQMS (liste des vm's qemu avec leurs disques)
init_srv() {
if fping -q $DEST; then
test -f $srvinfos && mv $srvinfos $srvinfos.last
Expand Down Expand Up @@ -203,6 +207,16 @@ if [ \$(mount -t zfs | wc -l) -gt 0 ]; then
else
echo ZFSFSES=\\\"\$(zfs list -H -t filesystem -o name,mountpoint | awk '{printf(\"%s|%s\\\n\",\$2,\$1);}')\\\";
fi
fi
if [ \"\$(uname -s)\" = \"Linux\" ] && [ -e \"/etc/pve/local\" ]; then
echo PVECLUSTER=\\\"\$(pvecm status|grep ^Name: | awk '{print \$NF}')\\\"
echo PVESTORAGES=\\\"\$(pvesm status | awk '{if(\$3==\"active\"){print \$1}}' | while read s; do pvesh get /storage/\$s --noborder --noheader | awk 'BEGIN{want=0;zpool=\"\";dpath=\"\"}/^content.*images/{want=1;}/^pool/{zpool=\$2;}/^shared.*1$/{want=0;}/^(path|mountpoint)/{dpath=\$2; want=1;}END{if(want==1){printf(\"'\$s':%s %s\\\n\",dpath,zpool)}}'; done)\\\";
if [ -x /usr/sbin/pct ]; then
echo PVELXCS=\\\"\$(pct list | grep ' running *[^$]' | awk '{if(\$2==\"running\"){print \$1}}')\\\";
fi
if [ -x /usr/sbin/qm ]; then
echo PVEQMS=\\\"\$(qm list | grep ' running *[0-9]' | awk '{ print \$1 }')\\\";
fi
fi" > $srvinfos 2> $TRACES/$NAME.init_srv
. $srvinfos >> $TRACES/$NAME.init_srv 2>&1
else
Expand Down Expand Up @@ -736,3 +750,46 @@ debug "jail fs curjail=$curjail curjaildir=$curjaildir curjailsrc=$curjailsrc"
debug "JAIL $curjail: END($(($ret+${cret})))"
}

## PROXMOX
# liste les stockages locaux
resolve_proxmox_storage() {
storage=$1
# dir
d=$(echo "$PVESTORAGES" | grep "${1%:*}" | cut -d' ' -f1 | cut -d: -f2)
# zfs source if any
z=$(echo "$PVESTORAGES" | grep "${1%:*}" | cut -d' ' -f2)
[ -n "$d" ] || return 1
echo "${d}|${z}"
}
# sauvegarde d'un LXC
get_proxmox_lxc() {
lxc_id=$1
init_zfs_dest none ${PVEDESTDIR}/${lxc_id} ${PVEZFSDEST}/${lxc_id}
doit $REMOTE_COMMAND $DEST "pct config $lxc_id" > ${PVEDESTDIR}/${lxc_id}/config
echo $DEST > ${PVEDESTDIR}/${lxc_id}/host
for disk in $(awk '/^(rootfs|mp[0-9]+):/{gsub(",.*","");if($2!="none"){printf("%s",$2);}}' ${PVEDESTDIR}/${lxc_id}/config); do
if storage=$(resolve_proxmox_storage $disk); then
if [ -n "${storage#*|}" ]; then
get_zfs ${storage%|*}/${disk#*:} ${PVEZFSDEST}/${lxc_id}/${disk#*:} ${storage#*|}/${disk#*:}
else
get_fs ${storage%|*}/${disk#*:} ${PVEDESTDIR}/${lxc_id}/${disk#*:}
fi
fi
done
}
# sauvegarde d'une VM qemu
get_proxmox_qemu() {
qemu_id=$1
init_zfs_dest none ${PVEDESTDIR}/${qemu_id} ${PVEZFSDEST}/${qemu_id}
doit $REMOTE_COMMAND $DEST "qm config $qemu_id" > ${PVEDESTDIR}/${qemu_id}/config
echo $DEST > ${PVEDESTDIR}/${qemu_id}/host
for disk in $(awk '/^(virtio|scsi|ide)[0-9]+:/{gsub(",.*","");if($2!="none"){printf("%s",$2);}}' ${PVEDESTDIR}/${qemu_id}/config); do
if storage=$(resolve_proxmox_storage $disk); then
if [ -n "${storage#*|}" ]; then
get_zfs ${storage%|*}/${disk#*:} ${PVEZFSDEST}/${qemu_id}/${disk#*:} ${storage#*|}/${disk#*:}
else
get_fs ${storage%|*}/${disk#*:} ${PVEDESTDIR}/${qemu_id}/${disk#*:}
fi
fi
done
}
2 changes: 2 additions & 0 deletions machines.d/conf.template
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ SAV_JAILS="%%SAVJAILS%%"
# lance un serveur rsync sur la destination au lieu de passer par ssh
# default: YES (essaye, fallback to ssh)
#RSYNC_DIRECT="NO"
# sauvegardes des vms proxmox
SAV_PROXMOX="%%SAVPROXMOX%%"
5 changes: 4 additions & 1 deletion tools/nouvelle_machine.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ savpath=$(realpath "$(dirname $0)/..")
prepend=",no-agent-forwarding,no-port-forwarding,no-X11-forwarding,no-user-rc,no-pty"

SAVJAILS="NO"
SAVPROXMOX="NO"

if ! ssh -oPasswordAuthentication=no -oIdentitiesOnly=yes -oIdentityFile=$SSH_KEY -oKbdInteractiveDevices=none root@$1 "echo connexion ssh ok"; then
echo "pousse la cle sur ${1}:"
Expand All @@ -28,6 +29,7 @@ case "$SYSTEM" in
;;
esac
FSLIST="`for fst in ext3 ext4 ext2 btrfs; do df -t $fst; done | tail -n +2 | awk '\''{print $6}'\''`"
PVE=$(stat --printf=%N /etc/pve/local | awk '\''{print $NF}'\'')
;;
"FreeBSD")
EXCLUDES=""
Expand All @@ -53,6 +55,7 @@ echo MYNAME="`hostname -s`"')

echo SYSTEM=$SYSTEM
[ -n "$JAILS" ] && SAVJAILS="YES"
[ -n "$PVE" ] && SAVPROXMOX="YES"
echo EXCLUDES="$EXCLUDES"
echo RSYNC=$RSYNC
MYFQDN=$(getent hosts $MYIP | awk '{print $2}')
Expand All @@ -64,7 +67,7 @@ if [ -z "$MYNAME" -o -z "$MYIP" -o -z "$MYFQDN" -o -z "$RSYNC" -o -z "$SYSTEM" ]
exit 1
fi

sed -E 's/%%NAME%%/'$MYNAME'/; s/%%FQDN%%/'$MYFQDN'/; s@%%EXCLUDES%%@'"$EXCLUDES"'@; s@%%SAVJAILS%%@'$SAVJAILS'@;' $savpath/machines.d/conf.template > /tmp/$MYNAME.conf
sed -E 's/%%NAME%%/'$MYNAME'/; s/%%FQDN%%/'$MYFQDN'/; s@%%EXCLUDES%%@'"$EXCLUDES"'@; s@%%SAVJAILS%%@'$SAVJAILS'@; s@%%SAVPROXMOX%%@'$SAVPROXMOX'@;' $savpath/machines.d/conf.template > /tmp/$MYNAME.conf

echo La conf generee:
echo -- "##############################################################################"
Expand Down

0 comments on commit fdc3032

Please sign in to comment.