Une des façons les plus simples de sauvegarder un système utilise un script shell. Par exemple, un script peut être utilisé pour configurer les répertoires à sauvegarder et transmettre ces répertoires comme arguments à l’utilitaire tar, ce qui crée un fichier d’archive. Le fichier d’archive peut ensuite être déplacé ou copié dans un autre emplacement. L’archive peut également être créée sur un système de fichiers distant tel qu’un montage NFS.
L’utilitaire tar crée un fichier d’archive de plusieurs fichiers ou répertoires. tar peut également filtrer les fichiers par le biais des utilitaires de compression, réduisant ainsi la taille du fichier d’archive.
Script shell simple
Le script shell suivant se sert de tar pour créer une archive sur un montage NFS. Le nom de l’archive est défini en se servant de divers utilitaires en ligne de commande.
#!/bin/bash
####################################
#
# Backup to NFS mount script.
#
####################################
# What to backup.
backup_files="/home /var/spool/mail /etc /root /boot /opt"
# Where to backup to.
dest="/mnt/backup"
# Create archive filename.
day=$(date +%A)
hostname=$(hostname -s)
archive_file="$hostname-$day.tgz"
# Print start status message.
echo "Backing up $backup_files to $dest/$archive_file"
date
echo
# Backup the files using tar.
tar czf $dest/$archive_file $backup_files
# Print end status message.
echo
echo "Backup finished"
date
# Long listing of files in $dest to check file sizes.
ls -lh $dest
$backup_files : variable listant les répertoires que vous souhaitez sauvegarder. La liste doit être adaptée à vos besoins.
$day : une variable contenant le jour de la semaine (lundi, mardi, mercredi, etc.) Cette fonction est utilisée pour créer un fichier d’archive pour chaque jour de la semaine, ce qui donne un historique de sauvegarde de sept jours. Il y a d’autres façons d’accomplir cela, par exemple en utilisant l’utilitaire date.
$hostname : variable contenant le nom d’hôte de votre système en format court. Vous pourrez ainsi placer les archives quotidiennes de plusieurs hôtes dans un même répertoire.
$archive_file : le nom complet de l’archive.
$dest : destination du fichier d’archive. Le répertoire doit être créé et, dans ce cas monté avant d’exécuter le script de sauvegarde. Voir Network File System (NFS) pour des détails sur l’utilisation de NFS.
status messages : messages optionnels affichés dans la console lors de l’utilisation de la commande echo.
tar czf $dest/$archive_file $backup_files : la commande tar utilisée pour créer le fichier d’archive.
c : crée une archive.
z : compresser l’archive avec gzip.
f : sortie vers un fichier d’archive. Sinon, la sortie tar est envoyée vers STDOUT.
ls -lh $dest : instruction optionnelle affichant une liste du répertoire de destination détaillée -l et au format lisible par facilement -h. Ceci est utile pour vérifier rapidement la taille du fichier archive, mais ne devrait pas remplacer le test de celui-ci.
Voici un exemple simple de script shell de sauvegarde. Cependant, il y a beaucoup d’options qui peuvent être inclues dans un tel script. Voir Références pour avoir des liens vers des ressources fournissant des informations plus approfondies sur les scripts shell.
Occasionally I type a password or other sensitive information into a shell prompt. Using bash history, the command can be removed.
# say we start with an empty bash command history
bash-3.2$ history
1 history
# enter a command that requires a password
bash-3.2$ sudo rm -i some_file
Password:
# accidentally ^C and type your password
# into the prompt and hit enter
bash-3.2$ secret_password
bash: secret_password: command not found
# your password is now there for all to
# see in your bash history
bash-3.2$ history
1 history
2 sudo rm -i some_file
3 secret_password
4 history
# first option to fix it, delete the numbered entry from
# history and write to your ~/.bash_history file
bash-3.2$ history -d 3
bash-3.2$ history -w
# entry 3 will be removed entirely from your command history
bash-3.2$ history
1 history
2 sudo rm -i some_file
3 history
4 history -d 3
5 history -w
6 history
# the second option is to clear the entire history
# and write the changes to disk
bash-3.2$ history -c
bash-3.2$ history -w
# it's now pretty obvious that your history has been
# scrubbed clean, but at least your password is history!
bash-3.2$ history
1 history -w
2 history
Shell – Provides an interface between the user and the kernel.
Terminal emulator – The xterm program is a terminal emulator for the X Window System. It allows user to enter commands and display back their results on screen.
Linux Desktop and Windows Manager – Linux desktop is collection of various software apps. It includes the file manger, the windows manager, the Terminal emulator and much more. KDE and Gnome are two examples of the complete desktop environment in Linux.
Login
User can login locally into the console when in runlevel # 3 or graphically when in runlevel # 5 (the level numbers may differ depending on the distribution). In both cases you need to provide username and password. Bash uses the following initialization and start-up files:
/etc/profile – The systemwide initialization file, executed for login shells.
/etc/bash.bashrc – The systemwide per-interactive-shell startup file. This is a non-standard file which may not exist on your distribution. Even if it exists, it will not be sourced unless it is done explicitly in another start-up file.
/etc/bash.logout – The systemwide login shell cleanup file, executed when a login shell exits.
$HOME/.bash_profile – The personal initialization file, executed for login shells.
$HOME/.bashrc – The individual per-interactive-shell startup file.
$HOME/.bash_logout – The individual login shell cleanup file, executed when a login shell exits.
Script of commands executed at login to set up environment. For example, setup JAVA_HOME path.
Login Shell
Login shells are first shell started when you log in to the system. Login shells set environment which is exported to non-login shells. Login shell calls the following when a user logs in:
/etc/profile runs first when a user logs in runlevel # 3 (the level numbers may differ depending on the distribution).
By default any modern Linux distributions will have IP Forwarding disabled. This is normally a good idea, as most peoples will not need IP Forwarding, but if we are setting up a Linux router/gateway or maybe a VPN server (pptp or ipsec) or just a plain dial-in server then we will need to enable forwarding. This can be done in several ways that I will present bellow.
Check if IP Forwarding is enabled
We have to query the sysctl kernel value net.ipv4.ip_forward to see if forwarding is enabled or not: Using sysctl:
or just checking out the value in the /proc system:
cat /proc/sys/net/ipv4/ip_forward
0
As we can see in both the above examples this was disabled (as show by the value 0).
Enable IP Forwarding on the fly
As with any sysctl kernel parameters we can change the value of net.ipv4.ip_forward on the fly (without rebooting the system):
sysctl -w net.ipv4.ip_forward=1
or
echo 1 > /proc/sys/net/ipv4/ip_forward
the setting is changed instantly; the result will not be preserved after rebooting the system.
Permanent setting using /etc/sysctl.conf
If we want to make this configuration permanent the best way to do it is using the file /etc/sysctl.conf where we can add a line containing net.ipv4.ip_forward = 1
/etc/sysctl.conf:
net.ipv4.ip_forward = 1
if you already have an entry net.ipv4.ip_forward with the value 0 you can change that 1.
To enable the changes made in sysctl.conf you will need to run the command:
sysctl -p /etc/sysctl.conf
On RedHat based systems this is also enabled when restarting the network service:
service network restart
and on Debian/Ubuntu systems this can be also done restarting the procps service:
/etc/init.d/procps.sh restart
Using distribution specific init scripts
Although the methods presented above should work just fine and you would not need any other method of doing this, I just wanted to note that there are also other methods to enable IP Forwarding specific to some Linux distributions. For example Debian based distributions might use the setting:
/etc/network/options:
ip_forward=no
set it to yes and restart the network service. Also RedHat distributions might set this using:
/etc/sysconfig/network:
FORWARD_IPV4=true
and again restart the network service.
Regardless the method you have used once you have completed this you can check it out using the same method shown above:
Or dpkg: dpkg -l | grep -E '^ii' | grep <package name>. When it’s not installed it won’t show output. When it is, it’ll show something like:
olivier@neews:~$ dpkg -l | grep -E '^ii' | grep firefox
ii firefox 8.0+build1-0ubuntu0.11.10.3 Safe and easy web browser from Mozilla
ii firefox-branding 8.0+build1-0ubuntu0.11.10.3 Safe and easy web browser from Mozilla - transitional package
ii firefox-globalmenu 8.0+build1-0ubuntu0.11.10.3 Unity appmenu integration for Firefox
ii firefox-gnome-support 8.0+build1-0ubuntu0.11.10.3 Safe and easy web browser from Mozilla - GNOME support
ii firefox-locale-en 8.0+build1-0ubuntu0.11.10.3 English language pack for Firefox
It's obviously a fuzzier search but handy if you're not sure which package you're looking for.
For manually installed things...
A bit harder but if they're on the current path, you could just run them. That's a bit of mission so I'd rather just run:
oli@bert:/$ which chromium-browser
/usr/bin/chromium-browser
And:
oli@bert:/$ which gnuift
# returns nothing
Which is better?
That depends on the sanity of user. There’s nothing to stop somebody installing something called chromium-browser that isn’t Chromium. They could even package it up incorrectly and install that. Neither method can be 100% certain.
But assuming the owner is sane – packages should be good enough for most people.