D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
opt
/
psa
/
admin
/
sbin
/
modules
/
wp-toolkit
/
Filename :
copy
back
Copy
#!/bin/bash # Copyright 1999-2019. Plesk International GmbH. All rights reserved. set -eu PATH_FILEMNG="/usr/local/psa/admin/bin/filemng" PATH_RANDOM_STR="/usr/local/psa/admin/bin/random_str" TMP_PATH_PREFIX=".wp-toolkit" IFS=$'\n' wait_process() { pid="$1" me="$(basename $0)($$):" if [ -z "$pid" ] then echo "$me a PID is required as an argument" >&2 exit 2 fi name=$(ps -p $pid -o comm=) if [ $? -eq 0 ] then echo "$me waiting for PID $pid to finish ($name)" while ps -p $pid > /dev/null; do sleep 1; done; else echo "$me failed to find process with PID $pid" >&2 exit 1 fi } copy_pipe() { echo "Copy '$3' owned by '$1' from '$2' into '$5' owned by '$4'" "${PATH_FILEMNG}" "$1" exec "$2" tar -cf - -- "$3" | "${PATH_FILEMNG}" "$4" exec "$5" tar -xf - } copy_rsync_local() { # parse source paths to extract paths to fetch from the source source_paths_str="$3" declare -a source_paths_arr for source_path in $(echo $source_paths_str | tr ":" "\n"); do source_paths_arr+=("$source_path") done rsync_additional_args_str=${5:-} # parse rsync additional args declare -a rsync_additional_args_arr for rsync_additional_arg in $(echo $rsync_additional_args_str | tr " " "\n"); do rsync_additional_args_arr+=("$rsync_additional_arg") done echo "Sync content of '$4' (target) owned by '$1' with '"${source_paths_arr[@]}"' (source) owned by '$1'" if [ ${#rsync_additional_args_arr[@]} -eq 0 ]; then "${PATH_FILEMNG}" "$1" exec "$2" rsync "${source_paths_arr[@]}" "$4" else "${PATH_FILEMNG}" "$1" exec "$2" rsync "${rsync_additional_args_arr[@]}" "${source_paths_arr[@]}" "$4" fi } copy_rsync_remote() { confId="$1" port="$2" source_owner="$3" source_workdir="$4" source_paths_str="$5" target_owner="$6" target_workdir="$7" target_path="$8" rsync_additional_args_str=${9:-} source_conf_path="$source_workdir/$TMP_PATH_PREFIX/$confId" target_conf_path="$target_workdir/$TMP_PATH_PREFIX/$confId" rsyncd_conf="$source_conf_path/rsyncd.conf" rsyncd_secrets="$source_conf_path/rsyncd.secrets" rsyncd_pid="$source_conf_path/rsyncd.pid" rsyncd_lock="$source_conf_path/rsyncd.lock" rsyncd_log="$source_conf_path/rsyncd.log" password=`"${PATH_RANDOM_STR}"` password_file="$target_conf_path/rsync.password" # create service directories if [ ! -d "$source_conf_path" ]; then "${PATH_FILEMNG}" "$source_owner" exec "$source_workdir" mkdir -p "$source_conf_path" || exit 1 fi if [ ! -d "$target_conf_path" ]; then "${PATH_FILEMNG}" "$target_owner" exec "$target_workdir" mkdir -p "$target_conf_path" || exit 1 fi # register cleanup trap "cleanup_rsyncd $rsyncd_pid $source_conf_path $target_conf_path" EXIT ERR INT TERM # as the source owner, create rsyncd configuration file "${PATH_FILEMNG}" "$source_owner" exec "$source_workdir" touch "$rsyncd_conf" || exit 1 cat > "$rsyncd_conf" << EOL read only = yes use chroot = no address = localhost pid file = $rsyncd_pid lock file = $rsyncd_lock log file = $rsyncd_log [root] path = $source_workdir comment = Root of subscription auth users = $target_owner secrets file = $rsyncd_secrets EOL # as the source owner, provide access to the content for the target owner "${PATH_FILEMNG}" "$source_owner" exec "$source_workdir" touch "$rsyncd_secrets" || exit 1 chmod 600 "$rsyncd_secrets" || exit 1 echo "$target_owner:$password" > "$rsyncd_secrets" || exit 1 # as the target owner, place the given password into file "${PATH_FILEMNG}" "$target_owner" exec "$target_workdir" touch "$password_file" || exit 1 chmod 600 "$password_file" || exit 1 echo "$password" > "$password_file" || exit 1 # as the source owner, start rsyncd "${PATH_FILEMNG}" "$source_owner" exec "$source_workdir" rsync --daemon --config "$rsyncd_conf" --port "$port" || exit 1 # parse source paths to extract paths to fetch declare -a source_paths_arr for source_path in $(echo $source_paths_str | tr ":" "\n"); do source_paths_arr+=("::root/$source_path") done # specify hostname for first path source_paths_arr[0]="$target_owner@localhost${source_paths_arr[0]}" # parse rsync additional args declare -a rsync_additional_args_arr for rsync_additional_arg in $(echo $rsync_additional_args_str | tr [:space:] "\n"); do rsync_additional_args_arr+=("$rsync_additional_arg") done # as the target owner, fetch content from the source echo "Sync content of '$target_path' (target) owned by '$target_owner' with '"${source_paths_arr[@]}"' (source) owned by '$source_owner'" if [ ${#rsync_additional_args_arr[@]} -eq 0 ]; then "${PATH_FILEMNG}" "$target_owner" exec "$target_workdir" rsync --password-file "$password_file" "${source_paths_arr[@]}" "$target_path" --port "$port" || exit 1 else "${PATH_FILEMNG}" "$target_owner" exec "$target_workdir" rsync "${rsync_additional_args_arr[@]}" --password-file "$password_file" "${source_paths_arr[@]}" "$target_path" --port "$port" || exit 1 fi } cleanup_rsyncd() { if [ -f "$1" ]; then pid="`cat "$1"`" kill $pid || true wait_process $pid fi rm -rf "$2" "$3" } print_usage() { echo "Usage: $0 pipe <source system user> <source workdir> <source path> <target system user> <target workdir> $0 rsync-local <system user> <workdir> <source path> <target path> $0 rsync-remote <conf id> <port> <source system user> <source workdir> <source path(s)> <target system user> <target workdir> <target path> [<rsync additional args>] Example: To copy directory '/var/www/vhosts/domain1.tld/httpdocs/wordpress' owned by 'domain1_user' into '/var/www/vhosts/domain2.tld/subdomain' owned by 'domain2_user' need to run: $0 pipe domain1_user /var/www/vhosts/domain1.tld/httpdocs wordpress domain2_user /var/www/vhosts/domain2.tld/subdomain Destination path will be '/var/www/vhosts/domain2.tld/subdomain/wordpress' owned by 'domain2_user'" exit 1 } case "${1:-}" in pipe) if [ ! $# -eq 6 ]; then print_usage fi copy_pipe "${2:-}" "${3:-}" "${4:-}" "${5:-}" "${6:-}" ;; rsync-local) if [ $# -lt 5 ]; then print_usage fi copy_rsync_local "${2:-}" "${3:-}" "${4:-}" "${5:-}" "${6:-}" ;; rsync-remote) if [ $# -lt 9 ]; then print_usage fi copy_rsync_remote "${2:-}" "${3:-}" "${4:-}" "${5:-}" "${6:-}" "${7:-}" "${8:-}" "${9:-}" "${10:-}" ;; *) print_usage esac