Bash script to take cPanel backup in batches


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash script to take cPanel backup in batches
# 1  
Old 05-31-2018
Bash script to take cPanel backup in batches

I'm trying to resolve the below scenario by writing bash script.

On my managed linux server I have about 30 accounts and there is not enough space to generate full 30 accounts backup in one go and transfer it via SFTP to my Backup Synology Server. What I'm thinking of doing is breaking the backup into batches and transfer it over accordingly to prevent server choking:

Steps:
Quote:
Generate 5 accounts cPanel Backup
Transfer the backup generated to Synology backup server via SFTP
Delete the backups on source server.
Repeat the process until all accounts are transferred in batches.
a. I have create 5 list of accounts into each array and named it cPList1 cPList2 ....and so on:
e.g.
cPList1(account1 account2 account3)
cPList2(account4 account 5 account6)
b. Then I have created another array list that contains all cPLists
e.g. mainList(cPList1 cPList2)
The logic that I'm trying to use is:

Code:
#Outer Loop to process mainList array elements
for i in "${mainList[@]}"
do
#Inner Loop to to process cPList array elements
for j in "${i[@]}" **

<<--I need help with this part, I was expecting the j variable to be accounts1, acocunts2 ...and so because it is supposed to be the elements of i variable. However, when I echo both i and j they hold same values i'e cPList1, cPList2 ....

SFTP 5 accounts backup to Synology
delete transferred files
done
done


Any input will be appreciated.
# 2  
Old 06-01-2018
This should work in newer bash and ksh.
Additional error handling should be done against utilities used against accounts.
Code:
#!/usr/bin/ksh
begin=0
i=0
nump=5 # number of accounts per processing, increase or decrease

# plain text file with account1 to account39, each in new line is created named 'accs.txt'
# for test input, fill array 'accounts' with results

while read accs 
do
	accounts[$i]=$accs
	i=$(($i+1))
done < accs.txt

# number of iterations required for processing, we divide total number of elems in array with $nump
ITT="$(( ${#accounts[@]} / $nump ))"

for (( f=$begin; f<=$ITT; f++ ))
do
	# If utilities used support multiple accounts input, a for loop is not neccesary.
	# echo "${accounts[@]:$begin:$nump}"
	# this takes care of reminder as well
	for account in "${accounts[@]:$begin:$nump}"
	do
		echo "work (backup, sftp, cleanup) against for $account"	
	done
	begin=$(($begin+$nump))
	sleep 1 # we sleep here 1 second after 5 accounts have been processed, you might not.
done

Hope that helps
Regards
Peasant.
This User Gave Thanks to Peasant For This Post:
# 3  
Old 06-01-2018
Simplified: the outer loop increments by 5
Code:
#!/bin/bash
# bash or ksh93
nump=5 # number of accounts per processing, increase or decrease

# plain text file with account1 to account39, each in new line is created named 'accs.txt'
# for test input, fill array 'accounts' with results
na=0
while read accs 
do
  accounts[na]=$accs
  na=$((na+1))
done < accs.txt

for (( begin=0; begin<na; begin+=nump ))
do
  # If utilities used support multiple accounts input, a for loop is not neccesary.
  # this takes care of reminder as well
  for account in "${accounts[@]:begin:nump}"
  do
    echo "work on $account"
  done
  echo "group action for ${accounts[@]:begin:nump}"
  sleep 1 # we sleep here 1 second after 5 accounts have been processed, you might not.
done

Now a "classic" variant without an array
Code:
#!/bin/sh
# any Posix sh
nump=5 # number of accounts per processing, increase or decrease

# the final group action is after the loop, so we put it in a function
group_action(){
  echo "group action on
$accounts"
  sleep 1
}

# plain text file with account1 to account39, each in new line is created named 'accs.txt'
i=0 na=0 accounts=""
while read account 
do
  accounts=$accounts${accounts:+"
"}$account
  echo "work on $account"
  if [ $((i+=1)) -eq $nump ]
  then
    group_action
    accounts=""
    i=0
  fi
  na=$((na+1))
done < accs.txt
[ $i -ne 0 ] && group_action


Last edited by MadeInGermany; 06-01-2018 at 09:01 AM..
# 4  
Old 06-08-2018
@peasant,

Thank you for the solution, worked smoothly.

---------- Post updated 06-08-18 at 09:15 AM ---------- Previous update was 06-07-18 at 02:23 PM ----------

Also thanks to 2 other replies.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to get cpanel backup data in rescue mode?

How to get cpanel backup data in rescue mode? Server OS 6.3 minimal with cPanel /dev/sdb1 is main partition root@rescue ~ # fdisk -l Anyone can help Thank you (0 Replies)
Discussion started by: jaydul
0 Replies

2. Homework & Coursework Questions

Create a simple bash backup script of a file

This is the problem: Write a script that will make a backup of a file giving it a ‘.bak’ extension & verify that it works. I have tried a number of different scripts that haven't worked and I haven't seen anything really concise and to the point via google. For brevity's sake this is one of the... (4 Replies)
Discussion started by: demet8
4 Replies

3. Shell Programming and Scripting

Help with Backup Shell Script for Network Device Configuration backup

HI all, im new to shell scripting. need your guidence for my script. i wrote one script and is attached here Im explaining the requirement of script. AIM: Shell script to run automatically as per scheduled and backup few network devices configurations. Script will contain a set of commands... (4 Replies)
Discussion started by: saichand1985
4 Replies

4. Shell Programming and Scripting

Bash Backup script

Hello, I'm supposed to create a simple script to backup onto a network using tar and scp -r command as well as the ~.bashrc to designate commands as a part of bash. I am having trouble making mine work. I would like to ask for input as to what I may be doing wrong. Here it is: !/bin/bash # #... (1 Reply)
Discussion started by: polineni
1 Replies

5. Shell Programming and Scripting

Help: Bash backup script (includes copy, test-

Basically it's for a work assignment. Have to make a menu with the following choices ***************menu********************* 1) Show Current Directory 2) Dispaly Current Time and Date 3) Copy 4) Change Password 5) write directory to file 6) Edit File Directory 7) Make backup from... (1 Reply)
Discussion started by: Covax
1 Replies

6. Shell Programming and Scripting

Running batches of files at a time from a script

Hi I have a script that performs a process on a file. I want to know how to include a function to run a batch of files? Here is my script #!/bin/bash #---------------------------------------------------------------------------------------------------------------------- #This... (2 Replies)
Discussion started by: ladyAnne
2 Replies

7. Shell Programming and Scripting

[bash] Simple backup (cp) script but incremental

Hi all, I would need a rather simple bash backup script that loops throught the (local) users and for each users backs up (cp!) its /home/username folder. About the functionalities: The script has to run every 2 hours (that's cron, so don't mind about that) and the files should be copied to... (12 Replies)
Discussion started by: laurens
12 Replies

8. Shell Programming and Scripting

script for cpanel

Hello, I'm Have 1 Question abut if i need to run another script in my bash script by example /scripts/killacct this script for cpanel but when i try to execute this command /scripts/killacct username he ask me yes or no any idea to answer on this question with yes in my bash script I'm... (2 Replies)
Discussion started by: LinuxCommandos
2 Replies

9. Shell Programming and Scripting

Bash script to backup each domain?

Hi there, I need sh script to create a backup per domain and then ftp each file to different host. let's say I have bunch of domains in /var/www/vhosts/ so when sh will be executed it will create something like domain.com.tar.gz domain2.tar.gz Can somebody help? Thank you, Dmitry (0 Replies)
Discussion started by: dmitryseliv
0 Replies

10. Shell Programming and Scripting

Need to know abt script that invokes batches and get d log files if batches failed

hi , I need to know commands to be used in the script to invoke batches in order from other scripts and then run those batches,and how to take those logs of those batches which fails........If anyone give me a better idea to complete this entire task in a single script... (5 Replies)
Discussion started by: gopimeklord
5 Replies
Login or Register to Ask a Question