Skip to content

Commit 6d9246e

Browse files
committed
sys backup integration
1 parent b386f8e commit 6d9246e

File tree

1 file changed

+120
-9
lines changed

1 file changed

+120
-9
lines changed

bin/v_backup_sys_user

Lines changed: 120 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,6 @@ tmpdir=$(mktemp -p $V_TMP -d)
4747
# Prinitng status
4848
if [ -z "$output" ]; then
4949
echo "$(date +%m-%d-%y" "%H:%m:%S) System backup for user $user"
50-
echo
51-
echo "VESTA VERSION $VERSION"
52-
echo "BACKUP VERSION 1.0"
5350
echo "TMPDIR is $tmpdir"
5451
echo
5552
fi
@@ -300,7 +297,10 @@ then
300297
if [ -z "$output" ]; then
301298
echo -e "\t$(date +%H:%m:%S) system cron"
302299
fi
303-
cp /var/spool/cron/$user $tmpdir/cron/
300+
301+
if [ -e "/var/spool/cron/$user" ]; then
302+
cp /var/spool/cron/$user $tmpdir/cron/
303+
fi
304304

305305
if [ -z "$output" ]; then
306306
echo
@@ -375,23 +375,134 @@ if [ -z "$output" ]; then
375375
echo
376376
fi
377377

378-
# Move tmp backup to local storage
379-
if [ "$BACKUP_SYSTEM" = 'local' ]; then
378+
# Defining local storage function
379+
local_backup(){
380380
if [ -z "$output" ]; then
381-
echo "ARCHIVE $V_BACKUP/$user.$V_DATE.tar"
381+
echo "-- STORAGE --"
382+
echo -e "\t$(date +%H:%m:%S) ARCHIVE $V_BACKUP/$user.$V_DATE.tar"
382383
fi
383384

384385
# Checking retention
385-
check_ret=$()
386+
archives=$(ls -lrt $V_BACKUP/ |awk '{print $9}' |grep "^$user.")
387+
archives_q=$(echo "$archives" |wc -l)
388+
if [ "$archives_q" -ge "$backups" ]; then
389+
archives_r=$((archives_q - backups))
390+
for archive in $(echo "$archives" | head -n $archives_r); do
391+
# Removing old archives
392+
if [ -z "$output" ]; then
393+
echo -e "\tRemoving old $archive"
394+
fi
395+
rm -f $V_BACKUP/$archive
396+
done
397+
fi
386398

399+
# Creating final tarball
387400
cd $tmpdir
388401
tar -cf $V_BACKUP/$user.$V_DATE.tar .
389-
fi
402+
localbackup='yes'
403+
404+
if [ -z "$output" ]; then
405+
echo
406+
fi
407+
}
408+
409+
410+
# Defining ftp command function
411+
ftpc() {
412+
ftp -n $HOST $PORT <<EOF
413+
quote USER $USER
414+
quote PASS $PASSWORD
415+
binary
416+
cd $BPATH
417+
$1
418+
quit
419+
EOF
420+
}
421+
422+
# Defining ftp storage function
423+
ftp_backup(){
424+
if [ -z "$output" ]; then
425+
echo "-- FTP --"
426+
fi
427+
428+
# Checking config
429+
if [ -e "$V_CONF/backup.conf" ]; then
430+
ftphost_str=$(grep "TYPE='FTP'" $V_CONF/backup.conf |head -n 1)
431+
fi
432+
433+
# Parsing config values
434+
if [ ! -z "$ftphost_str" ]; then
435+
for key in $ftphost_str; do
436+
eval ${key%%=*}=${key#*=}
437+
done
438+
else
439+
echo "Error: Parsing error"
440+
log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
441+
exit $E_PARSE_ERROR
442+
fi
390443

444+
# Debug info
445+
if [ -z "$output" ]; then
446+
echo -e "\t$(date +%H:%m:%S) ftp://$USER@$HOST$BPATH/$user.$V_DATE.tar"
447+
fi
448+
449+
# Checking ftp permission
450+
ftmpdir=$(mktemp -u -p $BPATH)
451+
command="mkdir $ftmpdir
452+
ls $ftmpdir
453+
rm $ftmpdir"
454+
if [ ! -z "$(ftpc "$command")" ] ; then
455+
echo "Error: FTP error"
456+
log_event 'debug' "$E_FTP_ERROR $V_EVENT"
457+
exit $E_FTP_ERROR
458+
fi
459+
460+
# Checking retention
461+
archives=$(ftpc "ls" |awk '{print $9}' |grep "^$user.")
462+
archives_q=$(echo "$archives" | wc -l)
463+
if [ "$archives_q" -ge "$backups" ]; then
464+
# Removing old backups
465+
archives_r=$((archives_q - backups))
466+
for archive in $(echo "$archives" | tail -n $archives_r); do
467+
if [ -z "$output" ]; then
468+
echo -e "\tRemoving old $archive"
469+
fi
470+
ftpc "delete $archive"
471+
done
472+
fi
473+
474+
# Uploading backup archive
475+
if [ "$localbackup" = 'yes' ]; then
476+
cd $V_BACKUP
477+
ftpc "put $user.$V_DATE.tar"
478+
else
479+
cd $tmpdir
480+
tar -cf $V_TMP/$user.$V_DATE.tar .
481+
cd $V_TMP/
482+
ftpc "put $user.$V_DATE.tar"
483+
rm -f $user.$V_DATE.tar
484+
fi
485+
486+
if [ -z "$output" ]; then
487+
echo
488+
fi
489+
}
490+
491+
# Switching on backup system types
492+
for backup_type in $(echo -e "${BACKUP_SYSTEM//,/\n}"); do
493+
case $backup_type in
494+
local) local_backup ;;
495+
ftp) ftp_backup ;;
496+
esac
497+
done
498+
499+
# Removing tmpdir
391500
cd /
392501
rm -rf $tmpdir
393502

394503
if [ -z "$output" ]; then
504+
echo "$(date +'%m-%d-%y %H:%m:%S')"
505+
echo
395506
echo
396507
fi
397508

0 commit comments

Comments
 (0)