User avatar
Posts: 373
Joined: Sat Jul 21, 2012 10:09 pm
Location: Zulia, Venezuela
Contact: Website

Remote backup script

Tue Feb 19, 2013 9:09 pm

Hello Pi Enthusiasts!

For downloading some remote folder for a local backup, I´m wrote the following bash script, this script opens a rsync link with the remote host, and grab the specified files/folders in a local folder, for late uploading to the final destination server.

My problem is that I need to switch this task to background, thus enabling me for close the ssh session (and hence, my client in my Windoze laptop) and save some watts of electrical power.

I´m tried putting a & at the end of relevant rsync command line, but no luck...

Here is my script:

Code: Select all

# Set the date for logfile.
MyDate="`date +'%d_%b_%Y'`"
# Backup the /var/www/html/ folder:
rsync -avz [email protected]:/var/www/html/ /home/pi/LocalBackupFolder/ &> "$MyDate$LogFile"
Any help/hints will be greatly appreciated :)
Telecom Specialist (Now Available for Hire!)

User avatar
Posts: 33
Joined: Fri Jan 04, 2013 4:17 am

Re: Remote backup script

Tue Feb 19, 2013 11:26 pm

The screen program is an awesome tool that, among other things, can help you with this problem.

sudo apt-get install screen
man screen
google: unix screen

The basic problem you're having, FYI, is that the rsync app on the pi is running in a terminal connected to your ssh session. When you end the session, the terminal goes away, and everything on that terminal gets killed, including rsync.

There are other tools that can help with this problem, probably the next most common one is nohup, but screen has the nice aspect that you can return to your full terminal session later, not just check the output from the app in a log file.

Posts: 20
Joined: Wed Feb 13, 2013 5:13 pm

Re: Remote backup script

Wed Feb 20, 2013 9:18 am

Screen and nohup are useful commands but there will be times when you have a job running that you haven't started via screen or nohup and you don't want to have to restart to leave it running when you log off. If that is the case then you can disown your job.

First push your job to the background (<ctrl+z> to pause the job then 'bg' to start it running in the background, if you started your job with an '&' at the end then it is already running in the background). Once it is running in the background then the disown command can be used to disown it. If you only have one job (the 'jobs' command should give you a list of current jobs and their status) then simply use 'disown' with no parameters will disown it. If you have more than one job running you can use its Process ID (PID) to specify which job to disown.

For more information 'man bash' and read the sections on 'SIGNALS' and 'JOB CONTROL'

Return to “General programming discussion”