The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Script to move user backup to their dir

Discussion in 'cPanel Developers' started by Diego.Vieira, May 21, 2012.

  1. Diego.Vieira

    Diego.Vieira Member

    Joined:
    Jul 19, 2010
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    1
    I have been thinking about allowing customers to download their own backup, so I deicide to wrote this.
    It's very simple as I'm not very skilled with shell programming, but does the job.

    Create file /move-backup-to-users.sh, paste the code below and chmod to 700:
    Code:
    #!/bin/sh
    BACKUPDIR=/home/backup #without trailing slash
    
    #don't change if you don't know what it is for
    BACKUPTYPES=$(ls $BACKUPDIR/cpbackup/)
    USERS=$(ls /home/) #users
    DATE=$(date)
    
    for u in $USERS
    do
    	for t in $BACKUPTYPES
    	do
    		if [ -f $BACKUPDIR/cpbackup/$t/$u.tar.gz ];
    		then
    			mv -f $BACKUPDIR/cpbackup/$t/$u.tar.gz /home/$u/backup-$t-$u.tar.gz
    			chown $u:$u /home/$u/backup-$t-$u.tar.gz
    		
    			if [ -f /home/$u/backup_email.txt ];
    			then
    				source /home/$u/backup_email.txt
    				/bin/mail -s "$SUBJECT" "$EMAIL" < $MESSAGE
    				rm $MESSAGE
    			fi
    		
    			# debug
    			echo $BACKUPDIR/cpbackup/$t/$u.tar.gz /home/$u/backup-$t-$u.tar.gz$n
    			echo chown $u:$u /home/$u/backup-$t-$u.tar.gz
    			echo $n
    			# debug
    		else
    			echo "File not found $BACKUPDIR/cpbackup/$t/$u.tar.gz"
    		fi
    	done
    done
    
    Create the file /scripts/postcpbackup (if doesn't exists) and paste this line:
    Code:
    /move-backup-to-users.sh
    Create the file /scripts/precpbackup (if doesn't exists) and paste this:
    Code:
    #!/bin/sh
    #don't change if you don't know what it is for
    USERS=$(ls /home/) #users
    BACKUPTYPES=daily weekly monthly
    
    for u in $USERS
    do
    	for t in $BACKUPTYPES
    	do
    		if [ -f $BACKUPDIR/cpbackup/$t/$u.tar.gz ];
    		then
    			rm /home/$u/backup-$t-$u.tar.gz
    		fi
    	done
    done
    
    Save this as txt to /home/username/backup_email.txt if you want the user to be emailed
    Since I have customer from three different countries, I have translated this to other languages as well
    Go to WHM -> Backup -> Configure Backup -> Check: Execute Pre/Post Backup Script
    /scripts/precpbackup
    /scripts/postcpbackup

    Test by running /./move-backup-to-users.sh and /scripts/cpbackup


    Feel free to modify, share, improve.
     
    #1 Diego.Vieira, May 21, 2012
    Last edited: May 21, 2012
  2. WhiteDog

    WhiteDog Well-Known Member

    Joined:
    Feb 19, 2008
    Messages:
    118
    Likes Received:
    0
    Trophy Points:
    16
    Hello Diego,

    This is a nice addition.

    I'm sure you are aware that users can already download their "daily", "weekly" and "monthly" backups from within cPanel? But perhaps you wanted to offer this "extra" so they can download via FTP and things?

    Personally, this kind of "scripts" should be integrated within cPanel itself so we as admins can allow the end-user to turn this on/of for his account :)
     
Loading...

Share This Page