1
0
mirror of https://github.com/myvesta/vesta.git synced 2025-03-12 04:35:23 -07:00
myvesta/bin/v-add-fs-archive

77 lines
1.6 KiB
Plaintext
Raw Permalink Normal View History

2015-08-20 18:01:27 +03:00
#!/bin/bash
# info: archive directory
2015-11-05 03:10:57 +02:00
# options: USER ARCHIVE SOURCE
2015-08-20 18:01:27 +03:00
#
# The function creates tar archive
user=$1
archive=$2
2016-08-10 14:36:54 +03:00
src0=$3
2015-08-20 18:01:27 +03:00
# Checking arguments
2016-08-10 14:36:54 +03:00
if [ -z "$src0" ]; then
echo "Usage: USER ARCHIVE FILE [FILE_2] [FILE_3] [FILE ...]"
2015-08-20 18:01:27 +03:00
exit 1
fi
# Checking vesta user
if [ ! -e "$VESTA/data/users/$user" ]; then
echo "Error: vesta user $user doesn't exist"
exit 3
fi
# Checking user homedir
2016-08-10 14:36:54 +03:00
homedir=$(grep "^$user:" /etc/passwd |cut -f 6 -d :)
2015-08-20 18:01:27 +03:00
if [ -z $homedir ]; then
echo "Error: user home directory doesn't exist"
exit 12
fi
# Checking archive
2016-08-10 14:36:54 +03:00
if [ -e "$archive" ]; then
echo "Error: archive already exist $archive"
2015-08-20 18:01:27 +03:00
exit 1
fi
# Checking source path
2016-08-10 14:36:54 +03:00
IFS=$'\n'
i=1
for src in $*; do
if [ "$i" -gt 2 ]; then
rpath=$(readlink -f "$src")
if [ -z "$(echo $rpath |egrep "^/tmp|^$homedir")" ]; then
echo "Error: invalid source path $src"
exit 1
fi
fi
((i++))
done
2015-11-05 03:10:57 +02:00
2016-08-10 14:36:54 +03:00
i=1
for src in $*; do
if [ "$i" -gt 2 ]; then
# Deleting leading home path
src=$(echo "$src"| sed -e "s|/home/$user/||")
2015-11-05 03:10:57 +02:00
2016-08-10 14:36:54 +03:00
# Creating tar.gz archive
sudo -u $user tar -rf "${archive/.gz/}" -C /home/$user $src >\
/dev/null 2>&1
if [ "$?" -ne 0 ]; then
echo "Error: archive $archive was not created"
exit 3
fi
fi
((i++))
done
2015-08-20 18:01:27 +03:00
2016-08-10 14:36:54 +03:00
# Checking gzip
if [[ "$archive" =~ \.gz$ ]]; then
sudo -u $user gzip "${archive/.gz/}" >/dev/null 2>&1
if [ "$?" -ne 0 ]; then
echo "Error: archive $archive was not gziped"
exit 3
fi
2015-08-20 18:01:27 +03:00
fi
exit