Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Variables
- REMOTE_SERVER="111.111.111.111"
- REMOTE_PATH="/backup" # Change this to your remote backup directory
- REMOTE_USER="root"
- SSH_PORT=22
- # If a username is provided as an argument, overwrite the USERS array
- if [ "$1" ]; then
- USERS=("$1")
- else
- # Get all users (for cPanel, adjust for Hestia if needed)
- USERS=($(ls /var/cpanel/users/))
- fi
- # Counter initialization
- counter=1
- total=${#USERS[@]}
- # Backup, SCP, and Restore for each user
- for user in "${USERS[@]}"; do
- echo "Processing user $user ($counter/$total)"
- # Trigger cPanel's backup for the user
- /scripts/pkgacct $user
- if [ $? -ne 0 ]; then
- echo "Error during backup for user $user. Exiting."
- exit 1
- fi
- # Find the most recent backup file for the user in the /home directory
- BACKUP_FILE_NAME=$(ls -t /home/cpmove-${user}*.tar.gz | head -n 1)
- if [ -z "$BACKUP_FILE_NAME" ]; then
- echo "Error getting backup file for user $user. Exiting."
- exit 1
- fi
- # Move the file using SCP to the destination server
- scp -P $SSH_PORT "$BACKUP_FILE_NAME" "$REMOTE_USER@$REMOTE_SERVER:$REMOTE_PATH/"
- if [ $? -ne 0 ]; then
- echo "Error during SCP for user $user. Exiting."
- exit 1
- fi
- # Change directory to /backup on the remote server and then restore the file
- ssh -t -p $SSH_PORT "$REMOTE_USER@$REMOTE_SERVER" "
- source /etc/profile
- cd $REMOTE_PATH && /usr/local/hestia/bin/v-import-cpanel $(basename $BACKUP_FILE_NAME)"
- if [ $? -ne 0 ]; then
- echo "Error during restore for user $user. Exiting."
- exit 1
- fi
- if [ $? -ne 0 ]; then
- echo "Error during restore for user $user. Exiting."
- exit 1
- fi
- # Cleanup backup file after transfer
- rm -f "$BACKUP_FILE_NAME"
- # Cleanup remote /backup after restoration
- ssh -p $SSH_PORT "$REMOTE_USER@$REMOTE_SERVER" "rm -rfv $REMOTE_PATH/*"
- if [ $? -ne 0 ]; then
- echo "Error during remote cleanup after restoration for user $user. Exiting."
- exit 1
- fi
- # Increment counter
- ((counter++))
- done
Advertisement
Add Comment
Please, Sign In to add comment