mirror of
https://github.com/serghey-rodin/vesta.git
synced 2026-03-16 09:00:35 +01:00
511 lines
13 KiB
Bash
Executable File
511 lines
13 KiB
Bash
Executable File
#!/bin/bash
|
|
# info: backup system user with all its objects
|
|
# options: user
|
|
#
|
|
# The call is used for backing up user with all its domains and databases.
|
|
|
|
|
|
#----------------------------------------------------------#
|
|
# Variable&Function #
|
|
#----------------------------------------------------------#
|
|
|
|
# Argument defenition
|
|
user=$1
|
|
|
|
# Importing variables
|
|
source $VESTA/conf/vars.conf
|
|
source $V_CONF/vesta.conf
|
|
source $V_FUNC/shared.func
|
|
source $V_FUNC/domain.func
|
|
source $V_FUNC/db.func
|
|
|
|
|
|
#----------------------------------------------------------#
|
|
# Verifications #
|
|
#----------------------------------------------------------#
|
|
|
|
# Checking arg number
|
|
check_args '1' "$#" 'user'
|
|
|
|
# Checking argument format
|
|
format_validation 'user'
|
|
|
|
# Checking backup system is enabled
|
|
is_system_enabled 'backup'
|
|
|
|
# Checking user
|
|
is_user_valid
|
|
|
|
# Checking user backups
|
|
is_backup_enabled
|
|
|
|
# Checking load averages
|
|
la=$(cat /proc/loadavg |cut -f 1 -d ' '|cut -f 1 -d '.')
|
|
i=0
|
|
while [ "$la" -ge "$V_BACKUP_LA_LIMIT" ]; do
|
|
echo "$(date "+%F %T") Load Average $la"
|
|
echo
|
|
|
|
sleep 60
|
|
|
|
if [ "$i" -ge "15" ]; then
|
|
echo "Error: LA is too high"
|
|
log_event 'debug' "$E_LA $V_EVENT"
|
|
exit $E_LA
|
|
fi
|
|
|
|
(( ++i))
|
|
done
|
|
|
|
|
|
#----------------------------------------------------------#
|
|
# Action #
|
|
#----------------------------------------------------------#
|
|
|
|
# Get current time
|
|
start_time=$(date '+%s')
|
|
|
|
# Creating temporary random directory
|
|
tmpdir=$(mktemp -p $V_BACKUP -d)
|
|
|
|
# Prinitng status
|
|
echo "$(date "+%F %T") System backup for user $user"
|
|
echo "TMPDIR is $tmpdir"
|
|
echo
|
|
|
|
# Addding backup and vesta version
|
|
echo "1.0" > $tmpdir/backup_version
|
|
echo "$VERSION" > $tmpdir/vesta_version
|
|
|
|
|
|
|
|
# Vesta
|
|
echo "-- VESTA --"
|
|
vst='yes'
|
|
mkdir $tmpdir/vesta
|
|
|
|
# Backingup vesta configs
|
|
echo -e "$(date "+%F %T") user.conf"
|
|
|
|
cp -r $V_USERS/$user/user.conf $tmpdir/vesta/
|
|
|
|
if [ -e "$V_USERS/$user/billing.log" ]; then
|
|
echo -e "$(date "+%F %T") billing.log"
|
|
cp -r $V_USERS/$user/billing.log $tmpdir/vesta/
|
|
fi
|
|
|
|
if [ -e "$V_USERS/$user/history.log" ]; then
|
|
echo -e "$(date "+%F %T") history.log"
|
|
cp -r $V_USERS/$user/history.log $tmpdir/vesta/
|
|
fi
|
|
|
|
if [ -e "$V_USERS/$user/backup.excludes" ]; then
|
|
echo -e "$(date "+%F %T") backup.excludes"
|
|
cp -r $V_USERS/$user/backup.excludes $tmpdir/vesta/
|
|
fi
|
|
|
|
|
|
echo
|
|
|
|
# PAM
|
|
echo "-- PAM --"
|
|
pam='yes'
|
|
mkdir $tmpdir/pam
|
|
echo -e "$(date "+%F %T") passwd / shadow /group"
|
|
grep "^$user:" /etc/passwd > $tmpdir/pam/passwd
|
|
grep "^$user:" /etc/shadow > $tmpdir/pam/shadow
|
|
grep "^$user:" /etc/group > $tmpdir/pam/group
|
|
echo
|
|
|
|
# Checking excludes
|
|
OLD_IFS="$IFS"
|
|
IFS=$'\n'
|
|
if [ -e "$V_USERS/$user/backup.excludes" ]; then
|
|
echo "-- Excludes --"
|
|
|
|
for exclude in $(cat $V_USERS/$user/backup.excludes); do
|
|
echo -e "$exclude"
|
|
# Indirect variable references (a bit of black magic)
|
|
eval ${exclude%%=*}=${exclude#*=}
|
|
done
|
|
echo
|
|
fi
|
|
IFS="$OLD_IFS"
|
|
|
|
# WEB domains
|
|
if [ ! -z "$WEB_SYSTEM" ] && [ "$WEB_SYSTEM" != 'no' ] && [ "$WEB" != '*' ]
|
|
then
|
|
echo "-- WEB --"
|
|
mkdir $tmpdir/web/
|
|
|
|
# Parsing unsuspeneded domains
|
|
conf="$V_USERS/$user/web.conf"
|
|
field='$DOMAIN'
|
|
search_string='DOMAIN='
|
|
domains=$(dom_clear_search)
|
|
domain_list=''
|
|
|
|
# Cleaning excludes
|
|
for domain in $domains; do
|
|
check_exl=$(echo "$WEB"|grep -w $domain)
|
|
if [ -z "$check_exl" ]; then
|
|
web_list="$web_list $domain"
|
|
fi
|
|
done
|
|
web_list=$(echo "$web_list" | sed -e "s/ */\ /g" -e "s/^ //")
|
|
|
|
for domain in $web_list; do
|
|
echo -e "$(date "+%F %T") $domain"
|
|
# Building directory tree
|
|
mkdir -p $tmpdir/web/$domain/conf
|
|
mkdir -p $tmpdir/web/$domain/ssl
|
|
|
|
# Defining domain variables
|
|
domain_idn=$(idn -t --quiet -a "$domain")
|
|
get_web_domain_values
|
|
|
|
|
|
# Packing data folders
|
|
cd $V_HOME/$user/web/$domain
|
|
tar -cf $tmpdir/web/$domain/$domain.tar \
|
|
public_html public_shtml private document_errors cgi-bin stats
|
|
|
|
# Creating web.config
|
|
cd $tmpdir/web/$domain/
|
|
conf="$V_USERS/$user/web.conf"
|
|
grep "DOMAIN='$domain'" $conf > conf/web.conf
|
|
|
|
# Apache config
|
|
if [ "$WEB_SYSTEM" = 'apache' ]; then
|
|
# Parsing httpd.conf
|
|
tpl_file="$V_WEBTPL/apache_$TPL.tpl"
|
|
conf="$V_HOME/$user/conf/httpd.conf"
|
|
get_web_config_brds
|
|
sed -n "$top_line,$bottom_line p" $conf > conf/httpd.conf
|
|
|
|
# SSL check
|
|
if [ "$SSL" = 'yes' ]; then
|
|
tpl_file="$V_WEBTPL/apache_$TPL.stpl"
|
|
conf="$V_HOME/$user/conf/shttpd.conf"
|
|
get_web_config_brds
|
|
sed -n "$top_line,$bottom_line p" $conf > conf/shttpd.conf
|
|
fi
|
|
fi
|
|
|
|
# Nginx config
|
|
if [ ! -z "$NGINX" ] ; then
|
|
tpl_file="$V_WEBTPL/ngingx_vhost_$NGINX.tpl"
|
|
conf="$V_HOME/$user/conf/nginx.conf"
|
|
get_web_config_brds
|
|
sed -n "$top_line,$bottom_line p" $conf > conf/nginx.conf
|
|
|
|
# SSL check
|
|
if [ "$SSL" = 'yes' ] ; then
|
|
tpl_file="$V_WEBTPL/ngingx_vhost_$NGINX.stpl"
|
|
conf="$V_HOME/$user/conf/snginx.conf"
|
|
get_web_config_brds
|
|
sed -n "$top_line,$bottom_line p" $conf > conf/snginx.conf
|
|
fi
|
|
fi
|
|
|
|
# Suplemental configs
|
|
for sconfig in $(ls $V_HOME/$user/conf/|grep ".$domain.conf"); do
|
|
cp $V_HOME/$user/conf/$sconfig conf/
|
|
done
|
|
|
|
# SSL Certificates
|
|
if [ "$SSL" = 'yes' ] ; then
|
|
cp $V_HOME/$user/conf/ssl.$domain.* ssl/
|
|
fi
|
|
|
|
tar -rf $tmpdir/web/$domain/$domain.tar conf ssl
|
|
mv $tmpdir/web/$domain/$domain.tar $tmpdir/web/
|
|
rm -rf $tmpdir/web/$domain
|
|
|
|
if [ ! -z "$V_BACKUP_GZIP" ]; then
|
|
gzip -$V_BACKUP_GZIP $tmpdir/web/$domain.tar
|
|
fi
|
|
|
|
done
|
|
echo
|
|
fi
|
|
|
|
# DNS domains
|
|
if [ ! -z "$DNS_SYSTEM" ] && [ "$DNS_SYSTEM" != 'no' ] && [ "$DNS" != '*' ]
|
|
then
|
|
echo "-- DNS --"
|
|
mkdir $tmpdir/dns/
|
|
|
|
# Parsing unsuspeneded domains
|
|
conf="$V_USERS/$user/dns.conf"
|
|
field='$DOMAIN'
|
|
search_string='DOMAIN='
|
|
domains=$(dom_clear_search)
|
|
|
|
# Cleaning excludes
|
|
for domain in $domains; do
|
|
check_exl=$(echo "$DNS"|grep -w $domain)
|
|
if [ -z "$check_exl" ]; then
|
|
dns_list="$dns_list $domain"
|
|
fi
|
|
done
|
|
dns_list=$(echo "$dns_list" | sed -e "s/ */\ /g" -e "s/^ //")
|
|
|
|
for domain in $dns_list; do
|
|
echo -e "$(date "+%F %T") $domain"
|
|
# Building directory tree
|
|
mkdir $tmpdir/dns/$domain
|
|
|
|
# Creating dns_domains config
|
|
cd $tmpdir/dns/$domain/
|
|
conf="$V_USERS/$user/dns.conf"
|
|
grep "DOMAIN='$domain'" $conf > dns.conf
|
|
|
|
# Backingup dns recods
|
|
cp $V_USERS/$user/dns/$domain $domain
|
|
cp /etc/namedb/$domain.db $domain.db
|
|
done
|
|
echo
|
|
fi
|
|
|
|
|
|
# Mail domains
|
|
# TBD
|
|
|
|
# DatbaBases
|
|
if [ ! -z "$DB_SYSTEM" ] && [ "$DB_SYSTEM" != 'no' ] && [ "$DB" != '*' ]; then
|
|
echo "-- DB --"
|
|
mkdir $tmpdir/db/
|
|
|
|
# Parsing unsuspeneded domains
|
|
conf="$V_USERS/$user/db.conf"
|
|
field='$DB'
|
|
search_string='DB='
|
|
dbs=$(dom_clear_search)
|
|
|
|
# Cleaning excludes
|
|
for database in $dbs; do
|
|
check_exl=$(echo "$DB"|grep -w $database)
|
|
if [ -z "$check_exl" ]; then
|
|
db_list="$db_list $database"
|
|
fi
|
|
done
|
|
db_list=$(echo "$db_list" | sed -e "s/ */\ /g" -e "s/^ //")
|
|
|
|
for database in $db_list; do
|
|
type=$(get_db_value '$TYPE')
|
|
host=$(get_db_value '$HOST')
|
|
db_user=$(get_db_value '$USER')
|
|
dump="$tmpdir/db/$database.$type.sql"
|
|
grants="$tmpdir/db/$database.$type.$db_user"
|
|
|
|
echo -e "$(date "+%F %T") $database $type"
|
|
|
|
case $type in
|
|
mysql) dump_db_mysql ;;
|
|
pgsql) dump_db_pgsql ;;
|
|
esac
|
|
|
|
if [ ! -z "$V_BACKUP_GZIP" ]; then
|
|
gzip -$V_BACKUP_GZIP $dump
|
|
fi
|
|
|
|
done
|
|
echo
|
|
fi
|
|
|
|
# Cron jobs
|
|
if [ ! -z "$CRON_SYSTEM" ] && [ "$CRON_SYSTEM" != 'no' ] && [ "$CRON" != '*' ]
|
|
then
|
|
echo "-- CRON --"
|
|
mkdir $tmpdir/cron/
|
|
|
|
# Backingup cron recods
|
|
echo -e "$(date "+%F %T") cron.conf"
|
|
cp $V_USERS/$user/cron.conf $tmpdir/cron/
|
|
echo -e "$(date "+%F %T") system cron"
|
|
|
|
if [ -e "/var/spool/cron/$user" ]; then
|
|
cron_list='yes'
|
|
cp /var/spool/cron/$user $tmpdir/cron/
|
|
fi
|
|
echo
|
|
fi
|
|
|
|
# Get backup size
|
|
size="$(du -shm $tmpdir | cut -f 1)"
|
|
|
|
# Get current time
|
|
end_time=$(date '+%s')
|
|
|
|
# Defining local storage function
|
|
local_backup(){
|
|
echo "-- STORAGE --"
|
|
echo -e "$(date "+%F %T") ARCHIVE $V_BACKUP/$user.$V_DATE.tar"
|
|
|
|
# Removing dublicate for this day
|
|
if [ -e "$V_BACKUP/$user.$V_DATE.tar" ]; then
|
|
deprecated="$V_DATE"
|
|
echo -e "Deleting old backup for $V_DATE"
|
|
rm -f $V_BACKUP/$user.$V_DATE.tar
|
|
fi
|
|
|
|
# Checking retention
|
|
backup_list=$(ls -lrt $V_BACKUP/ | awk '{print $9}' |grep "^$user\.")
|
|
backups_count=$(echo "$backup_list" | wc -l)
|
|
|
|
if [ "$BACKUPS" -le "$backups_count" ]; then
|
|
backups_rm_number=$((backups_count - BACKUPS))
|
|
(( ++backups_rm_number))
|
|
|
|
for backup in $(echo "$backup_list" | head -n $backups_rm_number); do
|
|
# Removing old backup
|
|
backup_date=$(echo $backup | sed -e "s/$user.//" -e "s/.tar$//")
|
|
deprecated="$deprecated $backup_date"
|
|
echo -e "\tDeleteing old backup for $backup_date"
|
|
rm -f $V_BACKUP/$backup
|
|
done
|
|
fi
|
|
|
|
# Checking disk space
|
|
disk_usage=$(df $V_BACKUP | awk '{print $5}'|tail -n1|cut -f 1 -d '%')
|
|
if [ "$disk_usage" -ge "$V_BACKUP_DISK_LIMIT" ]; then
|
|
echo "Error: Not enough disk space"
|
|
log_event 'debug' "$E_DISK $V_EVENT"
|
|
exit $E_DISK
|
|
fi
|
|
|
|
# Creating final tarball
|
|
cd $tmpdir
|
|
tar -cf $V_BACKUP/$user.$V_DATE.tar .
|
|
chmod 640 $V_BACKUP/$user.$V_DATE.tar
|
|
chown root:$user $V_BACKUP/$user.$V_DATE.tar
|
|
localbackup='yes'
|
|
echo
|
|
}
|
|
|
|
|
|
# Defining ftp command function
|
|
ftpc() {
|
|
ftp -n $HOST $PORT <<EOF
|
|
quote USER $USERNAME
|
|
quote PASS $PASSWORD
|
|
binary
|
|
cd $BPATH
|
|
$1
|
|
quit
|
|
EOF
|
|
}
|
|
|
|
# Defining ftp storage function
|
|
ftp_backup(){
|
|
echo "-- FTP --"
|
|
|
|
# Checking config
|
|
source $V_CONF/ftp.backup.conf
|
|
if [ -z "$HOST" ] || [ -z "$USERNAME" ] || [ -z "$PASSWORD" ] ||\
|
|
[ -z "$BPATH" ]; then
|
|
echo "Error: Parsing error"
|
|
log_event 'debug' "$E_PARSING $V_EVENT"
|
|
exit $E_PARSING
|
|
fi
|
|
|
|
# Debug info
|
|
echo -ne "$(date "+%F %T") "
|
|
echo -e "ftp://$USERNAME@$HOST$BPATH/$user.$V_DATE.tar"
|
|
|
|
# Checking ftp permission
|
|
ftmpdir=$(mktemp -u -p $BPATH)
|
|
command="mkdir $ftmpdir
|
|
ls $ftmpdir
|
|
rm $ftmpdir"
|
|
if [ ! -z "$(ftpc "$command")" ] ; then
|
|
echo "Error: FTP error"
|
|
log_event 'debug' "$E_FTP $V_EVENT"
|
|
exit $E_FTP
|
|
fi
|
|
|
|
# Checking retention
|
|
backup_list=$(ftpc "ls" |awk '{print $9}' |grep "^$user\.")
|
|
backups_count=$(echo "$backup_list" | wc -l)
|
|
if [ "$backups_count" -ge "$BACKUPS" ]; then
|
|
backups_rm_number=$((backups_count - BACKUPS + 1))
|
|
for backup in $(echo "$backup_list" | head -n $backups_rm_number); do
|
|
backup_date=$(echo $backup | sed -e "s/$user.//" -e "s/.tar$//")
|
|
deprecated="$deprecated $backup"
|
|
echo -e "\tDeleteing old backup for $backup_date"
|
|
ftpc "delete $backup"
|
|
done
|
|
fi
|
|
|
|
# Uploading backup archive
|
|
if [ "$localbackup" = 'yes' ]; then
|
|
cd $V_BACKUP
|
|
ftpc "put $user.$V_DATE.tar"
|
|
else
|
|
cd $tmpdir
|
|
tar -cf $V_BACKUP/$user.$V_DATE.tar .
|
|
cd $V_BACKUP/
|
|
ftpc "put $user.$V_DATE.tar"
|
|
rm -f $user.$V_DATE.tar
|
|
fi
|
|
echo
|
|
}
|
|
|
|
# Switching on backup system types
|
|
for backup_type in $(echo -e "${BACKUP_SYSTEM//,/\n}"); do
|
|
case $backup_type in
|
|
local) local_backup ;;
|
|
ftp) ftp_backup ;;
|
|
esac
|
|
done
|
|
|
|
# Removing tmpdir
|
|
cd /
|
|
rm -rf $tmpdir
|
|
|
|
# Calculation run time
|
|
run_time=$((end_time - start_time))
|
|
run_time=$((run_time / 60))
|
|
current_time=$(date "+%T")
|
|
if [ "$run_time" -lt 1 ]; then
|
|
run_time=1
|
|
fi
|
|
|
|
min=miutes
|
|
if [ "$run_time" -eq 1 ]; then
|
|
min=minute
|
|
fi
|
|
echo "$(date "+%F %T") Backup took $run_time $min"
|
|
echo
|
|
echo
|
|
|
|
|
|
#----------------------------------------------------------#
|
|
# Vesta #
|
|
#----------------------------------------------------------#
|
|
|
|
# Deleting old backup records
|
|
for backup_record in $deprecated; do
|
|
if [ -e "$V_USERS/$user/backup.conf" ]; then
|
|
sed -i "/DATE='$backup_record/d" $V_USERS/$user/backup.conf
|
|
fi
|
|
done
|
|
|
|
# Concatenating string
|
|
backup_str="DATE='$V_DATE' TIME='$current_time' RUNTIME='$run_time'"
|
|
backup_str="$backup_str TYPE='$BACKUP_SYSTEM' SIZE='$size'"
|
|
backup_str="$backup_str VESTA='$vst'"
|
|
backup_str="$backup_str PAM='$pam'"
|
|
backup_str="$backup_str WEB='${web_list// /,}'"
|
|
backup_str="$backup_str DNS='${dns_list// /,}'"
|
|
backup_str="$backup_str MAIL='${mail_list// /,}'"
|
|
backup_str="$backup_str DB='${db_list// /,}'"
|
|
backup_str="$backup_str CRON='$cron_list'"
|
|
echo "$backup_str" >> $V_USERS/$user/backup.conf
|
|
|
|
# Logging
|
|
log_event 'system' "$V_EVENT"
|
|
|
|
exit
|