Results 1 to 2 of 2

Thread: Script to move user backup to their dir

  1. #1
    Member
    Join Date
    Jul 2010
    Posts
    15

    Lightbulb Script to move user backup to their dir

    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
    SUBJECT="New Backup Available"
    EMAIL="user@email.com"
    MESSAGE="/tmp/emailmessage.txt"
    echo "$DATE" >> $MESSAGE
    echo "This is an email to tell you that a new backup file is available: /home/$u/backup-$t-$u.tar.gz" >> $MESSAGE
    echo "We recommend that you download this backup from time to time" >> $MESSAGE
    echo "If the backup is too large, try to download old things or asks us to exclude large directories" >> $MESSAGE
    echo "or directories that you don't need to backup" >> $MESSAGE
    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.
    Last edited by Diego.Vieira; 05-21-2012 at 01:09 AM.

  2. #2
    Member
    Join Date
    Feb 2008
    Posts
    81

    Default Re: Script to move user backup to their dir

    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

Similar Threads

  1. Modifying cpbackup script to skip home dir
    By Poopool in forum Data Protection
    Replies: 5
    Last Post: 04-16-2010, 03:08 PM
  2. cPanel / WHM Backup script User
    By egsi in forum New User Questions
    Replies: 1
    Last Post: 11-11-2009, 07:20 PM
  3. Addon Script no install in root dir?
    By silicon in forum cPanel & WHM Discussions
    Replies: 0
    Last Post: 03-06-2006, 01:40 PM
  4. Cpanel-backup-full backup to home dir
    By Blue Steel in forum cPanel & WHM Discussions
    Replies: 1
    Last Post: 03-20-2005, 06:17 AM
  5. A PHP script that you can use to backup all user accounts
    By Roy@ENHOST in forum cPanel & WHM Discussions
    Replies: 17
    Last Post: 11-27-2004, 02:12 PM