Guest User

Untitled

a guest
Jan 6th, 2024
434
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.13 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Variables
  4. REMOTE_SERVER="111.111.111.111"
  5. REMOTE_PATH="/backup" # Change this to your remote backup directory
  6. REMOTE_USER="root"
  7. SSH_PORT=22
  8.  
  9. # If a username is provided as an argument, overwrite the USERS array
  10. if [ "$1" ]; then
  11.     USERS=("$1")
  12. else
  13.     # Get all users (for cPanel, adjust for Hestia if needed)
  14.     USERS=($(ls /var/cpanel/users/))
  15. fi
  16.  
  17. # Counter initialization
  18. counter=1
  19. total=${#USERS[@]}
  20.  
  21. # Backup, SCP, and Restore for each user
  22. for user in "${USERS[@]}"; do
  23.     echo "Processing user $user ($counter/$total)"
  24.  
  25.     # Trigger cPanel's backup for the user
  26.     /scripts/pkgacct $user
  27.     if [ $? -ne 0 ]; then
  28.         echo "Error during backup for user $user. Exiting."
  29.         exit 1
  30.     fi
  31.  
  32.     # Find the most recent backup file for the user in the /home directory
  33.     BACKUP_FILE_NAME=$(ls -t /home/cpmove-${user}*.tar.gz | head -n 1)
  34.     if [ -z "$BACKUP_FILE_NAME" ]; then
  35.         echo "Error getting backup file for user $user. Exiting."
  36.         exit 1
  37.     fi
  38.  
  39.     # Move the file using SCP to the destination server
  40.     scp -P $SSH_PORT "$BACKUP_FILE_NAME" "$REMOTE_USER@$REMOTE_SERVER:$REMOTE_PATH/"
  41.     if [ $? -ne 0 ]; then
  42.         echo "Error during SCP for user $user. Exiting."
  43.         exit 1
  44.     fi
  45.  
  46.     # Change directory to /backup on the remote server and then restore the file
  47.     ssh -t -p $SSH_PORT "$REMOTE_USER@$REMOTE_SERVER" "
  48.        source /etc/profile
  49.        cd $REMOTE_PATH && /usr/local/hestia/bin/v-import-cpanel $(basename $BACKUP_FILE_NAME)"
  50.     if [ $? -ne 0 ]; then
  51.         echo "Error during restore for user $user. Exiting."
  52.         exit 1
  53.     fi
  54.  
  55.     if [ $? -ne 0 ]; then
  56.         echo "Error during restore for user $user. Exiting."
  57.         exit 1
  58.     fi
  59.  
  60.     # Cleanup backup file after transfer
  61.     rm -f "$BACKUP_FILE_NAME"
  62.  
  63.     # Cleanup remote /backup after restoration
  64.     ssh -p $SSH_PORT "$REMOTE_USER@$REMOTE_SERVER" "rm -rfv $REMOTE_PATH/*"
  65.     if [ $? -ne 0 ]; then
  66.         echo "Error during remote cleanup after restoration for user $user. Exiting."
  67.         exit 1
  68.     fi
  69.  
  70.     # Increment counter
  71.     ((counter++))
  72. done
Advertisement
Add Comment
Please, Sign In to add comment