hey_ram
July 20th, 2009, 08:23 PM
hi all,
i was trying to write a bash script to back up some files and folders on my system. though the script works fine, there are some things that were not very clear to me. i was hoping someone could shed some more light on that. first, the script:
#this is a script to back up all the data that I do not want lost.
#!/bin/bash #Use the bash script.
#sudo su
#These are the things to add.
#1. Check for last version of backed up file. File should be backed up only if changes have been made to it since last backup.
if [ ! -d Backup.tar.gz ] #check if Backup directory does not exist.
then
#directory does not exist. create new directory.
mkdir Backup/
else
echo "Backup already exists."
echo "Deleting Backup..."
rm -rf Backup/ #removing Backup.
rm -rf Backup.tar.gz
fi
#copy .vimrc.
echo "Backing up .vimrc"
cp ~/.vimrc Backup//.vimrc
#copy vim syntax folder.
echo "Backing up vim syntax folder."
cp -r ~/.vim/syntax Backup/
#copy cprogs.
echo "Backing up C programs."
cp -r ~/cprogs Backup/
#copy c++ directory.
echo "Backing up C++ programs."
cp -r ~/c++ Backup/
#copy web designing files.
echo "Backing up Web designing files."
cp -r ~/Desktop/Web\ designing\ files Backup/
#copy .bashrc and other bash files.
echo "Backing up bash files: .bashrc, .bash_history, .bash_logout"
cp ~/.bashrc Backup/
cp ~/.bash_history Backup/
cp ~/.bash_logout Backup/
#copy $PATH from printenv
echo "Backing up \$PATH environment variable."
echo $PATH > ~/path.txt
cp ~/path.txt Backup/
rm path.txt #remove path.txt
#copy other vim files.
echo "Backing up other vim files: .viminfo, .vim"
cp ~/.viminfo Backup/
cp -r ~/.vim Backup/
#copy shell scripts.
echo "Backing up Shell scripts."
cp -r shell_scripts/ Backup/
#copy tcl programs.
echo "Backing up tcl scripts."
cp -r tclprogs/ Backup/
#copy .tomboy directory and all its contents.
echo "Backing up notes written in Tomboy."
cp -r .tomboy/ Backup/
#copy /etc/fstab.
echo "Copying /etc/fstab."
cp /etc/fstab Backup/
#copy /etc/apt. Contains sources.list, a list of all sources (repositories) that apt-get looks for when downloading/installing a software.
echo "Backing up /etc/apt."
cp -r /etc/apt Backup/
#create a compressed version of backup.
echo "Create a compressed version of the backup folder. Archiving into .tar.gz."
tar -czpf Backup.tar.gz Backup/
echo "Removing Backup/ directory."
rm -rf Backup/ #remove the directory so it does not take up more space.
#echo "Exiting root user account."
#exit #exit root account and get back to user account.
the questions I had are:
1. while copying the /etc/apt directory, i get the following error messages:
Backing up /etc/apt.
cp: cannot open `/etc/apt/trustdb.gpg' for reading: Permission denied
cp: cannot open `/etc/apt/secring.gpg' for reading: Permission denied
when i look into the file permissions for these two files, they are as follows:
-rw------- 1 root root 1200 2008-09-21 18:11 /etc/apt/trustdb.gpg
is there a way around this given the following "constraints":
a. i do not want to do a sudo su, become root user and copy these files.
b. i do not want to change file permissions using chmod. i could do it on a temporary basis but that would also involve becoming the super user.
2. i was earlier doing the sudo su routine and used the "exit" command (see the 2 commented lines at the end) to exit the root user account. when i run the script, it just returns to the root user command prompt. the script does not print any of the "Backing up .." messages and neither does it exit the root user account until an explicit exit command is given. any ideas why this may be happening? this way of backing up files and folders, i realize, is dangerous since by not exiting the root user account, it gives the "normal" user extra privileges.
3. are there any files that i am missing from the list? i wanted to back up my firefox bookmarks list, but do not know where they may be on the system. any hints are most welcome.
4. how can i schedule it (from within the script) to make it run periodically?
thanks.
i was trying to write a bash script to back up some files and folders on my system. though the script works fine, there are some things that were not very clear to me. i was hoping someone could shed some more light on that. first, the script:
#this is a script to back up all the data that I do not want lost.
#!/bin/bash #Use the bash script.
#sudo su
#These are the things to add.
#1. Check for last version of backed up file. File should be backed up only if changes have been made to it since last backup.
if [ ! -d Backup.tar.gz ] #check if Backup directory does not exist.
then
#directory does not exist. create new directory.
mkdir Backup/
else
echo "Backup already exists."
echo "Deleting Backup..."
rm -rf Backup/ #removing Backup.
rm -rf Backup.tar.gz
fi
#copy .vimrc.
echo "Backing up .vimrc"
cp ~/.vimrc Backup//.vimrc
#copy vim syntax folder.
echo "Backing up vim syntax folder."
cp -r ~/.vim/syntax Backup/
#copy cprogs.
echo "Backing up C programs."
cp -r ~/cprogs Backup/
#copy c++ directory.
echo "Backing up C++ programs."
cp -r ~/c++ Backup/
#copy web designing files.
echo "Backing up Web designing files."
cp -r ~/Desktop/Web\ designing\ files Backup/
#copy .bashrc and other bash files.
echo "Backing up bash files: .bashrc, .bash_history, .bash_logout"
cp ~/.bashrc Backup/
cp ~/.bash_history Backup/
cp ~/.bash_logout Backup/
#copy $PATH from printenv
echo "Backing up \$PATH environment variable."
echo $PATH > ~/path.txt
cp ~/path.txt Backup/
rm path.txt #remove path.txt
#copy other vim files.
echo "Backing up other vim files: .viminfo, .vim"
cp ~/.viminfo Backup/
cp -r ~/.vim Backup/
#copy shell scripts.
echo "Backing up Shell scripts."
cp -r shell_scripts/ Backup/
#copy tcl programs.
echo "Backing up tcl scripts."
cp -r tclprogs/ Backup/
#copy .tomboy directory and all its contents.
echo "Backing up notes written in Tomboy."
cp -r .tomboy/ Backup/
#copy /etc/fstab.
echo "Copying /etc/fstab."
cp /etc/fstab Backup/
#copy /etc/apt. Contains sources.list, a list of all sources (repositories) that apt-get looks for when downloading/installing a software.
echo "Backing up /etc/apt."
cp -r /etc/apt Backup/
#create a compressed version of backup.
echo "Create a compressed version of the backup folder. Archiving into .tar.gz."
tar -czpf Backup.tar.gz Backup/
echo "Removing Backup/ directory."
rm -rf Backup/ #remove the directory so it does not take up more space.
#echo "Exiting root user account."
#exit #exit root account and get back to user account.
the questions I had are:
1. while copying the /etc/apt directory, i get the following error messages:
Backing up /etc/apt.
cp: cannot open `/etc/apt/trustdb.gpg' for reading: Permission denied
cp: cannot open `/etc/apt/secring.gpg' for reading: Permission denied
when i look into the file permissions for these two files, they are as follows:
-rw------- 1 root root 1200 2008-09-21 18:11 /etc/apt/trustdb.gpg
is there a way around this given the following "constraints":
a. i do not want to do a sudo su, become root user and copy these files.
b. i do not want to change file permissions using chmod. i could do it on a temporary basis but that would also involve becoming the super user.
2. i was earlier doing the sudo su routine and used the "exit" command (see the 2 commented lines at the end) to exit the root user account. when i run the script, it just returns to the root user command prompt. the script does not print any of the "Backing up .." messages and neither does it exit the root user account until an explicit exit command is given. any ideas why this may be happening? this way of backing up files and folders, i realize, is dangerous since by not exiting the root user account, it gives the "normal" user extra privileges.
3. are there any files that i am missing from the list? i wanted to back up my firefox bookmarks list, but do not know where they may be on the system. any hints are most welcome.
4. how can i schedule it (from within the script) to make it run periodically?
thanks.